Class: BenchmarkSpec::ReportPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark_spec/report_presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(benchmark, opts = {}, formatter: nil) ⇒ ReportPresenter

Returns a new instance of ReportPresenter.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/benchmark_spec/report_presenter.rb', line 5

def initialize(benchmark, opts = {}, formatter: nil)
  @benchmark = benchmark
  @formatter = formatter || BenchmarkSpec::OutputFormatter.new

  @round_to = opts[:round_to] || 3

  @nested_reports = []
  @benchmark.nested_specs.map do |ns|
    child_formatter = BenchmarkSpec::OutputFormatter.new(indent_level: @formatter.indent_level + 1)
    @nested_reports << ReportPresenter.new(ns, formatter: child_formatter)
  end
end

Instance Attribute Details

#benchmarkObject

Returns the value of attribute benchmark.



3
4
5
# File 'lib/benchmark_spec/report_presenter.rb', line 3

def benchmark
  @benchmark
end

#formatterObject

Returns the value of attribute formatter.



3
4
5
# File 'lib/benchmark_spec/report_presenter.rb', line 3

def formatter
  @formatter
end

#nested_reportsObject

Returns the value of attribute nested_reports.



3
4
5
# File 'lib/benchmark_spec/report_presenter.rb', line 3

def nested_reports
  @nested_reports
end

#round_toObject

Returns the value of attribute round_to.



3
4
5
# File 'lib/benchmark_spec/report_presenter.rb', line 3

def round_to
  @round_to
end

Class Method Details

.calculate_num_of_examples_run(benchmark) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/benchmark_spec/report_presenter.rb', line 55

def self.calculate_num_of_examples_run(benchmark)
  return benchmark.reports.count if benchmark.nested_specs.empty?

  num_of_examples_run = benchmark.reports.count
  benchmark.nested_specs.each do |ns|
    num_of_examples_run += calculate_num_of_examples_run(ns)
  end

  num_of_examples_run
end

.calculate_total_time(benchmark) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/benchmark_spec/report_presenter.rb', line 66

def self.calculate_total_time(benchmark)
  if benchmark.nested_specs.empty?
    return benchmark.reports.map(&:total).reduce(:+) || 0
  end

  total_time = benchmark.reports.map(&:total).reduce(:+) || 0
  benchmark.nested_specs.each do |ns|
    total_time += calculate_total_time(ns)
  end

  total_time
end


37
38
39
40
41
42
43
# File 'lib/benchmark_spec/report_presenter.rb', line 37

def self.print_results(benchmarks)
  benchmarks ||= []

  reports = benchmarks.map{|b| ReportPresenter.new(b)}
  reports.each{|r| r.print}
  print_summary(benchmarks)
end


45
46
47
48
49
50
51
52
53
# File 'lib/benchmark_spec/report_presenter.rb', line 45

def self.print_summary(benchmarks)
  benchmarks ||= []

  num_of_examples_run = benchmarks.map{|b| calculate_num_of_examples_run(b)}.reduce(:+) || 0
  total_time          = benchmarks.map{|b| calculate_total_time(b)}.reduce(:+) || 0

  puts "--------------------------------------------------------------------"
  puts "Examples run: #{num_of_examples_run}, Total Execution Time: #{total_time}s".colorize(:light_green)
end

Instance Method Details



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/benchmark_spec/report_presenter.rb', line 18

def print
  formatter.print "#{benchmark.description}:".colorize(:light_blue)
  benchmark.reports.each do |r|
    formatter.with_margins_vertically do
      formatter.print "#{r.label}:".colorize(:cyan).underline
      formatter.new_line
      formatter.print "    User: #{r.utime.round(round_to)}s"
      formatter.print "  System: #{r.stime.round(round_to)}s"
      formatter.print "   Total: #{r.total.round(round_to)}s"
    end
  end

  if nested_reports
    nested_reports.each do |nr|
      nr.print
    end
  end
end