blob: 4a520a5923a88d8830bb1531b82ca46200a9eb7f [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
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
17# under the License.
18#
19
Kevin Clarkca8a1b32008-06-18 01:17:06 +000020require 'rubygems'
21$:.unshift File.dirname(__FILE__) + '/../lib'
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050022$:.unshift File.dirname(__FILE__) + '/../ext'
Kevin Clarkca8a1b32008-06-18 01:17:06 +000023require 'thrift'
Kevin Clarkca8a1b32008-06-18 01:17:06 +000024require 'stringio'
Kevin Clarkd3cee022008-06-18 01:19:09 +000025
Kevin Clark77b39b32008-06-18 01:20:16 +000026HOST = '127.0.0.1'
Kevin Clarkca8a1b32008-06-18 01:17:06 +000027PORT = 42587
28
Kevin Clarkca8a1b32008-06-18 01:17:06 +000029###############
30## Server
31###############
32
Kevin Clarkd3cee022008-06-18 01:19:09 +000033class Server
34 attr_accessor :serverclass
35 attr_accessor :interpreter
36 attr_accessor :host
37 attr_accessor :port
Kevin Clarkca8a1b32008-06-18 01:17:06 +000038
Kevin Clarkd3cee022008-06-18 01:19:09 +000039 def initialize(opts)
40 @serverclass = opts.fetch(:class, Thrift::NonblockingServer)
41 @interpreter = opts.fetch(:interpreter, "ruby")
42 @host = opts.fetch(:host, ::HOST)
43 @port = opts.fetch(:port, ::PORT)
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050044 @tls = opts.fetch(:tls, false)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000045 end
46
Kevin Clarkd3cee022008-06-18 01:19:09 +000047 def start
Kevin Clark75532ee2008-06-18 01:19:14 +000048 return if @serverclass == Object
Kevin Clark66038a02008-06-18 01:19:18 +000049 args = (File.basename(@interpreter) == "jruby" ? "-J-server" : "")
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050050 @pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{"-tls" if @tls} #{@host} #{@port} #{@serverclass.name}", "r+")
Kevin Clark1a95a1d2008-06-18 01:19:28 +000051 Marshal.load(@pipe) # wait until the server has started
Kevin Clarkfdc9c972008-06-18 01:19:46 +000052 sleep 0.4 # give the server time to actually start spawning sockets
Kevin Clarkca8a1b32008-06-18 01:17:06 +000053 end
54
Kevin Clarkd3cee022008-06-18 01:19:09 +000055 def shutdown
56 return unless @pipe
57 Marshal.dump(:shutdown, @pipe)
58 begin
59 @pipe.read(10) # block until the server shuts down
60 rescue EOFError
Kevin Clarkca8a1b32008-06-18 01:17:06 +000061 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000062 @pipe.close
63 @pipe = nil
Kevin Clarkca8a1b32008-06-18 01:17:06 +000064 end
65end
66
Kevin Clarkca8a1b32008-06-18 01:17:06 +000067class BenchmarkManager
Kevin Clarkd3cee022008-06-18 01:19:09 +000068 def initialize(opts, server)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000069 @socket = opts.fetch(:socket) do
70 @host = opts.fetch(:host, 'localhost')
71 @port = opts.fetch(:port)
72 nil
73 end
Kevin Clarkca8a1b32008-06-18 01:17:06 +000074 @num_processes = opts.fetch(:num_processes, 40)
75 @clients_per_process = opts.fetch(:clients_per_process, 10)
76 @calls_per_client = opts.fetch(:calls_per_client, 50)
Kevin Clarkd3cee022008-06-18 01:19:09 +000077 @interpreter = opts.fetch(:interpreter, "ruby")
78 @server = server
Kevin Clarkfdc9c972008-06-18 01:19:46 +000079 @log_exceptions = opts.fetch(:log_exceptions, false)
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050080 @tls = opts.fetch(:tls, false)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000081 end
82
83 def run
84 @pool = []
85 @benchmark_start = Time.now
86 puts "Spawning benchmark processes..."
87 @num_processes.times do
88 spawn
Kevin Clarkfdc9c972008-06-18 01:19:46 +000089 sleep 0.02 # space out spawns
Kevin Clarkca8a1b32008-06-18 01:17:06 +000090 end
91 collect_output
92 @benchmark_end = Time.now # we know the procs are done here
93 translate_output
94 analyze_output
95 report_output
96 end
97
98 def spawn
Dmytro Shteflyuk09475942025-11-19 16:23:42 -050099 pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{"-log-exceptions" if @log_exceptions} #{"-tls" if @tls} #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}")
Kevin Clarkd3cee022008-06-18 01:19:09 +0000100 @pool << pipe
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000101 end
102
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000103 def socket_class
104 if @socket
105 Thrift::UNIXSocket
Dmytro Shteflyuk09475942025-11-19 16:23:42 -0500106 elsif @tls
107 Thrift::SSLSocket
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000108 else
109 Thrift::Socket
110 end
111 end
112
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000113 def collect_output
114 puts "Collecting output..."
115 # read from @pool until all sockets are closed
116 @buffers = Hash.new { |h,k| h[k] = '' }
117 until @pool.empty?
118 rd, = select(@pool)
119 next if rd.nil?
120 rd.each do |fd|
121 begin
Kevin Clarkfb5c0eb2008-06-18 01:19:04 +0000122 @buffers[fd] << fd.readpartial(4096)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000123 rescue EOFError
124 @pool.delete fd
125 end
126 end
127 end
128 end
129
130 def translate_output
131 puts "Translating output..."
132 @output = []
133 @buffers.each do |fd, buffer|
134 strio = StringIO.new(buffer)
135 logs = []
136 begin
137 loop do
138 logs << Marshal.load(strio)
139 end
140 rescue EOFError
141 @output << logs
142 end
143 end
144 end
145
146 def analyze_output
147 puts "Analyzing output..."
148 call_times = []
149 client_times = []
150 connection_failures = []
Kevin Clarkd2719792008-06-18 01:19:37 +0000151 connection_errors = []
Kevin Clark66038a02008-06-18 01:19:18 +0000152 shortest_call = 0
153 shortest_client = 0
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000154 longest_call = 0
155 longest_client = 0
156 @output.each do |logs|
157 cur_call, cur_client = nil
158 logs.each do |tok, time|
159 case tok
160 when :start
161 cur_client = time
162 when :call_start
163 cur_call = time
164 when :call_end
165 delta = time - cur_call
166 call_times << delta
167 longest_call = delta unless longest_call > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000168 shortest_call = delta if shortest_call == 0 or delta < shortest_call
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000169 cur_call = nil
170 when :end
171 delta = time - cur_client
172 client_times << delta
173 longest_client = delta unless longest_client > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000174 shortest_client = delta if shortest_client == 0 or delta < shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000175 cur_client = nil
176 when :connection_failure
177 connection_failures << time
Kevin Clarkd2719792008-06-18 01:19:37 +0000178 when :connection_error
179 connection_errors << time
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000180 end
181 end
182 end
183 @report = {}
184 @report[:total_calls] = call_times.inject(0.0) { |a,t| a += t }
185 @report[:avg_calls] = @report[:total_calls] / call_times.size
186 @report[:total_clients] = client_times.inject(0.0) { |a,t| a += t }
187 @report[:avg_clients] = @report[:total_clients] / client_times.size
188 @report[:connection_failures] = connection_failures.size
Kevin Clarkd2719792008-06-18 01:19:37 +0000189 @report[:connection_errors] = connection_errors.size
Kevin Clark66038a02008-06-18 01:19:18 +0000190 @report[:shortest_call] = shortest_call
191 @report[:shortest_client] = shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000192 @report[:longest_call] = longest_call
193 @report[:longest_client] = longest_client
194 @report[:total_benchmark_time] = @benchmark_end - @benchmark_start
195 @report[:fastthread] = $".include?('fastthread.bundle')
196 end
197
198 def report_output
199 fmt = "%.4f seconds"
200 puts
201 tabulate "%d",
Kevin Clark75532ee2008-06-18 01:19:14 +0000202 [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],
Kevin Clarkd3cee022008-06-18 01:19:09 +0000203 [["Server interpreter", "%s"], @server.interpreter],
204 [["Client interpreter", "%s"], @interpreter],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000205 [["Socket class", "%s"], socket_class],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000206 ["Number of processes", @num_processes],
207 ["Clients per process", @clients_per_process],
208 ["Calls per client", @calls_per_client],
209 [["Using fastthread", "%s"], @report[:fastthread] ? "yes" : "no"]
210 puts
Kevin Clark75532ee2008-06-18 01:19:14 +0000211 failures = (@report[:connection_failures] > 0)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000212 tabulate fmt,
Kevin Clarkd2719792008-06-18 01:19:37 +0000213 [["Connection failures", "%d", [:red, :bold]], @report[:connection_failures]],
214 [["Connection errors", "%d", [:red, :bold]], @report[:connection_errors]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000215 ["Average time per call", @report[:avg_calls]],
216 ["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]],
217 ["Total time for all calls", @report[:total_calls]],
218 ["Real time for benchmarking", @report[:total_benchmark_time]],
Kevin Clark66038a02008-06-18 01:19:18 +0000219 ["Shortest call time", @report[:shortest_call]],
220 ["Longest call time", @report[:longest_call]],
221 ["Shortest client time (%d calls)" % @calls_per_client, @report[:shortest_client]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000222 ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]
223 end
224
Kevin Clark75532ee2008-06-18 01:19:14 +0000225 ANSI = {
226 :reset => 0,
227 :bold => 1,
228 :black => 30,
229 :red => 31,
230 :green => 32,
231 :yellow => 33,
232 :blue => 34,
233 :magenta => 35,
234 :cyan => 36,
235 :white => 37
236 }
237
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000238 def tabulate(fmt, *labels_and_values)
Bryan Duxbury74c3de62009-03-20 16:54:33 +0000239 labels = labels_and_values.map { |l| Array === l ? l.first : l }
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000240 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
241 labels_and_values.each do |(l,v)|
242 f = fmt
Kevin Clark75532ee2008-06-18 01:19:14 +0000243 l, f, c = l if Array === l
244 fmtstr = "%-#{label_width+1}s #{f}"
Kevin Clarkd2719792008-06-18 01:19:37 +0000245 if STDOUT.tty? and c and v.to_i > 0
Kevin Clark75532ee2008-06-18 01:19:14 +0000246 fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m"
247 end
248 puts fmtstr % [l+":", v]
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000249 end
250 end
251end
252
253def resolve_const(const)
254 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
255end
256
257puts "Starting server..."
Kevin Clarkd3cee022008-06-18 01:19:09 +0000258args = {}
259args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
260args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
Kevin Clarkd8d0d602008-06-18 01:20:10 +0000261args[:host] = ENV['THRIFT_HOST'] || HOST
262args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
Dmytro Shteflyuk09475942025-11-19 16:23:42 -0500263args[:tls] = ENV['THRIFT_TLS'] == 'true'
Kevin Clarkd3cee022008-06-18 01:19:09 +0000264server = Server.new(args)
265server.start
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000266
Kevin Clarkd8d0d602008-06-18 01:20:10 +0000267args = {}
268args[:host] = ENV['THRIFT_HOST'] || HOST
269args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
Dmytro Shteflyuk09475942025-11-19 16:23:42 -0500270args[:tls] = ENV['THRIFT_TLS'] == 'true'
Kevin Clark4b429ad2008-06-18 01:20:06 +0000271args[:num_processes] = (ENV['THRIFT_NUM_PROCESSES'] || 40).to_i
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000272args[:clients_per_process] = (ENV['THRIFT_NUM_CLIENTS'] || 5).to_i
273args[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_i
Kevin Clarkd3cee022008-06-18 01:19:09 +0000274args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000275args[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS']
Kevin Clarkd3cee022008-06-18 01:19:09 +0000276BenchmarkManager.new(args, server).run
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000277
Kevin Clarkd3cee022008-06-18 01:19:09 +0000278server.shutdown