Skip to content

Instantly share code, notes, and snippets.

@palexander
Created April 28, 2015 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palexander/bf51131d7237e5bcab38 to your computer and use it in GitHub Desktop.
Save palexander/bf51131d7237e5bcab38 to your computer and use it in GitHub Desktop.
Benchmark Ruby storage mechanisms
def time_code(name, &block)
t1 = Time.now
yield
puts "#{name}: #{(Time.now - t1).to_f.round(2)}s"
end
def time_code_aggregate(name, &block)
unless @tc_at_exit
at_exit do
@tc_timers.each do |name, time|
puts "#{name}: #{time.to_f.round(2)}s"
end
end
end
@tc_at_exit = true
@tc_timers ||= {}
@tc_timers[name] ||= 0
t1 = Time.now
yield
@tc_timers[name] += Time.now - t1
end
require 'set'
def max_rand
Random.rand(0..(2**(0.size * 8 -2) -1))
end
at_exit do
File.delete("sdbm_benchmark.dir")
File.delete("sdbm_benchmark.pag")
File.delete("dbm_benchmark.db")
File.delete("hash_benchmark.dump")
File.delete("hash_benchmark.gz")
File.delete("benchmark.pstore")
end
key_values = []
5_000_000.times do
key_values << [max_rand.to_s, max_rand.to_s]
end
key_values_shuffled = key_values.shuffle
require 'redis'
redis = Redis.new
time_code("redis create") {
key_values.each do |kvs|
key, value = kvs
redis.set key, value
end
}
time_code("redis read") {
key_values_shuffled.each do |kvs|
redis.get kvs[0]
end
}
exit
hash = {}
time_code("hash create") {
key_values.each do |kvs|
key, value = kvs
hash[key] = value
end
File.open("hash_benchmark.dump", 'w') do |f|
f.write Marshal.dump(hash)
f.close
end
}
time_code("hash open") {
hash = Marshal.load(File.read("hash_benchmark.dump"))
}
time_code("hash read") {
key_values_shuffled.each do |kvs|
hash[kvs[0]]
end
}
require 'zlib'
hash = {}
time_code("hash gzip create") {
key_values.each do |kvs|
key, value = kvs
hash[key] = value
end
File.open("hash_benchmark.gz", 'w') do |f|
gz = Zlib::GzipWriter.new(f)
gz.write Marshal.dump(hash)
gz.close
end
}
time_code("hash gzip open") {
Zlib::GzipReader.open("hash_benchmark.gz") do |gz|
hash = Marshal.load(gz.read)
end
}
time_code("hash gzip read") {
key_values_shuffled.each do |kvs|
hash[kvs[0]]
end
}
require 'dbm'
db = DBM.open('dbm_benchmark', 666, DBM::WRCREAT)
time_code("dbm create") {
key_values.each do |kvs|
key, value = kvs
db[key] = value
end
}
time_code("dbm read") {
key_values_shuffled.each do |kvs|
db[kvs[0]]
end
}
require "pstore"
db = PStore.new("benchmark.pstore")
count = 0
time_code("pstore create") {
db.transaction do
key_values.each do |kvs|
key, value = kvs
db[key] = value
end
puts count += 1
end
}
time_code("sdbm read") {
key_values_shuffled.each do |kvs|
db.transaction(true) { db[kvs[0]] }
end
}
require 'sdbm'
SDBM.open 'sdbm_benchmark' do |db|
time_code("sdbm create") {
key_values.each do |kvs|
key, value = kvs
db[key] = value
end
}
time_code("sdbm read") {
key_values_shuffled.each do |kvs|
db[kvs[0]]
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment