summaryrefslogtreecommitdiff
path: root/benchmark/driver.rb
blob: 2a4e3a6fa58f1b1dbdb954279d92cc0c2d1db989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env ruby
#
# Wrapper of benchmark-driver command for `make benchmark` and `make benchmark-each`.
#

begin
  require 'optparse'
rescue LoadError
  $:.unshift File.join(File.dirname(__FILE__), '../lib')
  require 'optparse'
end

require 'shellwords'

class BenchmarkDriver
  # Run benchmark-driver prepared by `make update-benchmark-driver`
  def self.run(*args)
    benchmark_driver = File.expand_path('./benchmark-driver/exe/benchmark-driver', __dir__)
    command = [benchmark_driver, *args]
    unless system(command.shelljoin)
      abort "Failed to execute: #{command.shelljoin}"
    end
  end

  def initialize(opt = {})
    @dir = opt[:dir]
    @pattern = opt[:pattern]
    @exclude = opt[:exclude]
  end

  def files
    Dir.glob(File.join(@dir, '*.yml')).map{|file|
      next if @pattern && /#{@pattern}/ !~ File.basename(file)
      next if @exclude && /#{@exclude}/ =~ File.basename(file)
      file
    }.compact.sort
  end
end

if __FILE__ == $0
  opt = {
    runner: 'ips',
    output: 'compare',
    execs: [],
    rbenvs: [],
    repeat: 1,
    verbose: 1,
    dir: File.dirname(__FILE__),
  }

  parser = OptionParser.new{|o|
    #
    # Original benchmark-driver imitation
    #
    o.on('-r', '--runner [TYPE]', 'Specify runner type: ips, time, memory, once (default: ips)'){|r|
      abort '-r, --runner must take argument but not given' if r.nil?
      opt[:runner] = r
    }
    o.on('-o', '--output [TYPE]', 'Specify output type: compare, simple, markdown, record (default: compare)'){|o|
      abort '-o, --output must take argument but not given' if o.nil?
      opt[:output] = o
    }
    o.on('-e', '--executables [EXECS]',
      "Specify benchmark one or more targets (e1::path1; e2::path2; e3::path3;...)"){|e|
       e.split(/;/).each{|path|
         opt[:execs] << path
       }
    }
    o.on('--rbenv [VERSIONS]', 'Specify benchmark targets with rbenv version (vX.X.X;vX.X.X;...)'){|v|
      opt[:rbenvs] << v
    }
    o.on('--repeat-count [NUM]', "Repeat count"){|n|
      opt[:repeat] = n.to_i
    }
    o.on('--verbose [LEVEL]', 'Show some verbose outputs: 0, 1, 2 (default: 1)'){|v|
      opt[:verbose] = Integer(v)
    }

    #
    # benchmark/driver.rb original (deprecated, to be removed later)
    #
    o.on('--directory [DIRECTORY]', "Benchmark suites directory"){|d|
      opt[:dir] = d
    }
    o.on('--pattern [PATTERN]', "Benchmark name pattern"){|p|
      opt[:pattern] = p
    }
    o.on('--exclude [PATTERN]', "Benchmark exclude pattern"){|e|
      opt[:exclude] = e
    }
  }

  yamls = parser.parse!(ARGV)
  yamls += BenchmarkDriver.new(opt).files

  # Many variables in Makefile are not `foo,bar` but `foo bar`. So it's converted here.
  execs = opt[:execs].map { |exec| ['--executables', exec.shellsplit.join(',')] }.flatten
  rbenv = opt[:rbenvs].map { |r| ['--rbenv', r] }

  BenchmarkDriver.run(
    *yamls, *execs, *rbenv,
    "--runner=#{opt[:runner]}", "--output=#{opt[:output]}",
    "--verbose=#{opt[:verbose]}", "--repeat-count=#{opt[:repeat]}",
  )
end