Class: BenchmarkDriver::Output::All

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

Constant Summary collapse

NAME_LENGTH =
20
CONTEXT_LENGTH =
20
OPTIONS =
{
  sort: ['--output-sort true|false', TrueClass, 'Sort all output or not (default: true)'],
}

Instance Method Summary collapse

Constructor Details

#initialize(metrics:, jobs:, contexts:, options:) ⇒ All



12
13
14
15
16
17
18
# File 'lib/benchmark_driver/output/all.rb', line 12

def initialize(metrics:, jobs:, contexts:, options:)
  @metrics = metrics
  @job_names = jobs.map(&:name)
  @context_names = contexts.map(&:name)
  @name_length = [@job_names.map(&:length).max, NAME_LENGTH].max
  @sort = options.fetch(:sort, true)
end

Instance Method Details

#report(result) ⇒ Object



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
# File 'lib/benchmark_driver/output/all.rb', line 63

def report(result)
  if result.all_values.nil? || !defined?(@context_values)
    $stdout.puts(" %#{[CONTEXT_LENGTH, @context.name.length].max}s " % result.values.values.first.to_s)
    return
  end

  num_values = result.all_values.values.first.size
  if @context_values.empty?
    print("\r")
  else
    print("\e[#{num_values}F")
  end
  @context_values[@context] = result.all_values.values.first
  if @sort
    @context_values[@context] = @context_values[@context].sort
  end

  precision = result.values.values.first.to_s.sub(/\A\d+\./, '').length
  num_values.times do |i|
    if i == 0
      $stdout.print(@job_name)
    else
      print(" " * [@job_name.length, NAME_LENGTH].max)
    end

    @context_values.each do |context, values|
      $stdout.print(" %#{[CONTEXT_LENGTH, context.name.length].max}.#{precision}f " % values[i])
    end
    (@context_names - @context_values.keys.map(&:name)).each do |context_name|
      print(" " * ([CONTEXT_LENGTH, context_name.length].max + 2))
    end

    if i == 0
      $stdout.puts(@metrics.first.unit)
    else
      $stdout.puts
    end
  end
end

#with_benchmark(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/benchmark_driver/output/all.rb', line 28

def with_benchmark(&block)
  @job_context_result = Hash.new do |hash, job|
    hash[job] = {}
  end

  result = without_stdout_buffering do
    $stdout.puts 'Calculating -------------------------------------'
    if @context_names.size > 1
      $stdout.print(' ' * @name_length)
      @context_names.each do |context_name|
        $stdout.print(" %#{CONTEXT_LENGTH}s " % context_name)
      end
      $stdout.puts
    end

    block.call
  end
  result
end

#with_context(context, &block) ⇒ Object



57
58
59
60
# File 'lib/benchmark_driver/output/all.rb', line 57

def with_context(context, &block)
  @context = context
  block.call
end

#with_job(job, &block) ⇒ Object



49
50
51
52
53
54
# File 'lib/benchmark_driver/output/all.rb', line 49

def with_job(job, &block)
  @job_name = "%#{@name_length}s" % job.name
  $stdout.print(@job_name)
  @context_values = {}
  block.call
end

#with_warmup(&block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/benchmark_driver/output/all.rb', line 20

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