YUKI Hiroshi
null+****@clear*****
Fri Jan 17 16:29:28 JST 2014
YUKI Hiroshi 2014-01-17 16:29:28 +0900 (Fri, 17 Jan 2014) New Revision: 8b2bd7b82e7aaf37031ae5936f7a95275a75881a https://github.com/droonga/drnbench/commit/8b2bd7b82e7aaf37031ae5936f7a95275a75881a Message: Use configuration obejct instead of a large hash Added files: lib/drnbench/shuttle/configuration.rb Modified files: bin/drnbench-shuttle lib/drnbench.rb lib/drnbench/client/http-droonga.rb lib/drnbench/client/http.rb lib/drnbench/shuttle/gradual-runner.rb lib/drnbench/shuttle/runner.rb Modified: bin/drnbench-shuttle (+17 -38) =================================================================== --- bin/drnbench-shuttle 2014-01-17 10:50:33 +0900 (352bc68) +++ bin/drnbench-shuttle 2014-01-17 16:29:28 +0900 (5069109) @@ -5,38 +5,27 @@ require "drnbench" require "optparse" require "json" -options = { - :start_n_clients => 1, - :end_n_clients => 1, - :step => 1, - :mode => :http, - :default_host => "localhost", - :default_port => 80, - :default_path => "/", - :default_method => "GET", - :report_progressively => true, - :output_path => "/tmp/drnbench-result.csv", -} +config = Drnbench::Shuttle::Configuration.new option_parser = OptionParser.new do |parser| parser.on("--duration=SECONDS", Float, "duration of the benmark") do |duration| - options[:duration] = duration + config.duration = duration end parser.on("--wait=SECONDS", Float, "wait for each request") do |wait| - options[:wait] = wait + config.wait = wait end parser.on("--start-n-clients=N", Integer, "initial number of clients (optional)") do |n_clients| - options[:start_n_clients] = n_clients + config.start_n_clients = n_clients end parser.on("--end-n-clients=N", Integer, "final number of clients (optional)") do |n_clients| - options[:end_n_clients] = n_clients + config.end_n_clients = n_clients end parser.on("--step=COUNT", Integer, "step to increase number of clients (optional)") do |step| - options[:step] = step + config.step = step end parser.on("--mode=MODE", String, @@ -44,53 +33,43 @@ option_parser = OptionParser.new do |parser| "available modes:", " http", " http-droonga-search") do |mode| - options[:mode] = mode.gsub(/-/, "_") + config.mode = mode.gsub(/-/, "_") end parser.on("--request-patterns-file=PATH", "path to request patterns JSON file") do |path| - options[:request_patterns_file] = File.expand_path(path) + config.request_patterns_file = File.expand_path(path) end parser.on("--default-host=HOST", String, "default host name (optional)") do |host| - options[:default_host] = host + config.default_host = host end parser.on("--default-port=PORT", Integer, "default port number (optional)") do |port| - options[:default_port] = port + config.default_port = port end parser.on("--default-path=PATH", String, "default path (optional)") do |path| - options[:default_path] = path + config.default_path = path end parser.on("--default-method=METHOD", String, "default HTTP method (optional)") do |method| - options[:default_method] = method + config.default_method = method end parser.on("--output-path=PATH", "path to output statistics as a CSV file (optional)") do |path| - options[:output_path] = File.expand_path(path) + config.output_path = File.expand_path(path) end end args = option_parser.parse!(ARGV) -if options[:duration].nil? - raise "You must specify the test duration by --duration option." -end -if options[:request_patterns_file].nil? - raise "You must specify the path to the request patterns JSON file " + - "by --request-patterns-file option." -end - -request_patterns = File.read(options[:request_patterns_file]) -request_patterns = JSON.parse(request_patterns, :symbolize_names => true) -options[:request_patterns] = request_patterns +config.validate -runner = Drnbench::Shuttle::GradualRunner.new(options) +runner = Drnbench::Shuttle::GradualRunner.new(config) runner.run -File.open(options[:output_path], "w") do |file| +File.open(config.output_path, "w") do |file| file.puts runner.result.to_csv end -puts "Statistics has been saved to #{options[:output_path]}" +puts "Statistics has been saved to #{config.output_path}" Modified: lib/drnbench.rb (+1 -0) =================================================================== --- lib/drnbench.rb 2014-01-17 10:50:33 +0900 (80c74d1) +++ lib/drnbench.rb 2014-01-17 16:29:28 +0900 (3b9290b) @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- require "drnbench/version" +require "drnbench/shuttle/configuration" require "drnbench/shuttle/runner" require "drnbench/shuttle/gradual-runner" Modified: lib/drnbench/client/http-droonga.rb (+12 -6) =================================================================== --- lib/drnbench/client/http-droonga.rb 2014-01-17 10:50:33 +0900 (fe99047) +++ lib/drnbench/client/http-droonga.rb 2014-01-17 16:29:28 +0900 (c7aed17) @@ -3,13 +3,13 @@ module Drnbench class HttpDroongaClient < HttpClient DEFAULT_PATH_BASE = "/droonga" - DEFAULT_COMMAND = "search" - DEFAULT_METHOD = "POST" + DEFAULT_COMMAND = "search" + DEFAULT_METHOD = "POST" - def initialize(params) - @command = params[:command] || DEFAULT_COMMAND - params[:requests] = populate_http_requests(params[:requests]) + def initialize(params, config) super + @command = params[:command] || DEFAULT_COMMAND + @requests = populate_http_requests(@requests) end private @@ -19,9 +19,15 @@ module Drnbench :body => { :queries => queries, }, - :path => "#{DEFAULT_PATH_BASE}/#{@command}", } end end + + private + def fixup_request(request) + reqyest[:path] ||= "#{DEFAULT_PATH_BASE}/#{@command}" + request[:method] ||= DEFAULT_METHOD + super + end end end Modified: lib/drnbench/client/http.rb (+15 -14) =================================================================== --- lib/drnbench/client/http.rb 2014-01-17 10:50:33 +0900 (72495af) +++ lib/drnbench/client/http.rb 2014-01-17 16:29:28 +0900 (f3fc296) @@ -8,26 +8,17 @@ module Drnbench class HttpClient attr_reader :requests, :results, :wait - def initialize(params) + def initialize(params, config) @requests = params[:requests] - @result = params[:result] - @wait = params[:wait] - - @default_host = params[:default_host] - @default_port = params[:default_port] - @default_path = params[:default_path] - @default_method = params[:default_method] + @result = params[:result] + @config = config 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 + fixup_request(request) Net::HTTP.start(request[:host], request[:port]) do |http| header = { @@ -51,7 +42,7 @@ module Drnbench :elapsed_time => Time.now - start_time, } end - sleep @wait + sleep****@confi***** end end self @@ -60,5 +51,15 @@ module Drnbench def stop @thread.exit end + + private + def fixup_request(request) + request[:host] ||=****@confi*****_host + request[:port] ||=****@confi*****_port + request[:path] ||=****@confi*****_path + request[:method] ||=****@confi*****_method + request[:method] = request[:method].upcase + request + end end end Added: lib/drnbench/shuttle/configuration.rb (+66 -0) 100644 =================================================================== --- /dev/null +++ lib/drnbench/shuttle/configuration.rb 2014-01-17 16:29:28 +0900 (34362a8) @@ -0,0 +1,66 @@ +module Drnbench + module Shuttle + class Configuration + attr_accessor :duration, :wait, :request_patterns_file + attr_accessor :start_n_clients, :end_n_clients, :step, :n_requests + attr_accessor :mode + attr_accessor :default_host, :default_port, :default_path, :default_method + attr_accessor :report_progressively, :output_path + + MIN_DURATION = 1 + MIN_WAIT = 0 + + def initialize + @wait = 1 + @start_n_clients = 1 + @end_n_clients = 1 + @step = 1 + @n_requests = 1000 + @mode = :http + + @default_host = "localhost" + @default_port = 80 + @default_path = "/" + @default_method = "GET" + + @report_progressively = true + @output_path = "/tmp/drnbench-result.csv" + end + + def validate + if****@durat*****? + raise ArgumentError.new("You must specify the test duration.") + end + if @request_patterns_file.nil? + raise ArgumentError.new("You must specify the path to the request patterns JSON file.") + end + end + + def duration + [@duration, MIN_DURATION].max + end + + def wait + [@wait, MIN_WAIT].max + end + + def request_patterns + @request_patterns ||= prepare_request_patterns + end + + private + def prepare_request_patterns + request_patterns = File.read(@request_patterns_file) + request_patterns = JSON.parse(request_patterns, :symbolize_names => true) + + + if params[:request_pattern] + params[:request_pattern][:frequency] = 1 + @request_patterns = [params[:request_pattern]] + else + @request_patterns = params[:request_patterns] + end + end + end + end +end Modified: lib/drnbench/shuttle/gradual-runner.rb (+6 -15) =================================================================== --- lib/drnbench/shuttle/gradual-runner.rb 2014-01-17 10:50:33 +0900 (50cf827) +++ lib/drnbench/shuttle/gradual-runner.rb 2014-01-17 16:29:28 +0900 (30b641a) @@ -6,19 +6,10 @@ require "csv" module Drnbench module Shuttle class GradualRunner - attr_reader :start_n_clients, :end_n_clients, :step attr_reader :report_progressively, :result - DEFAULT_START_N_CLIENTS = 1 - DEFAULT_END_N_CLIENTS = 1 - DEFAULT_STEP = 1 - - def initialize(params) - @start_n_clients = params[:start_n_clients] || DEFAULT_START_N_CLIENTS - @end_n_clients = params[:end_n_clients] || DEFAULT_END_N_CLIENTS - @step = params[:step] || DEFAULT_STEP - @report_progressively = params[:report_progressively] || false - @params = params + def initialize(config) + @config = config end def run @@ -29,13 +20,13 @@ module Drnbench private def run_benchmarks @result = Result.new - @start_n_clients.step(@end_n_clients, @step) do |n_clients| - benchmark = Runner.new(@params.merge(:n_clients => n_clients)) - if @report_progressively + @config.start_n_clients.step(@config.end_n_clients, @config.step) do |n_clients| + benchmark = Runner.new(n_clients, @config) + if****@confi*****_progressively puts "Running benchmark with #{n_clients} clients..." end benchmark.run - if @report_progressively + if****@confi*****_progressively puts benchmark.result.to_s end @result << benchmark.result Modified: lib/drnbench/shuttle/runner.rb (+17 -35) =================================================================== --- lib/drnbench/shuttle/runner.rb 2014-01-17 10:50:33 +0900 (b9fe6cc) +++ lib/drnbench/shuttle/runner.rb 2014-01-17 16:29:28 +0900 (38bf07c) @@ -7,31 +7,11 @@ require "drnbench/shuttle/result" module Drnbench module Shuttle class Runner - attr_reader :duration, :n_clients, :result + attr_reader :n_clients, :result - MIN_DURATION = 1 - DEFAULT_DURATION = 10 - MIN_WAIT = 0 - DEFAULT_WAIT = 1 - DEFAULT_N_CLIENTS = 1 - TOTAL_N_REQUESTS = 1000 - - def initialize(params) - @duration = [params[:duration] || DEFAULT_DURATION, MIN_DURATION].max - @n_clients = params[:n_clients] || DEFAULT_N_CLIENTS - @n_requests = params[:n_requests] || TOTAL_N_REQUESTS - - params[:wait] ||= DEFAULT_WAIT - params[:wait] = [params[:wait], MIN_WAIT].max - - @params = params - - if params[:request_pattern] - params[:request_pattern][:frequency] = 1 - @request_patterns = [params[:request_pattern]] - else - @request_patterns = params[:request_patterns] - end + def initialize(n_clients, config) + @n_clients = n_clients + @config = config populate_requests end @@ -44,24 +24,26 @@ module Drnbench def process_requests requests_queue = Queue.new @result = Result.new(:n_clients => @n_clients, - :duration => @duration) + :duration => @config.duration) - client_params =****@param*****(:requests => requests_queue, - :result => @result) + client_params = { + :requests => requests_queue, + :result => @result, + } @clients = @n_clients.times.collect do |index| client = nil - case @params[:mode] + case****@confi***** when :http - client = HttpClient.new(client_params) + client = HttpClient.new(client_params, @config) when :http_droonga - client = HttpDroongaClient.new(client_params) + client = HttpDroongaClient.new(client_params, @config) end client.run client end start_time = Time.now - while Time.now - start_time < @duration + while Time.now - start_time < @config.duration if requests_queue.empty? @requests.each do |request| requests_queue.push(request) @@ -80,12 +62,12 @@ module Drnbench def populate_requests @requests = [] - if @request_patterns.is_a?(Array) - @request_patterns.each do |request_pattern| + if****@confi*****_patterns.is_a?(Array) + @config.request_patterns.each do |request_pattern| populate_request_pattern(request_pattern) end else - @request_patterns.each do |key, request_pattern| + @config.request_patterns.each do |key, request_pattern| populate_request_pattern(request_pattern) end end @@ -95,7 +77,7 @@ module Drnbench def populate_request_pattern(request_pattern) frequency = request_pattern[:frequency].to_f - n_requests = @n_requests * frequency + n_requests =****@confi*****_requests * frequency base_patterns = nil if request_pattern[:pattern] -------------- next part -------------- HTML����������������������������...Descargar