blob: be3fc7121979d759f25bdf7e51930eeb8a9117f3 [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 Clarkca8a1b32008-06-18 01:17:06 +000032 end
33
Kevin Clarkd3cee022008-06-18 01:19:09 +000034 def shutdown
35 return unless @pipe
36 Marshal.dump(:shutdown, @pipe)
37 begin
38 @pipe.read(10) # block until the server shuts down
39 rescue EOFError
Kevin Clarkca8a1b32008-06-18 01:17:06 +000040 end
Kevin Clarkd3cee022008-06-18 01:19:09 +000041 @pipe.close
42 @pipe = nil
Kevin Clarkca8a1b32008-06-18 01:17:06 +000043 end
44end
45
Kevin Clarkca8a1b32008-06-18 01:17:06 +000046class BenchmarkManager
Kevin Clarkd3cee022008-06-18 01:19:09 +000047 def initialize(opts, server)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000048 @socket = opts.fetch(:socket) do
49 @host = opts.fetch(:host, 'localhost')
50 @port = opts.fetch(:port)
51 nil
52 end
Kevin Clarkca8a1b32008-06-18 01:17:06 +000053 @num_processes = opts.fetch(:num_processes, 40)
54 @clients_per_process = opts.fetch(:clients_per_process, 10)
55 @calls_per_client = opts.fetch(:calls_per_client, 50)
Kevin Clarkd3cee022008-06-18 01:19:09 +000056 @interpreter = opts.fetch(:interpreter, "ruby")
57 @server = server
Kevin Clarkca8a1b32008-06-18 01:17:06 +000058 end
59
60 def run
61 @pool = []
62 @benchmark_start = Time.now
63 puts "Spawning benchmark processes..."
64 @num_processes.times do
65 spawn
66 sleep 0.05 # space out spawns
67 end
68 collect_output
69 @benchmark_end = Time.now # we know the procs are done here
70 translate_output
71 analyze_output
72 report_output
73 end
74
75 def spawn
Kevin Clarkd3cee022008-06-18 01:19:09 +000076 pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}")
77 @pool << pipe
Kevin Clarkca8a1b32008-06-18 01:17:06 +000078 end
79
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000080 def socket_class
81 if @socket
82 Thrift::UNIXSocket
83 else
84 Thrift::Socket
85 end
86 end
87
Kevin Clarkca8a1b32008-06-18 01:17:06 +000088 def collect_output
89 puts "Collecting output..."
90 # read from @pool until all sockets are closed
91 @buffers = Hash.new { |h,k| h[k] = '' }
92 until @pool.empty?
93 rd, = select(@pool)
94 next if rd.nil?
95 rd.each do |fd|
96 begin
Kevin Clarkfb5c0eb2008-06-18 01:19:04 +000097 @buffers[fd] << fd.readpartial(4096)
Kevin Clarkca8a1b32008-06-18 01:17:06 +000098 rescue EOFError
99 @pool.delete fd
100 end
101 end
102 end
103 end
104
105 def translate_output
106 puts "Translating output..."
107 @output = []
108 @buffers.each do |fd, buffer|
109 strio = StringIO.new(buffer)
110 logs = []
111 begin
112 loop do
113 logs << Marshal.load(strio)
114 end
115 rescue EOFError
116 @output << logs
117 end
118 end
119 end
120
121 def analyze_output
122 puts "Analyzing output..."
123 call_times = []
124 client_times = []
125 connection_failures = []
Kevin Clark66038a02008-06-18 01:19:18 +0000126 shortest_call = 0
127 shortest_client = 0
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000128 longest_call = 0
129 longest_client = 0
130 @output.each do |logs|
131 cur_call, cur_client = nil
132 logs.each do |tok, time|
133 case tok
134 when :start
135 cur_client = time
136 when :call_start
137 cur_call = time
138 when :call_end
139 delta = time - cur_call
140 call_times << delta
141 longest_call = delta unless longest_call > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000142 shortest_call = delta if shortest_call == 0 or delta < shortest_call
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000143 cur_call = nil
144 when :end
145 delta = time - cur_client
146 client_times << delta
147 longest_client = delta unless longest_client > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000148 shortest_client = delta if shortest_client == 0 or delta < shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000149 cur_client = nil
150 when :connection_failure
151 connection_failures << time
152 end
153 end
154 end
155 @report = {}
156 @report[:total_calls] = call_times.inject(0.0) { |a,t| a += t }
157 @report[:avg_calls] = @report[:total_calls] / call_times.size
158 @report[:total_clients] = client_times.inject(0.0) { |a,t| a += t }
159 @report[:avg_clients] = @report[:total_clients] / client_times.size
160 @report[:connection_failures] = connection_failures.size
Kevin Clark66038a02008-06-18 01:19:18 +0000161 @report[:shortest_call] = shortest_call
162 @report[:shortest_client] = shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000163 @report[:longest_call] = longest_call
164 @report[:longest_client] = longest_client
165 @report[:total_benchmark_time] = @benchmark_end - @benchmark_start
166 @report[:fastthread] = $".include?('fastthread.bundle')
167 end
168
169 def report_output
170 fmt = "%.4f seconds"
171 puts
172 tabulate "%d",
Kevin Clark75532ee2008-06-18 01:19:14 +0000173 [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],
Kevin Clarkd3cee022008-06-18 01:19:09 +0000174 [["Server interpreter", "%s"], @server.interpreter],
175 [["Client interpreter", "%s"], @interpreter],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000176 [["Socket class", "%s"], socket_class],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000177 ["Number of processes", @num_processes],
178 ["Clients per process", @clients_per_process],
179 ["Calls per client", @calls_per_client],
180 [["Using fastthread", "%s"], @report[:fastthread] ? "yes" : "no"]
181 puts
Kevin Clark75532ee2008-06-18 01:19:14 +0000182 failures = (@report[:connection_failures] > 0)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000183 tabulate fmt,
Kevin Clark75532ee2008-06-18 01:19:14 +0000184 [["Connection failures", "%d", *(failures ? [[:red, :bold]] : [])], @report[:connection_failures]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000185 ["Average time per call", @report[:avg_calls]],
186 ["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]],
187 ["Total time for all calls", @report[:total_calls]],
188 ["Real time for benchmarking", @report[:total_benchmark_time]],
Kevin Clark66038a02008-06-18 01:19:18 +0000189 ["Shortest call time", @report[:shortest_call]],
190 ["Longest call time", @report[:longest_call]],
191 ["Shortest client time (%d calls)" % @calls_per_client, @report[:shortest_client]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000192 ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]
193 end
194
Kevin Clark75532ee2008-06-18 01:19:14 +0000195 ANSI = {
196 :reset => 0,
197 :bold => 1,
198 :black => 30,
199 :red => 31,
200 :green => 32,
201 :yellow => 33,
202 :blue => 34,
203 :magenta => 35,
204 :cyan => 36,
205 :white => 37
206 }
207
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000208 def tabulate(fmt, *labels_and_values)
209 labels = labels_and_values.map { |(l,)| Array === l ? l.first : l }
210 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
211 labels_and_values.each do |(l,v)|
212 f = fmt
Kevin Clark75532ee2008-06-18 01:19:14 +0000213 l, f, c = l if Array === l
214 fmtstr = "%-#{label_width+1}s #{f}"
215 if STDOUT.tty? and c
216 fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m"
217 end
218 puts fmtstr % [l+":", v]
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000219 end
220 end
221end
222
223def resolve_const(const)
224 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
225end
226
227puts "Starting server..."
Kevin Clarkd3cee022008-06-18 01:19:09 +0000228args = {}
229args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
230args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
231server = Server.new(args)
232server.start
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000233
234sleep 0.2 # give the server time to start
235
Kevin Clarkd3cee022008-06-18 01:19:09 +0000236args = { :num_processes => 40, :clients_per_process => 5, :host => HOST, :port => PORT }
237args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
238BenchmarkManager.new(args, server).run
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000239
Kevin Clarkd3cee022008-06-18 01:19:09 +0000240server.shutdown