Class: BenchmarkDriver::Output::Compare

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark_driver/output/compare.rb

Overview

Compare output like benchmark-ips

Constant Summary collapse

NAME_LENGTH =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jobs:, executables:) ⇒ Compare



10
11
12
13
# File 'lib/benchmark_driver/output/compare.rb', line 10

def initialize(jobs:, executables:)
  @jobs = jobs
  @executables = executables
end

Instance Attribute Details

#metrics_type=(value) ⇒ Object (writeonly)



6
7
8
# File 'lib/benchmark_driver/output/compare.rb', line 6

def metrics_type=(value)
  @metrics_type = value
end

Instance Method Details

#report(metrics) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/benchmark_driver/output/compare.rb', line 69

def report(metrics)
  if defined?(@metrics_by_job)
    @metrics_by_job[@current_job] << metrics
  end

  @job_metrics << metrics
  $stdout.print("#{humanize(metrics.value, [10, metrics.executable.name.length].max)} ")
end

#with_benchmark(&block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/benchmark_driver/output/compare.rb', line 23

def with_benchmark(&block)
  @metrics_by_job = Hash.new { |h, k| h[k] = [] }

  without_stdout_buffering do
    $stdout.puts 'Calculating -------------------------------------'
    if @executables.size > 1
      $stdout.print(' ' * NAME_LENGTH)
      @executables.each do |executable|
        $stdout.print(' %10s ' % executable.name)
      end
      $stdout.puts
    end

    block.call
  end
ensure
  if @executables.size > 1
    compare_executables
  elsif @jobs.size > 1
    compare_jobs
  end
end

#with_job(job, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/benchmark_driver/output/compare.rb', line 47

def with_job(job, &block)
  if job.name.length > NAME_LENGTH
    $stdout.puts(job.name)
  else
    $stdout.print("%#{NAME_LENGTH}s" % job.name)
  end
  @current_job = job
  @job_metrics = []
  block.call
ensure
  $stdout.print(@metrics_type.unit)
  if job.respond_to?(:loop_count) && job.loop_count
    $stdout.print(" - #{humanize(job.loop_count)} times")
    if @job_metrics.all? { |metrics| metrics.duration }
      $stdout.print(" in")
      show_durations
    end
  end
  $stdout.puts
end

#with_warmup(&block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/benchmark_driver/output/compare.rb', line 15

def with_warmup(&block)
  without_stdout_buffering do
    $stdout.puts 'Warming up --------------------------------------'
    # TODO: show exec name if it has multiple ones
    block.call
  end
end