Skip to content

Instantly share code, notes, and snippets.

@shreyasbharath
Last active December 20, 2017 22:21
Show Gist options
  • Save shreyasbharath/4993fda80709d130207614f2cec9d03d to your computer and use it in GitHub Desktop.
Save shreyasbharath/4993fda80709d130207614f2cec9d03d to your computer and use it in GitHub Desktop.
FileSystem vs Database
require 'benchmark/ips'
require 'daybreak'
require 'dbm'
require 'fileutils'
require 'sqlite3_hash'
require 'xxhash'
class DaybreakDb
def initialize(db_file)
@db_file = db_file
@store = Daybreak::DB.new(@db_file)
end
def []=(key, val)
hash = XXhash.xxh32(key, 123_45)
@store[hash] = val
end
def [](key)
hash = XXhash.xxh32(key, 123_45)
@store[hash]
end
def values
@store.keys.map { |k| @store[k] }
end
def keys
@store.keys
end
def delete(key)
hash = XXhash.xxh32(key, 123_45)
@store.delete(hash)
end
def stop
@store.close unless @store.closed?
end
end
class DbmDb
@store = nil
def initialize(db_file)
@store = DBM.new(db_file)
end
def []=(key, val)
@store[key] = val
end
def [](key)
@store[key]
end
def values
@store.values
end
def keys
@store.keys
end
def delete(key)
@store.delete(key)
end
def stop
@store.close unless @store.closed?
end
end
class Sqlite3HashDb
def initialize(db_file)
@store = SQLite3Hash.new(db_file)
end
def []=(key, val)
hash = XXhash.xxh32(key, 123_45)
@store[hash] = val
end
def [](key)
hash = XXhash.xxh32(key, 123_45)
@store[hash]
end
def key?(key)
hash = XXhash.xxh32(key, 123_45)
@store.key?(hash)
end
def delete(key)
hash = XXhash.xxh32(key, 123_45)
@store.delete(hash)
end
def stop
end
def file_hashes
5000.times.map do
random_string = (0...16).map { (65 + rand(26)).chr }.join
random_filename = File.join('obj', random_string + '.depends')
content = (0..512).map { (65 + rand(26)).chr }.join
{name: random_filename, content: content}
end
end
def create_files(files)
files.each do |file|
File.write(file[:name], file[:content], mode: 'w')
end
end
def fill_db(db_class, db_file, files)
FileUtils.rm_rf(db_file)
db = db_class.new(db_file)
files.each do |file|
db[file[:name]] = file[:content]
end
db.stop
end
def fill_dbs(files)
fill_db(DaybreakDb, 'daybreak.db', files)
fill_db(DbmDb, 'dbm.db', files)
fill_db(Sqlite3HashDb, 'sqlite3hash.db', files)
end
def access_files_raw(files)
files.each do |file|
content = File.read(file)
puts 'blah' if content == 'blah'
end
end
def access_files_db(files, db_class, db_file)
db = db_class.new(db_file)
files.each do |file|
content = db[file]
puts 'blah' if content == 'blah'
end
db.stop
end
# FileUtils.rm_rf('obj')
# Dir.mkdir('obj')
# files = file_hashes
# create_files(files)
# fill_dbs(files)
files = Dir.glob('obj/**/*')
Benchmark.ips do |x|
x.report('raw:') { access_files_raw(files) }
x.report('daybreakdb:') { access_files_db(files, DaybreakDb, 'daybreak.db') }
x.report('dbmdb:') { access_files_db(files, DbmDb, 'dbm.db') }
x.report('sqlite3hash:') { access_files_db(files, Sqlite3HashDb, 'sqlite3hash.db') }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment