blob: e6a1a3b5af6cbfdce454d1fc919b6d23499028f0 [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
33 sleep 0.2 # 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 Clarkca8a1b32008-06-18 01:17:06 +000060 end
61
62 def run
63 @pool = []
64 @benchmark_start = Time.now
65 puts "Spawning benchmark processes..."
66 @num_processes.times do
67 spawn
68 sleep 0.05 # space out spawns
69 end
70 collect_output
71 @benchmark_end = Time.now # we know the procs are done here
72 translate_output
73 analyze_output
74 report_output
75 end
76
77 def spawn
Kevin Clarkd3cee022008-06-18 01:19:09 +000078 pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}")
79 @pool << pipe
Kevin Clarkca8a1b32008-06-18 01:17:06 +000080 end
81
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000082 def socket_class
83 if @socket
84 Thrift::UNIXSocket
85 else
86 Thrift::Socket
87 end
88 end
89
Kevin Clarkca8a1b32008-06-18 01:17:06 +000090 def collect_output
91 puts "Collecting output..."
92 # read from @pool until all sockets are closed
93 @buffers = Hash.new { |h,k| h[k] = '' }
94 until @pool.empty?
95 rd, = select(@pool)
96 next if rd.nil?
97 rd.each do |fd|
98 begin
Kevin Clarkfb5c0eb2008-06-18 01:19:04 +000099 @buffers[fd] << fd.readpartial(4096)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000100 rescue EOFError
101 @pool.delete fd
102 end
103 end
104 end
105 end
106
107 def translate_output
108 puts "Translating output..."
109 @output = []
110 @buffers.each do |fd, buffer|
111 strio = StringIO.new(buffer)
112 logs = []
113 begin
114 loop do
115 logs << Marshal.load(strio)
116 end
117 rescue EOFError
118 @output << logs
119 end
120 end
121 end
122
123 def analyze_output
124 puts "Analyzing output..."
125 call_times = []
126 client_times = []
127 connection_failures = []
Kevin Clark66038a02008-06-18 01:19:18 +0000128 shortest_call = 0
129 shortest_client = 0
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000130 longest_call = 0
131 longest_client = 0
132 @output.each do |logs|
133 cur_call, cur_client = nil
134 logs.each do |tok, time|
135 case tok
136 when :start
137 cur_client = time
138 when :call_start
139 cur_call = time
140 when :call_end
141 delta = time - cur_call
142 call_times << delta
143 longest_call = delta unless longest_call > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000144 shortest_call = delta if shortest_call == 0 or delta < shortest_call
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000145 cur_call = nil
146 when :end
147 delta = time - cur_client
148 client_times << delta
149 longest_client = delta unless longest_client > delta
Kevin Clark66038a02008-06-18 01:19:18 +0000150 shortest_client = delta if shortest_client == 0 or delta < shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000151 cur_client = nil
152 when :connection_failure
153 connection_failures << time
154 end
155 end
156 end
157 @report = {}
158 @report[:total_calls] = call_times.inject(0.0) { |a,t| a += t }
159 @report[:avg_calls] = @report[:total_calls] / call_times.size
160 @report[:total_clients] = client_times.inject(0.0) { |a,t| a += t }
161 @report[:avg_clients] = @report[:total_clients] / client_times.size
162 @report[:connection_failures] = connection_failures.size
Kevin Clark66038a02008-06-18 01:19:18 +0000163 @report[:shortest_call] = shortest_call
164 @report[:shortest_client] = shortest_client
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000165 @report[:longest_call] = longest_call
166 @report[:longest_client] = longest_client
167 @report[:total_benchmark_time] = @benchmark_end - @benchmark_start
168 @report[:fastthread] = $".include?('fastthread.bundle')
169 end
170
171 def report_output
172 fmt = "%.4f seconds"
173 puts
174 tabulate "%d",
Kevin Clark75532ee2008-06-18 01:19:14 +0000175 [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],
Kevin Clarkd3cee022008-06-18 01:19:09 +0000176 [["Server interpreter", "%s"], @server.interpreter],
177 [["Client interpreter", "%s"], @interpreter],
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000178 [["Socket class", "%s"], socket_class],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000179 ["Number of processes", @num_processes],
180 ["Clients per process", @clients_per_process],
181 ["Calls per client", @calls_per_client],
182 [["Using fastthread", "%s"], @report[:fastthread] ? "yes" : "no"]
183 puts
Kevin Clark75532ee2008-06-18 01:19:14 +0000184 failures = (@report[:connection_failures] > 0)
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000185 tabulate fmt,
Kevin Clark75532ee2008-06-18 01:19:14 +0000186 [["Connection failures", "%d", *(failures ? [[:red, :bold]] : [])], @report[:connection_failures]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000187 ["Average time per call", @report[:avg_calls]],
188 ["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]],
189 ["Total time for all calls", @report[:total_calls]],
190 ["Real time for benchmarking", @report[:total_benchmark_time]],
Kevin Clark66038a02008-06-18 01:19:18 +0000191 ["Shortest call time", @report[:shortest_call]],
192 ["Longest call time", @report[:longest_call]],
193 ["Shortest client time (%d calls)" % @calls_per_client, @report[:shortest_client]],
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000194 ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]
195 end
196
Kevin Clark75532ee2008-06-18 01:19:14 +0000197 ANSI = {
198 :reset => 0,
199 :bold => 1,
200 :black => 30,
201 :red => 31,
202 :green => 32,
203 :yellow => 33,
204 :blue => 34,
205 :magenta => 35,
206 :cyan => 36,
207 :white => 37
208 }
209
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000210 def tabulate(fmt, *labels_and_values)
211 labels = labels_and_values.map { |(l,)| Array === l ? l.first : l }
212 label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }
213 labels_and_values.each do |(l,v)|
214 f = fmt
Kevin Clark75532ee2008-06-18 01:19:14 +0000215 l, f, c = l if Array === l
216 fmtstr = "%-#{label_width+1}s #{f}"
217 if STDOUT.tty? and c
218 fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m"
219 end
220 puts fmtstr % [l+":", v]
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000221 end
222 end
223end
224
225def resolve_const(const)
226 const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
227end
228
229puts "Starting server..."
Kevin Clarkd3cee022008-06-18 01:19:09 +0000230args = {}
231args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"
232args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer
233server = Server.new(args)
234server.start
Kevin Clarkca8a1b32008-06-18 01:17:06 +0000235
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