YUKI Hiroshi
null+****@clear*****
Fri Oct 11 10:23:44 JST 2013
YUKI Hiroshi 2013-10-11 10:23:44 +0900 (Fri, 11 Oct 2013) New Revision: bbb317346cf5d393afb3cf952c3daae62084a43d https://github.com/droonga/drnbench/commit/bbb317346cf5d393afb3cf952c3daae62084a43d Message: Separate definition of client and result classes from runner.rb Added files: lib/droonga/benchmark/client/http.rb lib/droonga/benchmark/result.rb Modified files: lib/droonga/benchmark/runner.rb Added: lib/droonga/benchmark/client/http.rb (+69 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/benchmark/client/http.rb 2013-10-11 10:23:44 +0900 (279776b) @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- + +require "thread" +require "net/http" +require "json" + +module Droonga + module Benchmark + class HttpClient + attr_reader :requests, :results, :wait + + DEFAULT_PATH = "/" + DEFAULT_METHOD = "GET" + + def initialize(params) + @requests = params[:requests] + @result = params[:result] + @wait = params[:wait] + + @default_host = params[:host] + @default_port = params[:port] + @default_path = params[:path] || DEFAULT_PATH + @default_method = params[:method] || DEFAULT_METHOD + end + + def run + @thread = Thread.new do + loop do + request =****@reque***** + request[:host] ||= @default_host + request[:port] ||= @default_port + request[:path] ||= @default_path + request[:method] ||= @default_method + request[:method] = request[:method].upcase + + Net::HTTP.start(request[:host], request[:port]) do |http| + header = { + "user-agent" => "Ruby/#{RUBY_VERSION} Droonga::Benchmark::Runner::HttpClient" + } + response = nil + start_time = Time.now + case request[:method] + when "GET" + response = http.get(request[:path], header) + when "POST" + body = request[:body] + unless body.is_a?(String) + body = JSON.generate(body) + end + response = http.post(request[:path], body, header) + end + @result << { + :request => request, + :status => response.code, + :elapsed_time => Time.now - start_time, + } + end + sleep @wait + end + end + self + end + + def stop + @thread.exit + end + end + end +end Added: lib/droonga/benchmark/result.rb (+114 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/benchmark/result.rb 2013-10-11 10:23:44 +0900 (9e97c24) @@ -0,0 +1,114 @@ +# -*- coding: utf-8 -*- + +module Droonga + module Benchmark + class Result + attr_reader :n_clients, :duration, :statuses + + class << self + def keys + [ + :n_clients, + :total_n_requests, + :queries_per_second, + :min_elapsed_time, + :max_elapsed_time, + :average_elapsed_time, + ] + end + end + + def initialize(params) + @n_clients = params[:n_clients] + @duration = params[:duration] + + @results = [] + @total_elapsed_time = 0.0 + @elapsed_times = [] + @statuses = {} + end + + def <<(result) + clear_cached_statistics + + @results << result + + @statuses[result[:status]] ||= 0 + @statuses[result[:status]] += 1 + + @elapsed_times << result[:elapsed_time] + @total_elapsed_time += result[:elapsed_time] + end + + def total_n_requests + @total_n_requests ||=****@resul***** + end + + def queries_per_second + @queries_per_second ||= total_n_requests.to_f / @duration + end + + def status_percentages + @status_percentages ||= prepare_status_percentages + end + + def min_elapsed_time + @min_elapsed_time ||= @elapsed_times.min + end + + def max_elapsed_time + @max_elapsed_time ||= @elapsed_times.min + end + + def average_elapsed_time + @average_elapsed_time ||= @total_elapsed_time / @elapsed_times.size + end + + def to_s + "Total requests: #{total_n_requests} " + + "(#{queries_per_second} queries per second)\n" + + "Status:\n" + + status_percentages.collect do |status, percentage| + " #{status}: #{percentage} %" + end.join("\n") + "\n" + + "Elapsed time:\n" + + " min: #{min_elapsed_time} sec\n" + + " max: #{max_elapsed_time} sec\n" + + " average: #{average_elapsed_time} sec" + end + + def values + self.class.keys.collect do |column| + send(column) + end + end + + private + def clear_cached_statistics + @total_n_requests = nil + @queries_per_second = nil + @status_percentages = nil + @min_elapsed_time = nil + @max_elapsed_time = nil + @average_elapsed_time = nil + end + + def prepare_status_percentages + status_percentages = [] + @statuses.each do |status, n_results| + percentage = n_results.to_f / total_n_requests * 100 + status_percentages << {:percentage => percentage, + :status => status} + end + status_percentages.sort! do |a, b| + (-1) * (a[:percentage] <=> b[:percentage]) + end + status_percentages = {} + status_percentages.each do |status| + status_percentages[status[:status]] = status[:percentage] + end + status_percentages + end + end + end +end Modified: lib/droonga/benchmark/runner.rb (+2 -172) =================================================================== --- lib/droonga/benchmark/runner.rb 2013-10-10 18:31:53 +0900 (3fe68da) +++ lib/droonga/benchmark/runner.rb 2013-10-11 10:23:44 +0900 (8ae2665) @@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- -require "thread" -require "net/http" -require "json" +require "droonga/benchmark/client/http" +require "droonga/benchmark/result" module Droonga module Benchmark @@ -108,175 +107,6 @@ module Droonga @requests << base_patterns[count % base_patterns.size] end end - - class HttpClient - attr_reader :requests, :results, :wait - - DEFAULT_PATH = "/" - DEFAULT_METHOD = "GET" - - def initialize(params) - @requests = params[:requests] - @result = params[:result] - @wait = params[:wait] - - @default_host = params[:host] - @default_port = params[:port] - @default_path = params[:path] || DEFAULT_PATH - @default_method = params[:method] || DEFAULT_METHOD - end - - def run - @thread = Thread.new do - loop do - request =****@reque***** - request[:host] ||= @default_host - request[:port] ||= @default_port - request[:path] ||= @default_path - request[:method] ||= @default_method - request[:method] = request[:method].upcase - - Net::HTTP.start(request[:host], request[:port]) do |http| - header = { - "user-agent" => "Ruby/#{RUBY_VERSION} Droonga::Benchmark::Runner::HttpClient" - } - response = nil - start_time = Time.now - case request[:method] - when "GET" - response = http.get(request[:path], header) - when "POST" - body = request[:body] - unless body.is_a?(String) - body = JSON.generate(body) - end - response = http.post(request[:path], body, header) - end - @result << { - :request => request, - :status => response.code, - :elapsed_time => Time.now - start_time, - } - end - sleep @wait - end - end - self - end - - def stop - @thread.exit - end - end - - class Result - attr_reader :n_clients, :duration, :statuses - - class << self - def keys - [ - :n_clients, - :total_n_requests, - :queries_per_second, - :min_elapsed_time, - :max_elapsed_time, - :average_elapsed_time, - ] - end - end - - def initialize(params) - @n_clients = params[:n_clients] - @duration = params[:duration] - - @results = [] - @total_elapsed_time = 0.0 - @elapsed_times = [] - @statuses = {} - end - - def <<(result) - clear_cached_statistics - - @results << result - - @statuses[result[:status]] ||= 0 - @statuses[result[:status]] += 1 - - @elapsed_times << result[:elapsed_time] - @total_elapsed_time += result[:elapsed_time] - end - - def total_n_requests - @total_n_requests ||=****@resul***** - end - - def queries_per_second - @queries_per_second ||= total_n_requests.to_f / @duration - end - - def status_percentages - @status_percentages ||= prepare_status_percentages - end - - def min_elapsed_time - @min_elapsed_time ||= @elapsed_times.min - end - - def max_elapsed_time - @max_elapsed_time ||= @elapsed_times.min - end - - def average_elapsed_time - @average_elapsed_time ||= @total_elapsed_time / @elapsed_times.size - end - - def to_s - "Total requests: #{total_n_requests} " + - "(#{queries_per_second} queries per second)\n" + - "Status:\n" + - status_percentages.collect do |status, percentage| - " #{status}: #{percentage} %" - end.join("\n") + "\n" + - "Elapsed time:\n" + - " min: #{min_elapsed_time} sec\n" + - " max: #{max_elapsed_time} sec\n" + - " average: #{average_elapsed_time} sec" - end - - def values - self.class.keys.collect do |column| - send(column) - end - end - - private - def clear_cached_statistics - @total_n_requests = nil - @queries_per_second = nil - @status_percentages = nil - @min_elapsed_time = nil - @max_elapsed_time = nil - @average_elapsed_time = nil - end - - def prepare_status_percentages - status_percentages = [] - @statuses.each do |status, n_results| - percentage = n_results.to_f / total_n_requests * 100 - status_percentages << {:percentage => percentage, - :status => status} - end - status_percentages.sort! do |a, b| - (-1) * (a[:percentage] <=> b[:percentage]) - end - status_percentages = {} - status_percentages.each do |status| - status_percentages[status[:status]] = status[:percentage] - end - status_percentages - end - end end end end -------------- next part -------------- HTML����������������������������...Download