/
benchmark-notify.rb
143 lines (125 loc) · 4.21 KB
/
benchmark-notify.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# this benchmark must be done by benchmark-notify.sh.
require "benchmark"
require "fileutils"
require "optparse"
require "csv"
require "json"
require "droonga/client"
require File.expand_path(File.join(__FILE__, "..", "..", "utils.rb"))
class NotifyBenchmark
attr_reader :n_subscribers
WATCHING_KEYWORD = "a"
def initialize(params)
@params = params || {}
@n_times = params[:n_times] || 0
@timeout = params[:timeout] || 0
@n_subscribers = 0
@client = Droonga::Client.new(tag: "droonga", port: 23003)
@receiver = DroongaBenchmark::MessageReceiver.new
@route = "#{@receiver.host}:#{@receiver.port}/droonga"
setup
end
def setup
add_subscribers(@params[:n_initial_subscribers])
end
def run
@n_times.times do |index|
do_feed("#{WATCHING_KEYWORD} #{index}")
end
published_messages = []
while published_messages.size != @n_times
published_messages << @receiver.new_message
end
published_messages
end
def add_subscribers(n_subscribers)
n_subscribers.times do |index|
message = DroongaBenchmark::MessageCreator.envelope_to_subscribe(WATCHING_KEYWORD)
message["body"]["subscriber"] += " #{@n_subscribers + index}"
message["body"]["route"] = @route
@client.connection.send(message, :response => :one)
end
@n_subscribers += n_subscribers
end
def do_feed(target)
message = DroongaBenchmark::MessageCreator.envelope_to_feed(target)
@client.connection.send(message, :response => :none)
end
end
options = {
:n_subscribers => 1000,
:n_times => 1000,
:n_steps => 10,
:output_path => "/tmp/watch-benchmark-notify.csv",
}
option_parser = OptionParser.new do |parser|
parser.on("--subscribers=N", Integer,
"initial number of subscribers") do |n_subscribers|
options[:n_subscribers] = n_subscribers
end
parser.on("--times=N", Integer,
"number of publish times") do |n_times|
options[:n_times] = n_times
end
parser.on("--steps=N", Integer,
"number of benchmark steps") do |n_steps|
options[:n_steps] = n_steps
end
parser.on("--timeout=N", Float,
"timeout for receiving") do |timeout|
options[:timeout] = timeout
end
parser.on("--output-path=PATH", String,
"path to the output CSV file") do |output_path|
options[:output_path] = output_path
end
end
args = option_parser.parse!(ARGV)
notify_benchmark = NotifyBenchmark.new(:n_initial_subscribers => options[:n_subscribers],
:n_times => options[:n_times],
:timeout => options[:timeout])
results = []
options[:n_steps].times do |try_count|
notify_benchmark.add_subscribers(notify_benchmark.n_subscribers) if try_count > 0
label = "#{notify_benchmark.n_subscribers} subscribers"
percentage = nil
result = Benchmark.bm do |benchmark|
benchmark.report(label) do
published_messages = notify_benchmark.run
percentage = published_messages.size.to_f / options[:n_times] * 100
end
end
puts "=> #{percentage} % feeds are notified"
result = result.join("").strip.gsub(/[()]/, "").split(/\s+/)
qps = options[:n_times].to_f / result.last.to_f
puts " (#{qps} queries per second)"
results << [label, qps]
end
total_results = [
["case", "qps"],
]
total_results += results
puts ""
puts "Results (saved to #{options[:output_path]}):"
File.open(options[:output_path], "w") do |file|
total_results.each do |row|
file.puts(CSV.generate_line(row))
puts row.join(",")
end
end