exe/benchmark-driver in benchmark_driver-0.5.1 vs exe/benchmark-driver in benchmark_driver-0.6.0

- old
+ new

@@ -1,14 +1,18 @@ #!/usr/bin/env ruby $:.unshift File.expand_path('../lib', __dir__) require 'benchmark/driver' require 'benchmark/driver/bundle_installer' +require 'benchmark/driver/configuration' require 'benchmark/driver/yaml_parser' require 'optparse' require 'yaml' +# +# Parse command line options +# options = {} parser = OptionParser.new do |o| o.banner = "Usage: #{File.basename($0, '.*')} [options] [YAML]" o.on('-e', '--executables [EXECS]', 'Ruby executables (e1::path1,arg1,...; e2::path2,arg2;...)') do |e| abort '-e, --executable must take argument but not given' if e.nil? @@ -26,10 +30,14 @@ path = `RBENV_VERSION='#{version}' rbenv which ruby`.rstrip abort "Failed to execute 'rbenv which ruby'" unless $?.success? options[:execs] << Benchmark::Driver::Configuration::Executable.new(version, [path, *args]) end end + o.on('-o', '--output [TYPE]', 'Specify output type (ips, time, memory)') do |t| + abort '-o, --output must take argument but not given' if t.nil? + options[:output] = t + end o.on('-c', '--compare', 'Compare results (currently only supported in ips output)') do |v| options[:compare] = v end o.on('-r', '--repeat-count [NUM]', 'Try benchmark NUM times and use the fastest result') do |v| begin @@ -52,53 +60,63 @@ args = parser.parse!(ARGV) if args.empty? abort "No YAML file is specified!\n\n#{parser.help}" end -args.each do |path| +# +# Parse benchmark definitions +# +jobs = args.flat_map do |path| yaml = YAML.load(File.read(path)) Benchmark::Driver::Configuration.symbolize_keys!(yaml) begin - config = Benchmark::Driver::YamlParser.parse(yaml) + Benchmark::Driver::YamlParser.parse(yaml) rescue ArgumentError $stderr.puts "benchmark-driver: Failed to parse #{yaml.dump}." $stderr.puts ' YAML format may be wrong. See error below:' $stderr.puts raise end +end - opts = options.dup +# +# Proceed parsed options +# +config = Benchmark::Driver::Configuration.new(jobs) +config.runner_options = Benchmark::Driver::Configuration::RunnerOptions.new(:exec) +config.output_options = Benchmark::Driver::Configuration::OutputOptions.new(:ips) +if options.key?(:execs) # Proceed execs first for --bundler - if opts.key?(:execs) - config.runner_options.executables = opts.delete(:execs) - end + config.runner_options.executables = options.delete(:execs) +end - opts.each do |key, value| - case key - when :bundler - config.runner_options.executables.each do |executable| - Benchmark::Driver::BundleInstaller.bundle_install_for(executable) - executable.command << '-rbundler/setup' - end - when :compare - config.output_options.compare = value - when :dir - dir = File.dirname(path) - config.jobs.each do |job| - job.prelude = "__dir__ = #{dir.dump}.freeze; #{job.prelude}" - end - when :filter - filter = Regexp.compile(value) - config.jobs.select! do |job| - job.name.match(filter) - end - when :repeat_count - config.runner_options.repeat_count = value - else - raise "Unhandled option: #{key.inspect}" +options.each do |key, value| + case key + when :bundler + config.runner_options.executables.each do |executable| + Benchmark::Driver::BundleInstaller.bundle_install_for(executable) + executable.command << '-rbundler/setup' end + when :compare + config.output_options.compare = value + when :dir + dir = File.dirname(path) + config.jobs.each do |job| + job.prelude = "__dir__ = #{dir.dump}.freeze; #{job.prelude}" + end + when :filter + filter = Regexp.compile(value) + config.jobs.select! do |job| + job.name.match(filter) + end + when :output + config.output_options.type = value.to_sym + when :repeat_count + config.runner_options.repeat_count = value + else + raise "Unhandled option: #{key.inspect}" end - - Benchmark::Driver.run(config) end + +Benchmark::Driver.run(config)