Module: RSpecPower::BenchmarkHelpers

Defined in:
lib/rspec_power/benchmark.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_result(result) ⇒ Object



8
9
10
# File 'lib/rspec_power/benchmark.rb', line 8

def add_result(result)
  results_registry << result
end

.resultsObject



12
13
14
# File 'lib/rspec_power/benchmark.rb', line 12

def results
  results_registry.dup
end

.results_registryObject



4
5
6
# File 'lib/rspec_power/benchmark.rb', line 4

def results_registry
  @results_registry ||= []
end

Instance Method Details

#__run_benchmark__(runs: 1, label:) ⇒ Object

Internal: run the given block multiple times and record a summary. Used by the shared context, not exposed as a public helper anymore.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rspec_power/benchmark.rb', line 19

def __run_benchmark__(runs: 1, label:)
  raise ArgumentError, "__run_benchmark__ requires a block" unless block_given?

  iterations = runs.to_i
  raise ArgumentError, "runs must be >= 1" if iterations < 1

  timings_ms = []
  iterations.times do
    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    yield
    finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    timings_ms << (finish - start) * 1000.0
  end

  avg = timings_ms.sum / timings_ms.length
  summary = {
    label: label,
    runs: iterations,
    avg_ms: avg,
    min_ms: timings_ms.min,
    max_ms: timings_ms.max
  }

  BenchmarkHelpers.add_result(summary)
  summary
end