blob: f5fe3235a98b5c462d426564816810114c4cf558 [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'
22require 'thrift'
23require 'thrift/server/nonblockingserver'
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000024require 'thrift/transport/unixsocket'
Kevin Clarkca8a1b32008-06-18 01:17:06 +000025require 'stringio'
Kevin Clarkd3cee022008-06-18 01:19:09 +000026
Kevin Clark77b39b32008-06-18 01:20:16 +000027HOST = '127.0.0.1'
Kevin Clarkca8a1b32008-06-18 01:17:06 +000028PORT = 42587
29
Kevin Clarkca8a1b32008-06-18 01:17:06 +000030###############
31## Server
32###############
33
Kevin Clarkd3cee022008-06-18 01:19:09 +000034class Server
35 attr_accessor :serverclass
36 attr_accessor :interpreter
37 attr_accessor :host
38 attr_accessor :port
Kevin Clarkca8a1b32008-06-18 01:17:06 +000039
Kevin Clarkd3cee022008-06-18 01:19:09 +000040 def initialize(opts)
41 @serverclass = opts.fetch(:class, Thrift::NonblockingServer)
42 @interpreter = opts.fetch(:interpreter, "ruby")
43 @host = opts.fetch(:host, ::HOST)
44 @port = opts.fetch(:port, ::PORT)
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" : "")
50 @pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{@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)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000080 end
81
82 def run
83 @pool = []
84 @benchmark_start = Time.now
85 puts "Spawning benchmark processes..."
86 @num_processes.times do
87 spawn
Kevin Clarkfdc9c972008-06-18 01:19:46 +000088 sleep 0.02 # space out spawns
Kevin Clarkca8a1b32008-06-18 01:17:06 +000089 end
90 collect_output
91 @benchmark_end = Time.now # we know the procs are done here
92 translate_output
93 analyze_output
94 report_output
95 end
96
97 def spawn
Kevin Clarkfdc9c972008-06-18 01:19:46 +000098 pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{"-log-exceptions" if @log_exceptions} #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}")
Kevin Clarkd3cee022008-06-18 01:19:09 +000099 @pool << pipe
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000100 end
101
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000102 def socket_class
103 if @socket
104 Thrift::UNIXSocket
105 else
106 Thrift::Socket
107 end
108 end
109
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000110 def collect_output
111 puts "Collecting output..."
112 # read from @pool until all sockets are closed
113 @buffers = Hash.new { |h,k| h[k] = '' }
114 until @pool.empty?
115 rd, = select(@pool)
116 next if rd.nil?
117 rd.each do |fd|
118 begin
Kevin Clarkfb5c0eb2008-06-18 01:19:04 +0000119 @buffers[fd] << fd.readpartial(4096)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000120 rescue EOFError
121 @pool.delete fd
122 end
123 end
124 end
125 end
126
127 def translate_output
128 puts "Translating output..."
129 @output = []
130 @buffers.each do |fd, buffer|
131 strio = StringIO.new(buffer)
132 logs = []
133 begin
134 loop do
135 logs << Marshal.load(strio)
136 end
137 rescue EOFError
138 @output << logs
139 end
140 end
141 end
142
143 def analyze_output
144 puts "Analyzing output..."
145 call_times = []
146 client_times = []
147 connection_failures = []
Kevin Clarkd2719792008-06-18 01:19:37 +0000148 connection_errors = []
Kevin Clark66038a02008-06-18 01:19:18 +0000149 shortest_call = 0
150 shortest_client = 0
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000151 longest_call = 0
152 longest_client = 0
153 @output.each do |logs|
154 cur_call, cur_client = nil
155 logs.each do |tok, time|
156 case tok
157 when :start
158 cur_client = time
159 when :call_start
160 cur_call = time
161 when :call_end
162 delta = time - cur_call
163 call_times << delta
164 longest_call = delta unless longest_call > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000165 shortest_call = delta if shortest_call == 0 or delta < shortest_call
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000166 cur_call = nil
167 when :end
168 delta = time - cur_client
169 client_times << delta
170 longest_client = delta unless longest_client > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000171 shortest_client = delta if shortest_client == 0 or delta < shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000172 cur_client = nil
173 when :connection_failure
174 connection_failures << time
Kevin Clarkd2719792008-06-18 01:19:37 +0000175 when :connection_error
176 connection_errors << time
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000177 end
178 end
179 end
180 @report = {}
181 @report[:total_calls] = call_times.inject(0.0) { |a,t| a += t }
182 @report[:avg_calls] = @report[:total_calls] / call_times.size
183 @report[:total_clients] = client_times.inject(0.0) { |a,t| a += t }
184 @report[:avg_clients] = @report[:total_clients] / client_times.size
185 @report[:connection_failures] = connection_failures.size
Kevin Clarkd2719792008-06-18 01:19:37 +0000186 @report[:connection_errors] = connection_errors.size
Kevin Clark66038a02008-06-18 01:19:18 +0000187 @report[:shortest_call] = shortest_call
188 @report[:shortest_client] = shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000189 @report[:longest_call] = longest_call
190 @report[:longest_client] = longest_client
191 @report[:total_benchmark_time] = @benchmark_end - @benchmark_start
192 @report[:fastthread] = $".include?('fastthread.bundle')
193 end
194
195 def report_output
196 fmt = "%.4f seconds"
197 puts
198 tabulate "%d",
Kevin Clark75532ee2008-06-18 01:19:14 +0000199 [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],
Kevin Clarkd3cee022008-06-18 01:19:09 +0000200 [["Server interpreter", "%s"], @server.interpreter],
201 [["Client interpreter", "%s"], @interpreter],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000202 [["Socket class", "%s"], socket_class],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000203 ["Number of processes", @num_processes],
204 ["Clients per process", @clients_per_process],
205 ["Calls per client", @calls_per_client],
206 [["Using fastthread", "%s"], @report[:fastthread] ? "yes" : "no"]
207 puts
Kevin Clark75532ee2008-06-18 01:19:14 +0000208 failures = (@report[:connection_failures] > 0)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000209 tabulate fmt,
Kevin Clarkd2719792008-06-18 01:19:37 +0000210 [["Connection failures", "%d", [:red, :bold]], @report[:connection_failures]],
211 [["Connection errors", "%d", [:red, :bold]], @report[:connection_errors]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000212 ["Average time per call", @report[:avg_calls]],
213 ["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]],
214 ["Total time for all calls", @report[:total_calls]],
215 ["Real time for benchmarking", @report[:total_benchmark_time]],
Kevin Clark66038a02008-06-18 01:19:18 +0000216 ["Shortest call time", @report[:shortest_call]],
217 ["Longest call time", @report[:longest_call]],
218 ["Shortest client time (%d calls)" % @calls_per_client, @report[:shortest_client]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000219 ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]
220 end
221
Kevin Clark75532ee2008-06-18 01:19:14 +0000222 ANSI = {
223 :reset => 0,
224 :bold => 1,
225 :black => 30,
226 :red => 31,
227 :green => 32,
228 :yellow => 33,
229 :blue => 34,
230 :magenta => 35,
231 :cyan => 36,
232 :white => 37
233 }
234
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000235 def tabulate(fmt, *labels_and_values)
Bryan Duxbury74c3de62009-03-20 16:54:33 +0000236 labels = labels_and_values.map { |l| Array === l ? l.first : l }
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000237 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
238 labels_and_values.each do |(l,v)|
239 f = fmt
Kevin Clark75532ee2008-06-18 01:19:14 +0000240 l, f, c = l if Array === l
241 fmtstr = "%-#{label_width+1}s #{f}"
Kevin Clarkd2719792008-06-18 01:19:37 +0000242 if STDOUT.tty? and c and v.to_i > 0
Kevin Clark75532ee2008-06-18 01:19:14 +0000243 fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m"
244 end
245 puts fmtstr % [l+":", v]
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000246 end
247 end
248end
249
250def resolve_const(const)
251 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
252end
253
254puts "Starting server..."
Kevin Clarkd3cee022008-06-18 01:19:09 +0000255args = {}
256args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
257args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
Kevin Clarkd8d0d602008-06-18 01:20:10 +0000258args[:host] = ENV['THRIFT_HOST'] || HOST
259args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
Kevin Clarkd3cee022008-06-18 01:19:09 +0000260server = Server.new(args)
261server.start
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000262
Kevin Clarkd8d0d602008-06-18 01:20:10 +0000263args = {}
264args[:host] = ENV['THRIFT_HOST'] || HOST
265args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
Kevin Clark4b429ad2008-06-18 01:20:06 +0000266args[:num_processes] = (ENV['THRIFT_NUM_PROCESSES'] || 40).to_i
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000267args[:clients_per_process] = (ENV['THRIFT_NUM_CLIENTS'] || 5).to_i
268args[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_i
Kevin Clarkd3cee022008-06-18 01:19:09 +0000269args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000270args[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS']
Kevin Clarkd3cee022008-06-18 01:19:09 +0000271BenchmarkManager.new(args, server).run
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000272
Kevin Clarkd3cee022008-06-18 01:19:09 +0000273server.shutdown