Module: Benchmark::Perf::Iteration Private

Defined in:
lib/benchmark/perf/iteration.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Measure number of iterations a work could take in a second

Class Method Summary collapse

Class Method Details

.call_times(times) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Call work by given times

Parameters:

  • times (Integer)

    the times to call

Returns:

  • (Integer)

    the number of times worke has been called



22
23
24
25
26
27
28
# File 'lib/benchmark/perf/iteration.rb', line 22

def call_times(times)
  i = 0
  while i < times
    yield
    i += 1
  end
end

.cycles_per_100ms(iterations, time_s) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calcualte the number of cycles needed for 100ms

Parameters:

  • iterations (Integer)
  • time_s (Float)

    the total time for all iterations in seconds

Returns:

  • (Integer)

    the cycles per 100ms



41
42
43
44
45
46
# File 'lib/benchmark/perf/iteration.rb', line 41

def cycles_per_100ms(iterations, time_s)
  cycles = iterations * Clock::MICROSECONDS_PER_100MS
  cycles /= time_s * Clock::MICROSECONDS_PER_SECOND
  cycles = cycles.to_i
  cycles <= 0 ? 1 : cycles
end

.run(time: 2, warmup: 1, &work) ⇒ Object

Run measurements

Parameters:

  • time (Numeric) (defaults to: 2)

    the time to run measurements in seconds

  • warmup (Numeric) (defaults to: 1)

    the warmup time in seconds



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/benchmark/perf/iteration.rb', line 80

def run(time: 2, warmup: 1, &work)
  cycles_in_100ms = run_warmup(warmup: warmup, &work)

  GC.start

  result = IPSResult.new

  target = (before = Clock.now) + time

  while Clock.now < target
    time_s = Clock.measure { call_times(cycles_in_100ms, &work) }

    next if time_s <= 0.0 # Iteration took no time

    result.add(time_s, cycles_in_100ms)
  end

  result
end

.run_warmup(warmup: 1, &work) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Warmup run

Parameters:

  • warmup (Numeric) (defaults to: 1)

    the number of seconds for warmup



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/benchmark/perf/iteration.rb', line 55

def run_warmup(warmup: 1, &work)
  GC.start

  target = Clock.now + warmup
  iter = 0

  time_s = Clock.measure do
    while Clock.now < target
      call_times(1, &work)
      iter += 1
    end
  end

  cycles_per_100ms(iter, time_s)
end