exe/benchmark-driver in benchmark_driver-0.3.0 vs exe/benchmark-driver in benchmark_driver-0.4.0

- old
+ new

@@ -1,41 +1,67 @@ #!/usr/bin/env ruby $:.unshift File.expand_path('../lib', __dir__) -require 'benchmark_driver' +require 'benchmark/driver' +require 'benchmark/driver/yaml_parser' require 'optparse' require 'yaml' options = {} args = OptionParser.new do |o| o.banner = "Usage: #{File.basename($0, '.*')} [options] [YAML]" o.on('-e', '--executables [EXECS]', 'Ruby executables (e1::path1; e2::path2; e3::path3;...)') do |e| options[:execs] ||= [] - e.split(/;/).each do |path| - options[:execs] << path + e.split(';').each do |name_path| + name, path = name_path.split('::', 2) + options[:execs] << Benchmark::Driver::Configuration::Executable.new(name, path || name) end end - o.on('-r', '--rbenv [VERSION]', 'Ruby executable in rbenv') do |r| + o.on('--rbenv [VERSIONS]', 'Ruby executables in rbenv (2.3.5;2.4.2;...)') do |r| options[:execs] ||= [] - r.split(/;/).each do |version| - options[:execs] << "#{version}::#{`RBENV_VERSION='#{version}' rbenv which ruby`.rstrip}" + r.split(';').each do |version| + 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) end end - o.on('-i [DURATION]', '--ips [SECONDS]', "Measure IPS in duration seconds (default: #{Benchmark::Driver::DEFAULT_IPS_DURATION})") do |i| - options[:measure_type] = 'ips' - options[:measure_num] = Integer(i) if i + o.on('-c', '--compare', 'Compare results (currently only supported in ips output)') do |v| + options[:compare] = v end - o.on('-l [COUNT]', '--loop-count [COUNT]', "Measure execution time with loop count (default: #{Benchmark::Driver::DEFAULT_LOOP_COUNT})") do |l| - options[:measure_type] = 'loop_count' - options[:measure_num] = Integer(l) if l + o.on('-r', '--repeat-count [NUM]', 'Try benchmark NUM times and use the fastest result') do |v| + begin + options[:repeat_count] = Integer(v) + rescue ArgumentError + abort "-r, --repeat-count must take Integer, but got #{v.inspect}" + end end - o.on('-v', '--verbose') do |v| - options[:verbose] = v - end end.parse!(ARGV) abort "No YAML file is specified" if args.empty? -driver = Benchmark::Driver.new(options) args.each do |yaml| - default = { name: File.basename(yaml, '.*') } - driver.run(default.merge(YAML.load(File.read(yaml)))) + yaml = YAML.load(File.read(yaml)) + Benchmark::Driver::Configuration.symbolize_keys!(yaml) + + begin + config = 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 + + options.each do |key, value| + case key + when :compare + config.output_options.compare = value + when :execs + config.runner_options.executables = value + when :repeat_count + config.runner_options.repeat_count = value + else + raise "Unhandled option: #{key.inspect}" + end + end + + Benchmark::Driver.run(config) end