blob: 20f40f0cef52ca47512d7c84e7182b1807e1c857 [file] [log] [blame]
Kevin Clarkca8a1b32008-06-18 01:17:06 +00001require 'rubygems'
2$:.unshift File.dirname(__FILE__) + '/../lib'
3require 'thrift'
4require 'thrift/server/nonblockingserver'
Kevin Clark2ddd8ed2008-06-18 01:18:35 +00005require 'thrift/transport/unixsocket'
Kevin Clarkca8a1b32008-06-18 01:17:06 +00006require 'stringio'
Kevin Clarkd3cee022008-06-18 01:19:09 +00007
Kevin Clarkca8a1b32008-06-18 01:17:06 +00008HOST = 'localhost'
9PORT = 42587
10
Kevin Clarkca8a1b32008-06-18 01:17:06 +000011###############
12## Server
13###############
14
Kevin Clarkd3cee022008-06-18 01:19:09 +000015class Server
16 attr_accessor :serverclass
17 attr_accessor :interpreter
18 attr_accessor :host
19 attr_accessor :port
Kevin Clarkca8a1b32008-06-18 01:17:06 +000020
Kevin Clarkd3cee022008-06-18 01:19:09 +000021 def initialize(opts)
22 @serverclass = opts.fetch(:class, Thrift::NonblockingServer)
23 @interpreter = opts.fetch(:interpreter, "ruby")
24 @host = opts.fetch(:host, ::HOST)
25 @port = opts.fetch(:port, ::PORT)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000026 end
27
Kevin Clarkd3cee022008-06-18 01:19:09 +000028 def start
Kevin Clark75532ee2008-06-18 01:19:14 +000029 return if @serverclass == Object
Kevin Clark66038a02008-06-18 01:19:18 +000030 args = (File.basename(@interpreter) == "jruby" ? "-J-server" : "")
31 @pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{@host} #{@port} #{@serverclass.name}", "r+")
Kevin Clark1a95a1d2008-06-18 01:19:28 +000032 Marshal.load(@pipe) # wait until the server has started
Kevin Clarkfdc9c972008-06-18 01:19:46 +000033 sleep 0.4 # give the server time to actually start spawning sockets
Kevin Clarkca8a1b32008-06-18 01:17:06 +000034 end
35
Kevin Clarkd3cee022008-06-18 01:19:09 +000036 def shutdown
37 return unless @pipe
38 Marshal.dump(:shutdown, @pipe)
39 begin
40 @pipe.read(10) # block until the server shuts down
41 rescue EOFError
Kevin Clarkca8a1b32008-06-18 01:17:06 +000042 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000043 @pipe.close
44 @pipe = nil
Kevin Clarkca8a1b32008-06-18 01:17:06 +000045 end
46end
47
Kevin Clarkca8a1b32008-06-18 01:17:06 +000048class BenchmarkManager
Kevin Clarkd3cee022008-06-18 01:19:09 +000049 def initialize(opts, server)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000050 @socket = opts.fetch(:socket) do
51 @host = opts.fetch(:host, 'localhost')
52 @port = opts.fetch(:port)
53 nil
54 end
Kevin Clarkca8a1b32008-06-18 01:17:06 +000055 @num_processes = opts.fetch(:num_processes, 40)
56 @clients_per_process = opts.fetch(:clients_per_process, 10)
57 @calls_per_client = opts.fetch(:calls_per_client, 50)
Kevin Clarkd3cee022008-06-18 01:19:09 +000058 @interpreter = opts.fetch(:interpreter, "ruby")
59 @server = server
Kevin Clarkfdc9c972008-06-18 01:19:46 +000060 @log_exceptions = opts.fetch(:log_exceptions, false)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000061 end
62
63 def run
64 @pool = []
65 @benchmark_start = Time.now
66 puts "Spawning benchmark processes..."
67 @num_processes.times do
68 spawn
Kevin Clarkfdc9c972008-06-18 01:19:46 +000069 sleep 0.02 # space out spawns
Kevin Clarkca8a1b32008-06-18 01:17:06 +000070 end
71 collect_output
72 @benchmark_end = Time.now # we know the procs are done here
73 translate_output
74 analyze_output
75 report_output
76 end
77
78 def spawn
Kevin Clarkfdc9c972008-06-18 01:19:46 +000079 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 +000080 @pool << pipe
Kevin Clarkca8a1b32008-06-18 01:17:06 +000081 end
82
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000083 def socket_class
84 if @socket
85 Thrift::UNIXSocket
86 else
87 Thrift::Socket
88 end
89 end
90
Kevin Clarkca8a1b32008-06-18 01:17:06 +000091 def collect_output
92 puts "Collecting output..."
93 # read from @pool until all sockets are closed
94 @buffers = Hash.new { |h,k| h[k] = '' }
95 until @pool.empty?
96 rd, = select(@pool)
97 next if rd.nil?
98 rd.each do |fd|
99 begin
Kevin Clarkfb5c0eb2008-06-18 01:19:04 +0000100 @buffers[fd] << fd.readpartial(4096)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000101 rescue EOFError
102 @pool.delete fd
103 end
104 end
105 end
106 end
107
108 def translate_output
109 puts "Translating output..."
110 @output = []
111 @buffers.each do |fd, buffer|
112 strio = StringIO.new(buffer)
113 logs = []
114 begin
115 loop do
116 logs << Marshal.load(strio)
117 end
118 rescue EOFError
119 @output << logs
120 end
121 end
122 end
123
124 def analyze_output
125 puts "Analyzing output..."
126 call_times = []
127 client_times = []
128 connection_failures = []
Kevin Clarkd2719792008-06-18 01:19:37 +0000129 connection_errors = []
Kevin Clark66038a02008-06-18 01:19:18 +0000130 shortest_call = 0
131 shortest_client = 0
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000132 longest_call = 0
133 longest_client = 0
134 @output.each do |logs|
135 cur_call, cur_client = nil
136 logs.each do |tok, time|
137 case tok
138 when :start
139 cur_client = time
140 when :call_start
141 cur_call = time
142 when :call_end
143 delta = time - cur_call
144 call_times << delta
145 longest_call = delta unless longest_call > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000146 shortest_call = delta if shortest_call == 0 or delta < shortest_call
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000147 cur_call = nil
148 when :end
149 delta = time - cur_client
150 client_times << delta
151 longest_client = delta unless longest_client > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000152 shortest_client = delta if shortest_client == 0 or delta < shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000153 cur_client = nil
154 when :connection_failure
155 connection_failures << time
Kevin Clarkd2719792008-06-18 01:19:37 +0000156 when :connection_error
157 connection_errors << time
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000158 end
159 end
160 end
161 @report = {}
162 @report[:total_calls] = call_times.inject(0.0) { |a,t| a += t }
163 @report[:avg_calls] = @report[:total_calls] / call_times.size
164 @report[:total_clients] = client_times.inject(0.0) { |a,t| a += t }
165 @report[:avg_clients] = @report[:total_clients] / client_times.size
166 @report[:connection_failures] = connection_failures.size
Kevin Clarkd2719792008-06-18 01:19:37 +0000167 @report[:connection_errors] = connection_errors.size
Kevin Clark66038a02008-06-18 01:19:18 +0000168 @report[:shortest_call] = shortest_call
169 @report[:shortest_client] = shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000170 @report[:longest_call] = longest_call
171 @report[:longest_client] = longest_client
172 @report[:total_benchmark_time] = @benchmark_end - @benchmark_start
173 @report[:fastthread] = $".include?('fastthread.bundle')
174 end
175
176 def report_output
177 fmt = "%.4f seconds"
178 puts
179 tabulate "%d",
Kevin Clark75532ee2008-06-18 01:19:14 +0000180 [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],
Kevin Clarkd3cee022008-06-18 01:19:09 +0000181 [["Server interpreter", "%s"], @server.interpreter],
182 [["Client interpreter", "%s"], @interpreter],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000183 [["Socket class", "%s"], socket_class],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000184 ["Number of processes", @num_processes],
185 ["Clients per process", @clients_per_process],
186 ["Calls per client", @calls_per_client],
187 [["Using fastthread", "%s"], @report[:fastthread] ? "yes" : "no"]
188 puts
Kevin Clark75532ee2008-06-18 01:19:14 +0000189 failures = (@report[:connection_failures] > 0)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000190 tabulate fmt,
Kevin Clarkd2719792008-06-18 01:19:37 +0000191 [["Connection failures", "%d", [:red, :bold]], @report[:connection_failures]],
192 [["Connection errors", "%d", [:red, :bold]], @report[:connection_errors]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000193 ["Average time per call", @report[:avg_calls]],
194 ["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]],
195 ["Total time for all calls", @report[:total_calls]],
196 ["Real time for benchmarking", @report[:total_benchmark_time]],
Kevin Clark66038a02008-06-18 01:19:18 +0000197 ["Shortest call time", @report[:shortest_call]],
198 ["Longest call time", @report[:longest_call]],
199 ["Shortest client time (%d calls)" % @calls_per_client, @report[:shortest_client]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000200 ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]
201 end
202
Kevin Clark75532ee2008-06-18 01:19:14 +0000203 ANSI = {
204 :reset => 0,
205 :bold => 1,
206 :black => 30,
207 :red => 31,
208 :green => 32,
209 :yellow => 33,
210 :blue => 34,
211 :magenta => 35,
212 :cyan => 36,
213 :white => 37
214 }
215
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000216 def tabulate(fmt, *labels_and_values)
217 labels = labels_and_values.map { |(l,)| Array === l ? l.first : l }
218 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
219 labels_and_values.each do |(l,v)|
220 f = fmt
Kevin Clark75532ee2008-06-18 01:19:14 +0000221 l, f, c = l if Array === l
222 fmtstr = "%-#{label_width+1}s #{f}"
Kevin Clarkd2719792008-06-18 01:19:37 +0000223 if STDOUT.tty? and c and v.to_i > 0
Kevin Clark75532ee2008-06-18 01:19:14 +0000224 fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m"
225 end
226 puts fmtstr % [l+":", v]
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000227 end
228 end
229end
230
231def resolve_const(const)
232 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
233end
234
235puts "Starting server..."
Kevin Clarkd3cee022008-06-18 01:19:09 +0000236args = {}
237args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
238args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
Kevin Clarkd8d0d602008-06-18 01:20:10 +0000239args[:host] = ENV['THRIFT_HOST'] || HOST
240args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
Kevin Clarkd3cee022008-06-18 01:19:09 +0000241server = Server.new(args)
242server.start
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000243
Kevin Clarkd8d0d602008-06-18 01:20:10 +0000244args = {}
245args[:host] = ENV['THRIFT_HOST'] || HOST
246args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i
Kevin Clark4b429ad2008-06-18 01:20:06 +0000247args[:num_processes] = (ENV['THRIFT_NUM_PROCESSES'] || 40).to_i
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000248args[:clients_per_process] = (ENV['THRIFT_NUM_CLIENTS'] || 5).to_i
249args[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_i
Kevin Clarkd3cee022008-06-18 01:19:09 +0000250args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
Kevin Clarkfdc9c972008-06-18 01:19:46 +0000251args[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS']
Kevin Clarkd3cee022008-06-18 01:19:09 +0000252BenchmarkManager.new(args, server).run
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000253
Kevin Clarkd3cee022008-06-18 01:19:09 +0000254server.shutdown