]> git.saurik.com Git - redis.git/blame - client-libraries/ruby/benchmarking/worker.rb
client libraries synched in git
[redis.git] / client-libraries / ruby / benchmarking / worker.rb
CommitLineData
d7fc9edb 1BENCHMARK_ROOT = File.dirname(__FILE__)
2REDIS_ROOT = File.join(BENCHMARK_ROOT, "..", "lib")
3
4$: << REDIS_ROOT
5require 'redis'
6require 'benchmark'
7
8def show_usage
9 puts <<-EOL
10 Usage: worker.rb [read:write] <start_index> <end_index> <sleep_msec>
11 EOL
12end
13
14def shift_from_argv
15 value = ARGV.shift
16 unless value
17 show_usage
18 exit -1
19 end
20 value
21end
22
23operation = shift_from_argv.to_sym
24start_index = shift_from_argv.to_i
25end_index = shift_from_argv.to_i
26sleep_msec = shift_from_argv.to_i
27sleep_duration = sleep_msec/1000.0
28
29redis = Redis.new
30
31case 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}"
70end
71