Class: Benchmark::Sweet::Comparison

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/sweet/comparison.rb

Constant Summary collapse

UNITS =
{"ips" => "i/s", "memsize" => "bytes", "memsize_retained" => "bytes"}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric, label, stats, offset, total, baseline, worst = nil) ⇒ Comparison

Returns a new instance of Comparison.



7
8
9
10
11
12
13
14
15
# File 'lib/benchmark/sweet/comparison.rb', line 7

def initialize(metric, label, stats, offset, total, baseline, worst = nil)
  @metric = metric
  @label = label
  @stats = stats
  @offset = offset
  @total = total
  @baseline = baseline
  @worst = worst
end

Instance Attribute Details

#baselineObject (readonly)

Returns the value of attribute baseline.



5
6
7
# File 'lib/benchmark/sweet/comparison.rb', line 5

def baseline
  @baseline
end

#labelObject (readonly)

Returns the value of attribute label.



5
6
7
# File 'lib/benchmark/sweet/comparison.rb', line 5

def label
  @label
end

#metricObject (readonly)

Returns the value of attribute metric.



5
6
7
# File 'lib/benchmark/sweet/comparison.rb', line 5

def metric
  @metric
end

#offsetObject (readonly)

Returns the value of attribute offset.



6
7
8
# File 'lib/benchmark/sweet/comparison.rb', line 6

def offset
  @offset
end

#statsObject (readonly)

Returns the value of attribute stats.



5
6
7
# File 'lib/benchmark/sweet/comparison.rb', line 5

def stats
  @stats
end

#totalObject (readonly)

Returns the value of attribute total.



6
7
8
# File 'lib/benchmark/sweet/comparison.rb', line 6

def total
  @total
end

#worstObject (readonly)

Returns the value of attribute worst.



5
6
7
# File 'lib/benchmark/sweet/comparison.rb', line 5

def worst
  @worst
end

Instance Method Details

#[](field) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/benchmark/sweet/comparison.rb', line 17

def [](field)
  case field
  when :metric      then metric
  when :comp_short  then comp_short
  when :comp_string then comp_string
  when :label       then label  # not sure if this one makes sense
  else label[field]
  end
end

#best?Boolean

Returns:

  • (Boolean)


35
# File 'lib/benchmark/sweet/comparison.rb', line 35

def best? ; !baseline || (baseline == stats) ; end

#central_tendencyObject



27
# File 'lib/benchmark/sweet/comparison.rb', line 27

def central_tendency ; stats.central_tendency ; end

#colorObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/benchmark/sweet/comparison.rb', line 92

def color
  if !baseline
    ";0"
  elsif best? || overlaps?
    "32"
  elsif worst?
    "31"
  else
    ";0"
  end
end

#comp_short(value = nil) ⇒ Object

I tend to call with:

c.comp_short("\033[#{c.color}m#{c.central_tendency.round(1)} #{c.units}\e[0m") # "\033[31m#{value}\e[0m"


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/benchmark/sweet/comparison.rb', line 80

def comp_short(value = nil)
  value ||= "#{central_tendency.round(1)} #{units}"
  case mode
  when :best, :same
    value
  when :slower 
    "%s - %.2fx (± %.2f)" % [value, slowdown, error]
  when :slowerish
    "%s - %.2fx" % [value, slowdown]
  end
end

#comp_string(l_to_s = nil) ⇒ Object

quick display



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/benchmark/sweet/comparison.rb', line 64

def comp_string(l_to_s = nil)
  l_to_s ||= -> l { l.to_s }
  case mode
  when :best
    "%20s: %10.1f %s" % [l_to_s.call(label), central_tendency, units]
  when :same
    "%20s: %10.1f %s - same-ish: difference falls within error" % [l_to_s.call(label), central_tendency, units]
  when :slower 
    "%20s: %10.1f %s - %.2fx (± %.2f) slower" % [l_to_s.call(label), central_tendency, units, slowdown, error]
  when :slowerish
    "%20s: %10.1f %s - %.2fx slower" % [l_to_s.call(label), central_tendency, units, slowdown]
  end
end

#diff_errorObject



58
59
60
# File 'lib/benchmark/sweet/comparison.rb', line 58

def diff_error
  @diff_error ||= (slowdown ; @diff_error)
end

#errorObject



28
# File 'lib/benchmark/sweet/comparison.rb', line 28

def error ; stats.error ; end

#modeObject



31
32
33
# File 'lib/benchmark/sweet/comparison.rb', line 31

def mode
  @mode ||= best? ? :best : overlaps? ? :same : diff_error ? :slowerish : :slower
end

#overlaps?Boolean

Returns true if it is basically the same as the best.

Returns:

  • (Boolean)

    true if it is basically the same as the best



38
39
40
41
42
# File 'lib/benchmark/sweet/comparison.rb', line 38

def overlaps?
  return @overlaps if defined?(@overlaps)
  @overlaps = slowdown == 1 ||
                stats && baseline && (stats.central_tendency == baseline.central_tendency || stats.overlaps?(baseline))
end

#slowdownObject



52
53
54
55
56
# File 'lib/benchmark/sweet/comparison.rb', line 52

def slowdown
  return @slowdown if @slowdown
  @slowdown, @diff_error = stats.slowdown(baseline)
  @slowdown
end

#unitsObject



29
# File 'lib/benchmark/sweet/comparison.rb', line 29

def units ; UNITS[metric] || "objs" ; end

#worst?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/benchmark/sweet/comparison.rb', line 44

def worst?
  if @worst
    stats.overlaps?(@worst)
  else
    slowdown == Float::INFINITY || (total.to_i - 1 == offset.to_i && slowdown.to_i > 1)
   end
end