Module: Benchmark::Runner

Defined in:
lib/rubybench_runner/support/benchmark_runner.rb

Class Method Summary collapse

Class Method Details

.compute_ips(time, warmup, label, &block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rubybench_runner/support/benchmark_runner.rb', line 19

def self.compute_ips(time, warmup, label, &block)
  report = Benchmark.ips(time, warmup, true) do |x|
    x.report(label) { yield }
  end

  report.entries.first
end

.compute_objects(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubybench_runner/support/benchmark_runner.rb', line 27

def self.compute_objects(&block)
  if block_given?
    key =
      if RUBY_VERSION < '2.2'
        :total_allocated_object
      else
        :total_allocated_objects
      end

    before = GC.stat[key]
    yield
    after = GC.stat[key]
    after - before
  end
end


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubybench_runner/support/benchmark_runner.rb', line 43

def self.print_output(ips_result, objects_result, label, version)
  standard_deviation =
    # https://github.com/evanphx/benchmark-ips/commit/b42c3dfbe104f32ce7db075a01858d598da87a8b
    if ips_result.respond_to?(:error_percentage)
      ips_result.error_percentage
    else
      ips_result.stddev_percentage # deprecated
    end
  output = {
    label: label,
    version: version,
    iterations_per_second: ips_result.ips,
    iterations_per_second_standard_deviation: standard_deviation,
    total_allocated_objects_per_iteration: objects_result,
  }.to_json

  puts output
end

.run(label = nil, version:, time:, disable_gc:, warmup:, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rubybench_runner/support/benchmark_runner.rb', line 6

def self.run(label=nil, version:, time:, disable_gc:, warmup:, &block)
  unless block_given?
    raise ArgumentError.new, "You must pass block to run"
  end

  GC.disable if disable_gc

  ips_result = compute_ips(time, warmup, label, &block)
  objects_result = compute_objects(&block)

  print_output(ips_result, objects_result, label, version)
end