-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.rb
73 lines (56 loc) · 1.66 KB
/
redis.rb
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
require 'benchmark'
require 'redis'
require './ctt'
puts "Redis benchmark"
puts "Building data cache..."
postal_codes = CTT::load_postal_codes
postal_codes.collect! { |postal_code| postal_code.to_hash }
def create_connection
return Redis.new :host => "10.48.237.224", :port => 6379
end
conn = create_connection
conn.select 'nosql_perf'
insert_benchmark = -> do
thread_count = 2
threads = []
page_size = postal_codes.length / thread_count
(0..thread_count).each do |i|
threads[i] = Thread.new do
tconn = create_connection
conn.select 'nosql_perf'
lower_limit = i * page_size
upper_limit = i * page_size + page_size
upper_limit = postal_codes.length - 1 if upper_limit >= postal_codes.length - 1
key = i * page_size + 1
postal_codes[lower_limit..upper_limit].each do |postal_code|
tconn.set key = postal_code
key += 1
end
end
end
end
query_benchmark = -> do
thread_count = 2
threads = []
page_size = postal_codes.length / thread_count
(0..thread_count).each do |i|
threads[i] = Thread.new do
tconn = create_connection
conn.select 'nosql_perf'
lower_limit = i * page_size
upper_limit = i * page_size + page_size
upper_limit = postal_codes.length - 1 if upper_limit >= postal_codes.length - 1
(lower_limit..upper_limit).each do |key|
postal_code = tconn.get key
end
end
end
threads.each { |thread| thread.join }
end
puts "Benchmarking"
Benchmark.bm 7 do |b|
(1...10).each do |run|
b.report("i: #{run}") { insert_benchmark.call }
b.report("q: #{run}") { query_benchmark.call }
end
end