Class: BenchmarkDriver::Output::Markdown

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

Constant Summary collapse

NAME_LENGTH =
8
OPTIONS =
{
  compare: ['--output-compare', 'Show comparison between results'],
}

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Markdown.

Parameters:



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

def initialize(metrics:, jobs:, contexts:, options:)
  @metrics = metrics
  @contexts = contexts
  @context_names = contexts.map(&:name)
  @name_length = jobs.map(&:name).map(&:size).max
  @compare = options.fetch(:compare, false)
end

Instance Method Details

#report(result) ⇒ Object

Parameters:



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/benchmark_driver/output/markdown.rb', line 77

def report(result)
  if defined?(@job_context_result)
    @job_context_result[@context] = result
  end

  if @with_benchmark
    length = [NAME_LENGTH, @context.name.length].max
    $stdout.printf("|%*s", length, humanize(result.values.fetch(@metrics.first)))
  else
    $stdout.print '.'
  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
47
48
49
50
51
52
53
# File 'lib/benchmark_driver/output/markdown.rb', line 28

def with_benchmark(&block)
  @with_benchmark = true
  without_stdout_buffering do
    # Show header
    $stdout.puts "# #{@metrics.first.name} (#{@metrics.first.unit})\n\n"

    # Show executable names
    $stdout.print("|#{' ' * @name_length}  ")
    @context_names.each do |context_name|
      $stdout.printf("|%*s", NAME_LENGTH, context_name) # same size as humanize
    end
    $stdout.puts('|')

    # Show header separator
    $stdout.print("|:#{'-' * (@name_length - 1)}--")
    @context_names.each do |context_name|
      length = [context_name.length, NAME_LENGTH].max
      $stdout.print("|#{'-' * (length - 1)}:") # same size as humanize
    end
    $stdout.puts('|')

    block.call
  end
rescue
  @with_benchmark = false
end

#with_context(context, &block) ⇒ Object

Parameters:



71
72
73
74
# File 'lib/benchmark_driver/output/markdown.rb', line 71

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

#with_job(job, &block) ⇒ Object

Parameters:



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/benchmark_driver/output/markdown.rb', line 56

def with_job(job, &block)
  if @with_benchmark
    @job_context_result = {} if @context_names.size > 1

    $stdout.printf("|%-*s  ", @name_length, job.name)
  end
  block.call
ensure
  if @with_benchmark
    $stdout.puts('|')
    compare_executables if @compare && @context_names.size > 1
  end
end

#with_warmup(&block) ⇒ Object



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

def with_warmup(&block)
  without_stdout_buffering do
    $stdout.print 'warming up'
    block.call
  end
ensure
  $stdout.puts
end