Class: Benchkit

Inherits:
Object
  • Object
show all
Defined in:
lib/benchkit.rb,
lib/benchkit/version.rb

Defined Under Namespace

Modules: IpsReporter, LoopCountReporter Classes: BenchmarkMetrics, BenchmarkResult, BenchmarkRoot, BenchmarkScript, Executable

Constant Summary collapse

MEASURE_TYPES =
%w[loop_count ips]
DEFAULT_LOOP_COUNT =
100_000
DEFAULT_IPS_DURATION =
1
VERSION =
'0.2.0'

Instance Method Summary collapse

Constructor Details

#initialize(measure_type: 'loop_count', measure_num: nil, execs: ['ruby'], verbose: false) ⇒ Benchkit

Returns a new instance of Benchkit.

Parameters:

  • measure_type (String) (defaults to: 'loop_count')
    • “loop_count”|“ips”

  • measure_num (Integer, nil) (defaults to: nil)
    • Loop count for “loop_type”, duration seconds for “ips”

  • execs (Array<String>) (defaults to: ['ruby'])
    • “path1”, “path2”

      or ‘[“ruby1::path1”, “ruby2::path2”]`

  • verbose (Boolean) (defaults to: false)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/benchkit.rb', line 14

def initialize(measure_type: 'loop_count', measure_num: nil, execs: ['ruby'], verbose: false)
  unless MEASURE_TYPES.include?(measure_type)
    abort "unsupported measure type: #{measure_type.dump}"
  end
  @measure_type = measure_type
  @measure_num = measure_num
  @execs = execs.map do |exec|
    name, path = exec.split('::', 2)
    Executable.new(name, path || name)
  end
  @verbose = verbose
end

Instance Method Details

#run(root_hash) ⇒ Object

Parameters:

  • root_hash (Hash)


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
# File 'lib/benchkit.rb', line 28

def run(root_hash)
  root = BenchmarkRoot.new(Hash[root_hash.map { |k, v| [k.to_sym, v] }])

  results = root.benchmarks.map do |benchmark|
    metrics_by_exec = {}
    iterations = calc_iterations(@execs.first, benchmark)
    @execs.each do |exec|
      if @verbose
        puts "--- Running #{benchmark.name.dump} with #{exec.name.dump} #{iterations} times ---"
        puts "#{benchmark.benchmark_script(iterations)}\n"
      end
      elapsed_time = run_benchmark(exec, benchmark, iterations)
      metrics_by_exec[exec] = BenchmarkMetrics.new(iterations, elapsed_time)
    end
    BenchmarkResult.new(benchmark.name, metrics_by_exec)
  end
  puts if @verbose

  case @measure_type
  when 'loop_count'
    LoopCountReporter.report(@execs, results)
  when 'ips'
    IpsReporter.report(@execs, results)
  else
    raise "unexpected measure type: #{@measure_type.dump}"
  end
end