diff --git a/Makefile b/Makefile index d780bde..ccc1d41 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,6 @@ test: $(CRYSTAL_BIN) spec benchmark: - $$(mkdir tmp -p) + $$(mkdir -p tmp) $(CRYSTAL_BIN) build --release -o tmp/benchmark ./benchmark.cr $(CRFLAGS) tmp/benchmark diff --git a/benchmark.cr b/benchmark.cr index 8fb4df5..34bda09 100644 --- a/benchmark.cr +++ b/benchmark.cr @@ -3,17 +3,25 @@ require "./src/kiwi/file_store" require "./src/kiwi/redis_store" require "./src/kiwi/leveldb_store" require "./src/kiwi/memcached_store" - -N = 100_000 - -def benchmark(name) - start_time = Time.now - yield - writing_time = Time.now - start_time - speed = (N.to_f / writing_time.to_f) - rounded_speed = ((speed / 1000).round * 1000).to_i - puts " #{name}: #{rounded_speed} ops/sec" - rounded_speed +require "benchmark" +X = 50000 +N = 5 +LIMITER = Time::Span.new(seconds: 5) + +def benchmark(name, &block) + r = Time::Span.new + i = 0 + loop do + puts "N=%i / %i" % [i, N] + r += Benchmark.realtime do + X.times do + block.call + end + end + i += X + break if r > LIMITER + end + (i.to_f/r.seconds.to_f).round.to_i end def gen_data @@ -113,9 +121,10 @@ puts "Initializing stores..." stores = Array(Kiwi::Store).new stores << Kiwi::MemoryStore.new +stores << Kiwi::MemcachedStore.new(Memcached::Client.new) + stores << Kiwi::LevelDBStore.new(LevelDB::DB.new("/tmp/kiwi_benchmark_leveldb")) stores << Kiwi::RedisStore.new(Redis.new) -stores << Kiwi::MemcachedStore.new(Memcached::Client.new) stores << Kiwi::FileStore.new("/tmp/kiwi_benchmark_file") result = measure(stores) diff --git a/src/kiwi/file_store.cr b/src/kiwi/file_store.cr index 123da1e..da789a4 100644 --- a/src/kiwi/file_store.cr +++ b/src/kiwi/file_store.cr @@ -9,12 +9,12 @@ module Kiwi create_dir end - def get(key) + def get(key) : String | Nil file = file_for_key(key) File.exists?(file) ? File.read(file) : nil end - def set(key, val) + def set(key, val) : String create_dir unless dir_created? file = file_for_key(key) @@ -22,7 +22,7 @@ module Kiwi val end - def delete(key) + def delete(key) : String create_dir unless dir_created? file = file_for_key(key) @@ -31,11 +31,11 @@ module Kiwi File.delete(file) value else - nil + "" end end - def clear + def clear : Store remove_dir self end diff --git a/src/kiwi/leveldb_store.cr b/src/kiwi/leveldb_store.cr index 501d1c1..fdfc190 100644 --- a/src/kiwi/leveldb_store.cr +++ b/src/kiwi/leveldb_store.cr @@ -6,22 +6,22 @@ module Kiwi def initialize(@leveldb : ::LevelDB::DB) end - def set(key, val) + def set(key, val) : String @leveldb.put(key, val) val end - def get(key) + def get(key) : String | Nil @leveldb.get(key) end - def delete(key) + def delete(key) : String val = get(key) @leveldb.delete(key) - val + val || "" end - def clear + def clear : Store @leveldb.clear self end diff --git a/src/kiwi/memcached_store.cr b/src/kiwi/memcached_store.cr index 7bb50d1..359a4d8 100644 --- a/src/kiwi/memcached_store.cr +++ b/src/kiwi/memcached_store.cr @@ -6,22 +6,22 @@ module Kiwi def initialize(@memcached : Memcached::Client) end - def set(key, val) + def set(key, val) : String @memcached.set(key, val) val end - def get(key) + def get(key) : String | Nil @memcached.get(key) end - def delete(key) + def delete(key) : String val = get(key) @memcached.delete(key) - val + val || "" end - def clear + def clear : Store @memcached.flush self end diff --git a/src/kiwi/memory_store.cr b/src/kiwi/memory_store.cr index 9e97a06..4dca65f 100644 --- a/src/kiwi/memory_store.cr +++ b/src/kiwi/memory_store.cr @@ -6,19 +6,19 @@ module Kiwi @mem = Hash(String, String).new end - def get(key) + def get(key) : String | Nil @mem[key]? end - def set(key, val) + def set(key, val) : String @mem[key] = val end - def delete(key) - @mem.delete(key) + def delete(key) : String + @mem.delete(key) || "" end - def clear + def clear : Store @mem.clear self end diff --git a/src/kiwi/redis_store.cr b/src/kiwi/redis_store.cr index 78d359e..e74f1ba 100644 --- a/src/kiwi/redis_store.cr +++ b/src/kiwi/redis_store.cr @@ -6,22 +6,22 @@ module Kiwi def initialize(@redis : Redis) end - def set(key, val) + def set(key, val) : String @redis.set(key, val) val end - def get(key) + def get(key) : String | Nil @redis.get(key) end - def delete(key) + def delete(key) : String val = get(key) @redis.del(key) - val + val || "" end - def clear + def clear : Store @redis.flushdb self end