lib/mp_perf.rb in stresser-0.3.2 vs lib/mp_perf.rb in stresser-0.4.0

- old
+ new

@@ -1,38 +1,44 @@ require 'optparse' require 'ruport' require 'httperf' +require 'trollop' + # # Takes command line options and attempts to make a benchmark. # class MPPerf def initialize(opts = {}) - @conf = {} - OptionParser.new do |opts| - opts.banner = "Usage: stresser -c your.conf -o output.csv" - opts.on("-o", "--output FILE", String, "This file will be overwritten with a detailed report of the stresstest") do |v| - @output_file = v - end - opts.on( "-c", "--config FILE", String, "Your stresser configuration file with stresstest options and parameters directly passed to httperf") do |v| - @conf = parse_config(v) - end - end.parse! + parse_options + parse_config + run_suite + display_hints + end - run() + # + # Parse command line options + # + def parse_options + @conf = Trollop::options do + banner = MPPerf.options_banner + opt :output_file, "The name of the csv file to write the results to. Warning: overwrites that file!", + :type => String + opt :config_file, "The name of the .conf file defining your testsuite. See http://github.com/moviepilot/stresser", + :type => String + end - puts "~"*80 - puts "Great, now create a graph with" - puts " stresser-grapher -o #{File.expand_path(File.dirname(@output_file))} #{@output_file}" - puts "" + Trollop::die :output_file, "must be a writeable file" unless @conf[:config_file] + Trollop::die :config_file, "must be a readable file" unless @conf[:config_file] end # # Taken from http://github.com/igrigorik/autoperf # - def parse_config(config_file) + def parse_config + config_file = @conf[:config_file] raise Errno::EACCES, "#{config_file} is not readable" unless File.readable?(config_file) conf = {} open(config_file).each { |line| line.chomp @@ -54,11 +60,11 @@ conf[var_name] = new_value =~ /^\d+$/ ? new_value.to_i : new_value end end } - return conf + @conf.merge! conf end # # Runs a single benchmark (this method will be called many times # with different concurrency levels) @@ -69,11 +75,11 @@ res = Httperf.run(conf) return res end - def run + def run_suite results = {} report = nil (@conf['low_rate']..@conf['high_rate']).step(@conf['rate_step']) do |rate| # Run httperf @@ -92,11 +98,25 @@ # Try to keep old pending requests from influencing the next round sleep(@conf['sleep_time'] || 0) end # Write csv - File.new(@output_file, "w").puts report.to_csv unless report.nil? + File.new(@conf[:output_file], "w").puts report.to_csv unless report.nil? end -end + + def display_hints + puts "~"*80 + puts "Great, now create a graph with" + puts " stresser-grapher -o #{File.expand_path(File.dirname(@conf[:output_file]))} #{@conf[:output_file]}" + puts "" + end + def self.options_banner + <<BANNER +Runs a stresstest defined in a config file and write the results to a csv file. - +Usage: + stresser -c my_config_file -o results.csv +where options are: +BANNER + end +end