-
Notifications
You must be signed in to change notification settings - Fork 8
/
benchmark.cr
124 lines (103 loc) · 3.12 KB
/
benchmark.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require "./src/kiwi/memory_store"
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
end
def gen_data
data = Hash(String, String).new
N.times do |i|
# from 5 up to ~100 chars
key = "0123456789" * rand(10) + "-key#{i}"
# up to ~ 1Kb
value = "0123456789" * rand(100)
data[key] = value
end
data
end
def measure(stores)
puts "Genrating data..."
data = gen_data
empty_keys = (0..N).map { |i| "empty-key-#{i}" }
result = Hash(String, Hash(String, Int32)).new
puts "Starting...\n\n"
stores.each do |store|
store.clear
puts store.class
store_metrics = Hash(String, Int32).new
store_metrics["set"] = benchmark("set") do
data.each do |key, val|
store.set(key, val)
end
end
store_metrics["get"] = benchmark("get") do
data.each do |key, val|
store.get(key)
end
end
store_metrics["get_empty"] = benchmark("get (empty)") do
data.each do |key, val|
store.get(key)
end
end
store_metrics["delete"] = benchmark("delete") do
data.each do |key, val|
store.delete(key)
end
end
store.clear
store_name = store.class.to_s.split("::").last
result["**#{store_name}**"] = store_metrics
end
result
end
def print_table(result)
store_col_size = result.keys.map(&.size).max
set_col_size = result.values.map { |metrics| metrics["set"].to_s.size }.max
get_col_size = result.values.map { |metrics| metrics["get"].to_s.size }.max
get_empty_col_size = "get(empty)".size
delete_col_size = result.values.map { |metrics| metrics["delete"].to_s.size }.max
# header
puts "| " + " " * store_col_size +
" | " + "set".ljust(set_col_size) +
" | " + "get".ljust(get_col_size) +
" | " + "get(empty)".ljust(get_empty_col_size) +
" | " + "delete".ljust(delete_col_size) +
" |"
puts "| " + "-" * store_col_size +
" | " + "-" * set_col_size +
" | " + "-" * get_col_size +
" | " + "-" * get_empty_col_size +
" | " + "-" * delete_col_size +
" |"
# rows
result.each do |store, metrics|
puts "| " + store.ljust(store_col_size) +
" | " + metrics["set"].to_s.rjust(set_col_size) +
" | " + metrics["get"].to_s.rjust(get_col_size) +
" | " + metrics["get_empty"].to_s.rjust(get_empty_col_size) +
" | " + metrics["delete"].to_s.rjust(delete_col_size) +
" |"
end
end
puts
puts "Initializing stores..."
stores = Array(Kiwi::Store).new
stores << Kiwi::MemoryStore.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)
puts "\n"
print_table(result)
puts "\n"