Kenji Okimoto
okimo****@clear*****
Wed Oct 9 16:31:58 JST 2013
> + def csv_body > + @results.values.collect do |result| > + (result.values + > + response_statuses.collect do |status| > + result.response_statuses[status] || 0 > + end).join(",") > + end.join("\n") > + end > + end ここは csv ライブラリを使うとすっきり書けそうな気がしました。 http://doc.ruby-lang.org/ja/2.0.0/class/CSV.html CSV.generate do |csv| ... end On 2013年10月09日 16:17, YUKI Hiroshi wrote: > Author > YUKI Hiroshi <shimoda �� clear-code.com> > Date > 2013-10-09 16:17:44 +0900 (Wed, 09 Oct 2013) > New Revision > 066575113d6def75ee37c5554292a016393848fd > <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd> > Message > > Implement GradualResult as a class > > Modified files > > * lib/droonga/http-benchmark/formatter.rb > <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#diff-0> > * lib/droonga/http-benchmark/gradual-runner.rb > <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#diff-1> > * lib/droonga/http-benchmark/runner.rb > <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#diff-2> > > Modified: lib/droonga/http-benchmark/formatter.rb (+0 -30) > =================================================================== > > @@ -4,36 +4,6 @@module Droonga > module HttpBenchmark > class Formatter > class << self > - def output_gradual_results(results) > - http_statuses = [] > - results.each do |n_clients, result| > - http_statuses += result[:responses].keys > - end > - http_statuses.uniq! > - http_statuses.sort! > - > - puts "n_clients,total_n_requests,queries_per_second," + > - "#{http_statuses.join(",")}," + > - "min_elapsed_time,max_elapsed_time,average_elapsed_time" > - results.each do |n_clients, result| > - result[:n_clients] = n_clients > - response_statuses = http_statuses.collect do |status| > - if result[:responses].include?(status) > - result[:responses][status] > - else > - 0 > - end > - end > - result[:response_statuses] = response_statuses.join(",") > - puts(("%{n_clients}," + > - "%{total_n_requests}," + > - "%{queries_per_second}," + > - "%{response_statuses}," + > - "%{min_elapsed_time}," + > - "%{max_elapsed_time}," + > - "%{average_elapsed_time}") % result) > - end > - end > end > end > end > > Modified: lib/droonga/http-benchmark/gradual-runner.rb (+46 -3) > =================================================================== > > @@ -20,18 +20,61 @@module Droonga > > def run > run_benchmarks > - Formatter.output_gradual_results(@results) > + puts****@resul*****_csv > end > > private > def run_benchmarks > - @results = {} > + @result = GradualResult.new > @start_n_clients.step(@end_n_clients, @step) do |n_clients| > benchmark = Runner.new(@params.merge(:n_clients => n_clients)) > puts "Running benchmark with #{n_clients} clients..." > - @results[n_clients] = benchmark.run > + @result << benchmark.run > end > end > end > + > + class GradualResult > + def initialize > + @results = {} > + end > + > + def <<(result) > + @response_statuses = nil > + @results[result.n_clients] = result > + end > + > + def response_statuses > + @response_statuses ||= prepare_response_statuses > + end > + > + def to_csv > + "#{csv_header}\n#{csv_body}" > + end > + > + private > + def prepare_response_statuses > + response_statuses = [] > + @results.each do |n_clients, result| > + response_statuses += result.response_statuses.keys > + end > + response_statuses.uniq! > + response_statuses.sort! > + response_statuses > + end > + > + def csv_header > + (Result.keys + response_statuses).join(",") > + end > + > + def csv_body > + @results.values.collect do |result| > + (result.values + > + response_statuses.collect do |status| > + result.response_statuses[status] || 0 > + end).join(",") > + end.join("\n") > + end > + end > end > end > > Modified: lib/droonga/http-benchmark/runner.rb (+24 -1) > =================================================================== > > ... <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L50> > 51 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L51> > 52 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L52> > 53 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L53> > 54 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L54> > > > 55 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L55> > 56 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L56> > 57 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L57> > ... <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L163> > 164 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L164> > 165 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L165> > 166 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L166> > > > > > > > > > > > > > > > > 167 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L167> > > 168 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L168> > 169 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L169> > 170 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L170> > ... <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L221> > 222 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L222> > 223 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L223> > 224 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L224> > > > > > > > 225 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L225> > 226 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L226> > 227 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2L227> > > > > ... <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R50> > 51 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R51> > 52 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R52> > 53 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R53> > > 54 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R54> > 55 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R55> > 56 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R56> > 57 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R57> > 58 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R58> > ... <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R164> > 165 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R165> > 166 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R166> > 167 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R167> > 168 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R168> > 169 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R169> > 170 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R170> > 171 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R171> > 172 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R172> > 173 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R173> > 174 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R174> > 175 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R175> > 176 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R176> > 177 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R177> > 178 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R178> > 179 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R179> > 180 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R180> > 181 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R181> > 182 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R182> > 183 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R183> > 184 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R184> > 185 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R185> > 186 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R186> > 187 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R187> > ... <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R238> > 239 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R239> > 240 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R240> > 241 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R241> > 242 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R242> > 243 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R243> > 244 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R244> > 245 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R245> > 246 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R246> > 247 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R247> > 248 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R248> > 249 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R249> > 250 <https://github.com/droonga/http-benchmark/commit/066575113d6def75ee37c5554292a016393848fd#L2R250> > > > > @@ -51,7 +51,8 @@module Droonga > private > def process_requests > requests_queue = Queue.new > - @result = Result.new(:duration => @duration) > + @result = Result.new(:n_clients => @n_clients, > + :duration => @duration) > > @clients = @n_clients.times.collect do |index| > client = Client.new(:requests => requests_queue, > @@ -164,7 +165,23 @@module Droonga > end > > class Result > + attr_reader :n_clients, :duration, :response_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 = [] > @@ -222,6 +239,12 @@module Droonga > " 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 > > > > _______________________________________________ > Groonga-commit mailing list > Groonga-commit �� lists.sourceforge.jp > http://lists.sourceforge.jp/mailman/listinfo/groonga-commit > -- Kenji Okimoto <okimoto �� clear-code.com>