Class: Ace::TestSupport::PerformanceHelpers::PerformanceComparison

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/test_support/performance_helpers.rb

Overview

Performance comparison helper class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label_a, label_b) ⇒ PerformanceComparison

Returns a new instance of PerformanceComparison.



142
143
144
145
146
147
# File 'lib/ace/test_support/performance_helpers.rb', line 142

def initialize(label_a, label_b)
  @label_a = label_a
  @label_b = label_b
  @block_a = nil
  @block_b = nil
end

Instance Attribute Details

#label_aObject (readonly)

Returns the value of attribute label_a.



140
141
142
# File 'lib/ace/test_support/performance_helpers.rb', line 140

def label_a
  @label_a
end

#label_bObject (readonly)

Returns the value of attribute label_b.



140
141
142
# File 'lib/ace/test_support/performance_helpers.rb', line 140

def label_b
  @label_b
end

Instance Method Details

#baseline(&block) ⇒ Object



149
150
151
# File 'lib/ace/test_support/performance_helpers.rb', line 149

def baseline(&block)
  @block_a = block
end

#candidate(&block) ⇒ Object



153
154
155
# File 'lib/ace/test_support/performance_helpers.rb', line 153

def candidate(&block)
  @block_b = block
end

#executeObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ace/test_support/performance_helpers.rb', line 157

def execute
  raise "Both baseline and candidate blocks required" unless @block_a && @block_b

  result_a = Benchmark.measure { @block_a.call }
  result_b = Benchmark.measure { @block_b.call }

  faster_label = (result_a.real < result_b.real) ? label_a : label_b
  slower_label = (result_a.real < result_b.real) ? label_b : label_a
  speedup = [result_a.real, result_b.real].max / [result_a.real, result_b.real].min

  {
    faster: faster_label,
    slower: slower_label,
    speedup: speedup,
    baseline_time: result_a.real,
    candidate_time: result_b.real
  }
end