David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 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 Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 20 | require 'rubygems' |
| 21 | $:.unshift File.dirname(__FILE__) + '/../lib' |
| 22 | require 'thrift' |
| 23 | require 'thrift/server/nonblockingserver' |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 24 | require 'thrift/transport/unixsocket' |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 25 | require 'stringio' |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 26 | |
Kevin Clark | 77b39b3 | 2008-06-18 01:20:16 +0000 | [diff] [blame] | 27 | HOST = '127.0.0.1' |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 28 | PORT = 42587 |
| 29 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 30 | ############### |
| 31 | ## Server |
| 32 | ############### |
| 33 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 34 | class Server |
| 35 | attr_accessor :serverclass |
| 36 | attr_accessor :interpreter |
| 37 | attr_accessor :host |
| 38 | attr_accessor :port |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 39 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 40 | 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 Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 45 | end |
| 46 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 47 | def start |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 48 | return if @serverclass == Object |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 49 | args = (File.basename(@interpreter) == "jruby" ? "-J-server" : "") |
| 50 | @pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{@host} #{@port} #{@serverclass.name}", "r+") |
Kevin Clark | 1a95a1d | 2008-06-18 01:19:28 +0000 | [diff] [blame] | 51 | Marshal.load(@pipe) # wait until the server has started |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 52 | sleep 0.4 # give the server time to actually start spawning sockets |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 53 | end |
| 54 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 55 | 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 Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 61 | end |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 62 | @pipe.close |
| 63 | @pipe = nil |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 64 | end |
| 65 | end |
| 66 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 67 | class BenchmarkManager |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 68 | def initialize(opts, server) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 69 | @socket = opts.fetch(:socket) do |
| 70 | @host = opts.fetch(:host, 'localhost') |
| 71 | @port = opts.fetch(:port) |
| 72 | nil |
| 73 | end |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 74 | @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 Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 77 | @interpreter = opts.fetch(:interpreter, "ruby") |
| 78 | @server = server |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 79 | @log_exceptions = opts.fetch(:log_exceptions, false) |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 80 | 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 Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 88 | sleep 0.02 # space out spawns |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 89 | 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 Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 98 | pipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{"-log-exceptions" if @log_exceptions} #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}") |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 99 | @pool << pipe |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 100 | end |
| 101 | |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 102 | def socket_class |
| 103 | if @socket |
| 104 | Thrift::UNIXSocket |
| 105 | else |
| 106 | Thrift::Socket |
| 107 | end |
| 108 | end |
| 109 | |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 110 | 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 Clark | fb5c0eb | 2008-06-18 01:19:04 +0000 | [diff] [blame] | 119 | @buffers[fd] << fd.readpartial(4096) |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 120 | 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 Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 148 | connection_errors = [] |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 149 | shortest_call = 0 |
| 150 | shortest_client = 0 |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 151 | 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 Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 165 | shortest_call = delta if shortest_call == 0 or delta < shortest_call |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 166 | cur_call = nil |
| 167 | when :end |
| 168 | delta = time - cur_client |
| 169 | client_times << delta |
| 170 | longest_client = delta unless longest_client > delta |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 171 | shortest_client = delta if shortest_client == 0 or delta < shortest_client |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 172 | cur_client = nil |
| 173 | when :connection_failure |
| 174 | connection_failures << time |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 175 | when :connection_error |
| 176 | connection_errors << time |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 177 | 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 Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 186 | @report[:connection_errors] = connection_errors.size |
Kevin Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 187 | @report[:shortest_call] = shortest_call |
| 188 | @report[:shortest_client] = shortest_client |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 189 | @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 Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 199 | [["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass], |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 200 | [["Server interpreter", "%s"], @server.interpreter], |
| 201 | [["Client interpreter", "%s"], @interpreter], |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 202 | [["Socket class", "%s"], socket_class], |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 203 | ["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 Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 208 | failures = (@report[:connection_failures] > 0) |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 209 | tabulate fmt, |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 210 | [["Connection failures", "%d", [:red, :bold]], @report[:connection_failures]], |
| 211 | [["Connection errors", "%d", [:red, :bold]], @report[:connection_errors]], |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 212 | ["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 Clark | 66038a0 | 2008-06-18 01:19:18 +0000 | [diff] [blame] | 216 | ["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 Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 219 | ["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]] |
| 220 | end |
| 221 | |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 222 | 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 Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 235 | def tabulate(fmt, *labels_and_values) |
Bryan Duxbury | 74c3de6 | 2009-03-20 16:54:33 +0000 | [diff] [blame] | 236 | labels = labels_and_values.map { |l| Array === l ? l.first : l } |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 237 | 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 Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 240 | l, f, c = l if Array === l |
| 241 | fmtstr = "%-#{label_width+1}s #{f}" |
Kevin Clark | d271979 | 2008-06-18 01:19:37 +0000 | [diff] [blame] | 242 | if STDOUT.tty? and c and v.to_i > 0 |
Kevin Clark | 75532ee | 2008-06-18 01:19:14 +0000 | [diff] [blame] | 243 | fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m" |
| 244 | end |
| 245 | puts fmtstr % [l+":", v] |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 246 | end |
| 247 | end |
| 248 | end |
| 249 | |
| 250 | def resolve_const(const) |
| 251 | const and const.split('::').inject(Object) { |k,c| k.const_get(c) } |
| 252 | end |
| 253 | |
| 254 | puts "Starting server..." |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 255 | args = {} |
| 256 | args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby" |
| 257 | args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServer |
Kevin Clark | d8d0d60 | 2008-06-18 01:20:10 +0000 | [diff] [blame] | 258 | args[:host] = ENV['THRIFT_HOST'] || HOST |
| 259 | args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 260 | server = Server.new(args) |
| 261 | server.start |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 262 | |
Kevin Clark | d8d0d60 | 2008-06-18 01:20:10 +0000 | [diff] [blame] | 263 | args = {} |
| 264 | args[:host] = ENV['THRIFT_HOST'] || HOST |
| 265 | args[:port] = (ENV['THRIFT_PORT'] || PORT).to_i |
Kevin Clark | 4b429ad | 2008-06-18 01:20:06 +0000 | [diff] [blame] | 266 | args[:num_processes] = (ENV['THRIFT_NUM_PROCESSES'] || 40).to_i |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 267 | args[:clients_per_process] = (ENV['THRIFT_NUM_CLIENTS'] || 5).to_i |
| 268 | args[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_i |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 269 | args[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby" |
Kevin Clark | fdc9c97 | 2008-06-18 01:19:46 +0000 | [diff] [blame] | 270 | args[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS'] |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 271 | BenchmarkManager.new(args, server).run |
Kevin Clark | ca8a1b3 | 2008-06-18 01:17:06 +0000 | [diff] [blame] | 272 | |
Kevin Clark | d3cee02 | 2008-06-18 01:19:09 +0000 | [diff] [blame] | 273 | server.shutdown |