]>
Commit | Line | Data |
---|---|---|
d7fc9edb | 1 | BENCHMARK_ROOT = File.dirname(__FILE__) |
2 | REDIS_ROOT = File.join(BENCHMARK_ROOT, "..", "lib") | |
3 | ||
4 | $: << REDIS_ROOT | |
5 | require 'redis' | |
6 | require 'benchmark' | |
7 | ||
8 | def show_usage | |
9 | puts <<-EOL | |
10 | Usage: worker.rb [read:write] <start_index> <end_index> <sleep_msec> | |
11 | EOL | |
12 | end | |
13 | ||
14 | def shift_from_argv | |
15 | value = ARGV.shift | |
16 | unless value | |
17 | show_usage | |
18 | exit -1 | |
19 | end | |
20 | value | |
21 | end | |
22 | ||
23 | operation = shift_from_argv.to_sym | |
24 | start_index = shift_from_argv.to_i | |
25 | end_index = shift_from_argv.to_i | |
26 | sleep_msec = shift_from_argv.to_i | |
27 | sleep_duration = sleep_msec/1000.0 | |
28 | ||
29 | redis = Redis.new | |
30 | ||
31 | case operation | |
32 | when :initialize | |
33 | ||
34 | start_index.upto(end_index) do |i| | |
35 | redis[i] = 0 | |
36 | end | |
37 | ||
38 | when :clear | |
39 | ||
40 | start_index.upto(end_index) do |i| | |
41 | redis.delete(i) | |
42 | end | |
43 | ||
44 | when :read, :write | |
45 | ||
46 | puts "Starting to #{operation} at segment #{end_index + 1}" | |
47 | ||
48 | loop do | |
49 | t1 = Time.now | |
50 | start_index.upto(end_index) do |i| | |
51 | case operation | |
52 | when :read | |
53 | redis.get(i) | |
54 | when :write | |
55 | redis.incr(i) | |
56 | else | |
57 | raise "Unknown operation: #{operation}" | |
58 | end | |
59 | sleep sleep_duration | |
60 | end | |
61 | t2 = Time.now | |
62 | ||
63 | requests_processed = end_index - start_index | |
64 | time = t2 - t1 | |
65 | puts "#{t2.strftime("%H:%M")} [segment #{end_index + 1}] : Processed #{requests_processed} requests in #{time} seconds - #{(requests_processed/time).round} requests/sec" | |
66 | end | |
67 | ||
68 | else | |
69 | raise "Unknown operation: #{operation}" | |
70 | end | |
71 |