Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes various incompatabilities with current versions #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 21 additions & 12 deletions benchmark.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/kiwi/file_store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ 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)
File.write(file, val)
val
end

def delete(key)
def delete(key) : String
create_dir unless dir_created?

file = file_for_key(key)
Expand All @@ -31,11 +31,11 @@ module Kiwi
File.delete(file)
value
else
nil
""
end
end

def clear
def clear : Store
remove_dir
self
end
Expand Down
10 changes: 5 additions & 5 deletions src/kiwi/leveldb_store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/kiwi/memcached_store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/kiwi/memory_store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/kiwi/redis_store.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down