File: reader.rb

package info (click to toggle)
ruby-hiredis 0.6.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: ruby: 945; ansic: 503; makefile: 7
file content (76 lines) | stat: -rw-r--r-- 1,555 bytes parent folder | download | duplicates (5)
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
# Compare throughput of pure-Ruby reply reader vs extension
#
# Run with
#
#   $ ruby -Ilib benchmark/reader.rb
#

require "benchmark"
require "hiredis/ruby/reader"

begin
  require "hiredis/ext/reader"
rescue LoadError
end

N = 100_000

def benchmark(b, title, klass, pipeline = 1)
  reader = klass.new

  data = "+OK\r\n"

  GC.start
  b.report("#{title}: Status reply") do
    (N / pipeline).times do
      pipeline.times { reader.feed(data) }
      pipeline.times { reader.gets }
    end
  end

  data = "$10\r\nxxxxxxxxxx\r\n"

  GC.start
  b.report("#{title}: Bulk reply (10 bytes)") do
    (N / pipeline).times do
      pipeline.times { reader.feed(data) }
      pipeline.times { reader.gets }
    end
  end

  data = "*10\r\n"
  10.times { data << "$10\r\nxxxxxxxxxx\r\n" }

  GC.start
  b.report("#{title}: Multi-bulk reply (10x 10 bytes)") do
    (N / pipeline).times do
      pipeline.times { reader.feed(data) }
      pipeline.times { reader.gets }
    end
  end

  data = "*1\r\n#{data}"

  GC.start
  b.report("#{title}: Nested multi-bulk reply (1x 10x 10 bytes)") do
    (N / pipeline).times do
      pipeline.times { reader.feed(data) }
      pipeline.times { reader.gets }
    end
  end
end

pipeline = (ARGV.shift || 1).to_i
puts "\n%d replies in pipelines of %d replies\n" % [N, pipeline]

Benchmark.bm(50) do |b|
  if defined?(Hiredis::Ext::Reader)
    benchmark(b, "Ext", Hiredis::Ext::Reader, pipeline)
    puts
  end

  if defined?(Hiredis::Ruby::Reader)
    benchmark(b, "Ruby", Hiredis::Ruby::Reader, pipeline)
    puts
  end
end