]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rb/benchmark/benchmark.rb
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
21 $
:.unshift File
.dirname(__FILE__
) + '/../lib'
33 attr_accessor
:serverclass
34 attr_accessor
:interpreter
39 @serverclass = opts
.fetch(:class, Thrift
::NonblockingServer)
40 @interpreter = opts
.fetch(:interpreter, "ruby")
41 @host = opts
.fetch(:host, ::HOST)
42 @port = opts
.fetch(:port, ::PORT)
46 return if @serverclass == Object
47 args
= (File
.basename(@interpreter) == "jruby" ? "-J-server" : "")
48 @pipe = IO
.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{@host} #{@port} #{@serverclass.name}", "r+")
49 Marshal
.load(@pipe) # wait until the server has started
50 sleep
0.4 # give the server time to actually start spawning sockets
55 Marshal
.dump(:shutdown, @pipe)
57 @pipe.read(10) # block until the server shuts down
65 class BenchmarkManager
66 def initialize(opts
, server
)
67 @socket = opts
.fetch(:socket) do
68 @host = opts
.fetch(:host, 'localhost')
69 @port = opts
.fetch(:port)
72 @num_processes = opts
.fetch(:num_processes, 40)
73 @clients_per_process = opts
.fetch(:clients_per_process, 10)
74 @calls_per_client = opts
.fetch(:calls_per_client, 50)
75 @interpreter = opts
.fetch(:interpreter, "ruby")
77 @log_exceptions = opts
.fetch(:log_exceptions, false)
82 @benchmark_start = Time
.now
83 puts
"Spawning benchmark processes..."
84 @num_processes.times
do
86 sleep
0.02 # space out spawns
89 @benchmark_end = Time
.now
# we know the procs are done here
96 pipe
= IO
.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{"-log-exceptions
" if @log_exceptions} #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}")
109 puts
"Collecting output..."
110 # read from @pool until all sockets are closed
111 @buffers = Hash
.new
{ |h
,k
| h
[k
] = '' }
117 @buffers[fd
] << fd
.readpartial(4096)
126 puts
"Translating output..."
128 @buffers.each
do |fd
, buffer
|
129 strio
= StringIO
.new(buffer
)
133 logs
<< Marshal
.load(strio
)
142 puts
"Analyzing output..."
145 connection_failures
= []
146 connection_errors
= []
151 @output.each
do |logs
|
152 cur_call
, cur_client
= nil
153 logs
.each
do |tok
, time
|
160 delta
= time
- cur_call
162 longest_call
= delta
unless longest_call
> delta
163 shortest_call
= delta
if shortest_call
== 0 or delta
< shortest_call
166 delta
= time
- cur_client
167 client_times
<< delta
168 longest_client
= delta
unless longest_client
> delta
169 shortest_client
= delta
if shortest_client
== 0 or delta
< shortest_client
171 when :connection_failure
172 connection_failures
<< time
173 when :connection_error
174 connection_errors
<< time
179 @report[:total_calls] = call_times
.inject(0.0) { |a
,t
| a
+= t
}
180 @report[:avg_calls] = @report[:total_calls] / call_times
.size
181 @report[:total_clients] = client_times
.inject(0.0) { |a
,t
| a
+= t
}
182 @report[:avg_clients] = @report[:total_clients] / client_times
.size
183 @report[:connection_failures] = connection_failures
.size
184 @report[:connection_errors] = connection_errors
.size
185 @report[:shortest_call] = shortest_call
186 @report[:shortest_client] = shortest_client
187 @report[:longest_call] = longest_call
188 @report[:longest_client] = longest_client
189 @report[:total_benchmark_time] = @benchmark_end - @benchmark_start
190 @report[:fastthread] = $
".include?('fastthread.bundle')
197 [["Server
class", "%s
"], @server.serverclass == Object ? "" : @server.serverclass],
198 [["Server interpreter
", "%s
"], @server.interpreter],
199 [["Client interpreter
", "%s
"], @interpreter],
200 [["Socket
class", "%s
"], socket_class],
201 ["Number of processes
", @num_processes],
202 ["Clients per process
", @clients_per_process],
203 ["Calls per client
", @calls_per_client],
204 [["Using fastthread
", "%s
"], @report[:fastthread] ? "yes
" : "no
"]
206 failures = (@report[:connection_failures] > 0)
208 [["Connection failures
", "%d
", [:red, :bold]], @report[:connection_failures]],
209 [["Connection errors
", "%d
", [:red, :bold]], @report[:connection_errors]],
210 ["Average time per call
", @report[:avg_calls]],
211 ["Average time per
client (%d calls
)" % @calls_per_client, @report[:avg_clients]],
212 ["Total time
for all calls
", @report[:total_calls]],
213 ["Real time
for benchmarking
", @report[:total_benchmark_time]],
214 ["Shortest call time
", @report[:shortest_call]],
215 ["Longest call time
", @report[:longest_call]],
216 ["Shortest client
time (%d calls
)" % @calls_per_client, @report[:shortest_client]],
217 ["Longest client
time (%d calls
)" % @calls_per_client, @report[:longest_client]]
233 def tabulate(fmt, *labels_and_values)
234 labels = labels_and_values.map { |l| Array === l ? l.first : l }
235 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
236 labels_and_values.each do |(l,v)|
238 l, f, c = l if Array === l
239 fmtstr = "%-#{label_width+1}s #{f}"
240 if STDOUT.tty
? and c
and v
.to_i
> 0
241 fmtstr
= "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr
+ "\e[#{ANSI[:reset]}m"
243 puts fmtstr
% [l
+":", v
]
248 def resolve_const(const
)
249 const
and const
.split('::').inject(Object
) { |k
,c
| k
.const_get(c
) }
252 puts
"Starting server..."
254 args
[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
255 args
[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift
::NonblockingServer
256 args
[:host] = ENV['THRIFT_HOST'] || HOST
257 args
[:port] = (ENV['THRIFT_PORT'] || PORT
).to_i
258 server
= Server
.new(args
)
262 args
[:host] = ENV['THRIFT_HOST'] || HOST
263 args
[:port] = (ENV['THRIFT_PORT'] || PORT
).to_i
264 args
[:num_processes] = (ENV['THRIFT_NUM_PROCESSES'] || 40).to_i
265 args
[:clients_per_process] = (ENV['THRIFT_NUM_CLIENTS'] || 5).to_i
266 args
[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_i
267 args
[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
268 args
[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS']
269 BenchmarkManager
.new(args
, server
).run