blob: 3dc67dd8ce1856f6e2128ddd92a23a1dbe4a7d65 [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'
Kevin Clarkca8a1b32008-06-18 01:17:06 +000023require 'stringio'
Kevin Clarkd3cee022008-06-18 01:19:09 +000024
Kevin Clark77b39b32008-06-18 01:20:16 +000025HOST = '127.0.0.1'
Kevin Clarkca8a1b32008-06-18 01:17:06 +000026PORT = 42587
27
Kevin Clarkca8a1b32008-06-18 01:17:06 +000028###############
29## Server
30###############
31
Kevin Clarkd3cee022008-06-18 01:19:09 +000032class Server
33 attr_accessor :serverclass
34 attr_accessor :interpreter
35 attr_accessor :host
36 attr_accessor :port
Kevin Clarkca8a1b32008-06-18 01:17:06 +000037
Kevin Clarkd3cee022008-06-18 01:19:09 +000038 def initialize(opts)
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)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000043 end
44
Kevin Clarkd3cee022008-06-18 01:19:09 +000045 def start
Kevin Clark75532ee2008-06-18 01:19:14 +000046 return if @serverclass == Object
Kevin Clark66038a02008-06-18 01:19:18 +000047 args = (File.basename(@interpreter) == "jruby" ? "-J-server" : "")
48 @pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{@host} #{@port} #{@serverclass.name}", "r+")
Kevin Clark1a95a1d2008-06-18 01:19:28 +000049 Marshal.load(@pipe) # wait until the server has started
Kevin Clarkfdc9c972008-06-18 01:19:46 +000050 sleep 0.4 # give the server time to actually start spawning sockets
Kevin Clarkca8a1b32008-06-18 01:17:06 +000051 end
52
Kevin Clarkd3cee022008-06-18 01:19:09 +000053 def shutdown
54 return unless @pipe
55 Marshal.dump(:shutdown, @pipe)
56 begin
57 @pipe.read(10) # block until the server shuts down
58 rescue EOFError
Kevin Clarkca8a1b32008-06-18 01:17:06 +000059 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000060 @pipe.close
61 @pipe = nil
Kevin Clarkca8a1b32008-06-18 01:17:06 +000062 end
63end
64
Kevin Clarkca8a1b32008-06-18 01:17:06 +000065class BenchmarkManager
Kevin Clarkd3cee022008-06-18 01:19:09 +000066 def initialize(opts, server)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000067 @socket = opts.fetch(:socket) do
68 @host = opts.fetch(:host, 'localhost')
69 @port = opts.fetch(:port)
70 nil
71 end
Kevin Clarkca8a1b32008-06-18 01:17:06 +000072 @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)
Kevin Clarkd3cee022008-06-18 01:19:09 +000075 @interpreter = opts.fetch(:interpreter, "ruby")
76 @server = server
Kevin Clarkfdc9c972008-06-18 01:19:46 +000077 @log_exceptions = opts.fetch(:log_exceptions, false)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000078 end
79
80 def run
81 @pool = []
82 @benchmark_start = Time.now
83 puts "Spawning benchmark processes..."
84 @num_processes.times do
85 spawn
Kevin Clarkfdc9c972008-06-18 01:19:46 +000086 sleep 0.02 # space out spawns
Kevin Clarkca8a1b32008-06-18 01:17:06 +000087 end
88 collect_output
89 @benchmark_end = Time.now # we know the procs are done here
90 translate_output
91 analyze_output
92 report_output
93 end
94
95 def spawn
Kevin Clarkfdc9c972008-06-18 01:19:46 +000096 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 +000097 @pool << pipe
Kevin Clarkca8a1b32008-06-18 01:17:06 +000098 end
99
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000100 def socket_class
101 if @socket
102 Thrift::UNIXSocket
103 else
104 Thrift::Socket
105 end
106 end
107
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000108 def collect_output
109 puts "Collecting output..."
110 # read from @pool until all sockets are closed
111 @buffers = Hash.new { |h,k| h[k] = '' }
112 until @pool.empty?
113 rd, = select(@pool)
114 next if rd.nil?
115 rd.each do |fd|
116 begin
Kevin Clarkfb5c0eb2008-06-18 01:19:04 +0000117 @buffers[fd] << fd.readpartial(4096)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000118 rescue EOFError
119 @pool.delete fd
120 end
121 end
122 end
123 end
124
125 def translate_output
126 puts "Translating output..."
127 @output = []
128 @buffers.each do |fd, buffer|
129 strio = StringIO.new(buffer)
130 logs = []
131 begin
132 loop do
133 logs << Marshal.load(strio)
134 end
135 rescue EOFError
136 @output << logs
137 end
138 end
139 end
140
141 def analyze_output
142 puts "Analyzing output..."
143 call_times = []
144 client_times = []
145 connection_failures = []
Kevin Clarkd2719792008-06-18 01:19:37 +0000146 connection_errors = []
Kevin Clark66038a02008-06-18 01:19:18 +0000147 shortest_call = 0
148 shortest_client = 0
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000149 longest_call = 0
150 longest_client = 0
151 @output.each do |logs|
152 cur_call, cur_client = nil
153 logs.each do |tok, time|
154 case tok
155 when :start
156 cur_client = time
157 when :call_start
158 cur_call = time
159 when :call_end
160 delta = time - cur_call
161 call_times << delta
162 longest_call = delta unless longest_call > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000163 shortest_call = delta if shortest_call == 0 or delta < shortest_call
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000164 cur_call = nil
165 when :end
166 delta = time - cur_client
167 client_times << delta
168 longest_client = delta unless longest_client > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000169 shortest_client = delta if shortest_client == 0 or delta < shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000170 cur_client = nil
171 when :connection_failure
172 connection_failures << time
Kevin Clarkd2719792008-06-18 01:19:37 +0000173 when :connection_error
174 connection_errors << time
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000175 end
176 end
177 end
178 @report = {}
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
Kevin Clarkd2719792008-06-18 01:19:37 +0000184 @report[:connection_errors] = connection_errors.size
Kevin Clark66038a02008-06-18 01:19:18 +0000185 @report[:shortest_call] = shortest_call
186 @report[:shortest_client] = shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000187 @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')
191 end
192
193 def report_output
194 fmt = "%.4f seconds"
195 puts
196 tabulate "%d",
Kevin Clark75532ee2008-06-18 01:19:14 +0000197 [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],
Kevin Clarkd3cee022008-06-18 01:19:09 +0000198 [["Server interpreter", "%s"], @server.interpreter],
199 [["Client interpreter", "%s"], @interpreter],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000200 [["Socket class", "%s"], socket_class],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000201 ["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"]
205 puts
Kevin Clark75532ee2008-06-18 01:19:14 +0000206 failures = (@report[:connection_failures] > 0)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000207 tabulate fmt,
Kevin Clarkd2719792008-06-18 01:19:37 +0000208 [["Connection failures", "%d", [:red, :bold]], @report[:connection_failures]],
209 [["Connection errors", "%d", [:red, :bold]], @report[:connection_errors]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000210 ["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]],
Kevin Clark66038a02008-06-18 01:19:18 +0000214 ["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]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000217 ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]
218 end
219
Kevin Clark75532ee2008-06-18 01:19:14 +0000220 ANSI = {
221 :reset => 0,
222 :bold => 1,
223 :black => 30,
224 :red => 31,
225 :green => 32,
226 :yellow => 33,
227 :blue => 34,
228 :magenta => 35,
229 :cyan => 36,
230 :white => 37
231 }
232
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000233 def tabulate(fmt, *labels_and_values)
Bryan Duxbury74c3de62009-03-20 16:54:33 +0000234 labels = labels_and_values.map { |l| Array === l ? l.first : l }
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000235 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
236 labels_and_values.each do |(l,v)|
237 f = fmt
Kevin Clark75532ee2008-06-18 01:19:14 +0000238 l, f, c = l if Array === l
239 fmtstr = "%-#{label_width+1}s #{f}"
Kevin Clarkd2719792008-06-18 01:19:37 +0000240 if STDOUT.tty? and c and v.to_i > 0
Kevin Clark75532ee2008-06-18 01:19:14 +0000241 fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m"
242 end
243 puts fmtstr % [l+":", v]
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000244 end
245 end
246end
247
248def resolve_const(const)
249 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
250end
251
252puts "Starting server..."
Kevin Clarkd3cee022008-06-18 01:19:09 +0000253args = {}
254args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
255args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
Kevin Clarkd8d0d602008-06-18 01:20:10 +0000256args[:host] = ENV['THRIFT_HOST'] || HOST
257args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
Kevin Clarkd3cee022008-06-18 01:19:09 +0000258server = Server.new(args)
259server.start
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000260
Kevin Clarkd8d0d602008-06-18 01:20:10 +0000261args = {}
262args[:host] = ENV['THRIFT_HOST'] || HOST
263args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
Kevin Clark4b429ad2008-06-18 01:20:06 +0000264args[:num_processes] = (ENV['THRIFT_NUM_PROCESSES'] || 40).to_i
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000265args[:clients_per_process] = (ENV['THRIFT_NUM_CLIENTS'] || 5).to_i
266args[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_i
Kevin Clarkd3cee022008-06-18 01:19:09 +0000267args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000268args[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS']
Kevin Clarkd3cee022008-06-18 01:19:09 +0000269BenchmarkManager.new(args, server).run
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000270
Kevin Clarkd3cee022008-06-18 01:19:09 +0000271server.shutdown