Rev 30 | Blame | Compare with Previous | Last modification | View Log | RSS feed
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.#require 'rubygems'$:.unshift File.dirname(__FILE__) + '/../lib'require 'thrift'require 'stringio'HOST = '127.0.0.1'PORT = 42587################# Server###############class Serverattr_accessor :serverclassattr_accessor :interpreterattr_accessor :hostattr_accessor :portdef initialize(opts)@serverclass = opts.fetch(:class, Thrift::NonblockingServer)@interpreter = opts.fetch(:interpreter, "ruby")@host = opts.fetch(:host, ::HOST)@port = opts.fetch(:port, ::PORT)enddef startreturn if @serverclass == Objectargs = (File.basename(@interpreter) == "jruby" ? "-J-server" : "")@pipe = IO.popen("#{@interpreter} #{args} #{File.dirname(__FILE__)}/server.rb #{@host} #{@port} #{@serverclass.name}", "r+")Marshal.load(@pipe) # wait until the server has startedsleep 0.4 # give the server time to actually start spawning socketsenddef shutdownreturn unless @pipeMarshal.dump(:shutdown, @pipe)begin@pipe.read(10) # block until the server shuts downrescue EOFErrorend@pipe.close@pipe = nilendendclass BenchmarkManagerdef initialize(opts, server)@socket = opts.fetch(:socket) do@host = opts.fetch(:host, 'localhost')@port = opts.fetch(:port)nilend@num_processes = opts.fetch(:num_processes, 40)@clients_per_process = opts.fetch(:clients_per_process, 10)@calls_per_client = opts.fetch(:calls_per_client, 50)@interpreter = opts.fetch(:interpreter, "ruby")@server = server@log_exceptions = opts.fetch(:log_exceptions, false)enddef run@pool = []@benchmark_start = Time.nowputs "Spawning benchmark processes..."@num_processes.times dospawnsleep 0.02 # space out spawnsendcollect_output@benchmark_end = Time.now # we know the procs are done heretranslate_outputanalyze_outputreport_outputenddef spawnpipe = IO.popen("#{@interpreter} #{File.dirname(__FILE__)}/client.rb #{"-log-exceptions" if @log_exceptions} #{@host} #{@port} #{@clients_per_process} #{@calls_per_client}")@pool << pipeenddef socket_classif @socketThrift::UNIXSocketelseThrift::Socketendenddef collect_outputputs "Collecting output..."# read from @pool until all sockets are closed@buffers = Hash.new { |h,k| h[k] = '' }until @pool.empty?rd, = select(@pool)next if rd.nil?rd.each do |fd|begin@buffers[fd] << fd.readpartial(4096)rescue EOFError@pool.delete fdendendendenddef translate_outputputs "Translating output..."@output = []@buffers.each do |fd, buffer|strio = StringIO.new(buffer)logs = []beginloop dologs << Marshal.load(strio)endrescue EOFError@output << logsendendenddef analyze_outputputs "Analyzing output..."call_times = []client_times = []connection_failures = []connection_errors = []shortest_call = 0shortest_client = 0longest_call = 0longest_client = 0@output.each do |logs|cur_call, cur_client = nillogs.each do |tok, time|case tokwhen :startcur_client = timewhen :call_startcur_call = timewhen :call_enddelta = time - cur_callcall_times << deltalongest_call = delta unless longest_call > deltashortest_call = delta if shortest_call == 0 or delta < shortest_callcur_call = nilwhen :enddelta = time - cur_clientclient_times << deltalongest_client = delta unless longest_client > deltashortest_client = delta if shortest_client == 0 or delta < shortest_clientcur_client = nilwhen :connection_failureconnection_failures << timewhen :connection_errorconnection_errors << timeendendend@report = {}@report[:total_calls] = call_times.inject(0.0) { |a,t| a += t }@report[:avg_calls] = @report[:total_calls] / call_times.size@report[:total_clients] = client_times.inject(0.0) { |a,t| a += t }@report[:avg_clients] = @report[:total_clients] / client_times.size@report[:connection_failures] = connection_failures.size@report[:connection_errors] = connection_errors.size@report[:shortest_call] = shortest_call@report[:shortest_client] = shortest_client@report[:longest_call] = longest_call@report[:longest_client] = longest_client@report[:total_benchmark_time] = @benchmark_end - @benchmark_start@report[:fastthread] = $".include?('fastthread.bundle')enddef report_outputfmt = "%.4f seconds"putstabulate "%d",[["Server class", "%s"], @server.serverclass == Object ? "" : @server.serverclass],[["Server interpreter", "%s"], @server.interpreter],[["Client interpreter", "%s"], @interpreter],[["Socket class", "%s"], socket_class],["Number of processes", @num_processes],["Clients per process", @clients_per_process],["Calls per client", @calls_per_client],[["Using fastthread", "%s"], @report[:fastthread] ? "yes" : "no"]putsfailures = (@report[:connection_failures] > 0)tabulate fmt,[["Connection failures", "%d", [:red, :bold]], @report[:connection_failures]],[["Connection errors", "%d", [:red, :bold]], @report[:connection_errors]],["Average time per call", @report[:avg_calls]],["Average time per client (%d calls)" % @calls_per_client, @report[:avg_clients]],["Total time for all calls", @report[:total_calls]],["Real time for benchmarking", @report[:total_benchmark_time]],["Shortest call time", @report[:shortest_call]],["Longest call time", @report[:longest_call]],["Shortest client time (%d calls)" % @calls_per_client, @report[:shortest_client]],["Longest client time (%d calls)" % @calls_per_client, @report[:longest_client]]endANSI = {:reset => 0,:bold => 1,:black => 30,:red => 31,:green => 32,:yellow => 33,:blue => 34,:magenta => 35,:cyan => 36,:white => 37}def tabulate(fmt, *labels_and_values)labels = labels_and_values.map { |l| Array === l ? l.first : l }label_width = labels.inject(0) { |w,l| l.size > w ? l.size : w }labels_and_values.each do |(l,v)|f = fmtl, f, c = l if Array === lfmtstr = "%-#{label_width+1}s #{f}"if STDOUT.tty? and c and v.to_i > 0fmtstr = "\e[#{[*c].map { |x| ANSI[x] } * ";"}m" + fmtstr + "\e[#{ANSI[:reset]}m"endputs fmtstr % [l+":", v]endendenddef resolve_const(const)const and const.split('::').inject(Object) { |k,c| k.const_get(c) }endputs "Starting server..."args = {}args[:interpreter] = ENV['THRIFT_SERVER_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"args[:class] = resolve_const(ENV['THRIFT_SERVER']) || Thrift::NonblockingServerargs[:host] = ENV['THRIFT_HOST'] || HOSTargs[:port] = (ENV['THRIFT_PORT'] || PORT).to_iserver = Server.new(args)server.startargs = {}args[:host] = ENV['THRIFT_HOST'] || HOSTargs[:port] = (ENV['THRIFT_PORT'] || PORT).to_iargs[:num_processes] = (ENV['THRIFT_NUM_PROCESSES'] || 40).to_iargs[:clients_per_process] = (ENV['THRIFT_NUM_CLIENTS'] || 5).to_iargs[:calls_per_client] = (ENV['THRIFT_NUM_CALLS'] || 50).to_iargs[:interpreter] = ENV['THRIFT_CLIENT_INTERPRETER'] || ENV['THRIFT_INTERPRETER'] || "ruby"args[:log_exceptions] = !!ENV['THRIFT_LOG_EXCEPTIONS']BenchmarkManager.new(args, server).runserver.shutdown