Class: Diagnostics::Sample

Inherits:
Object
  • Object
show all
Includes:
Dependency, Initializer, Log::Dependency
Defined in:
lib/diagnostics/sample/log.rb,
lib/diagnostics/sample/result.rb,
lib/diagnostics/sample/sample.rb,
lib/diagnostics/sample/measure.rb,
lib/diagnostics/sample/defaults.rb,
lib/diagnostics/sample/controls/mean.rb,
lib/diagnostics/sample/controls/time.rb,
lib/diagnostics/sample/measure/clock.rb,
lib/diagnostics/sample/controls/clock.rb,
lib/diagnostics/sample/controls/result.rb,
lib/diagnostics/sample/controls/frequency.rb,
lib/diagnostics/sample/controls/standard_deviation.rb

Defined Under Namespace

Modules: Controls, Defaults Classes: Log, Measure, Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cyclesObject



16
17
18
# File 'lib/diagnostics/sample/sample.rb', line 16

def cycles
  @cycles ||= Defaults.cycles
end

#warmup_cyclesObject



21
22
23
# File 'lib/diagnostics/sample/sample.rb', line 21

def warmup_cycles
  @warmup_cycles ||= Defaults.warmup_cycles
end

Class Method Details

.build(cycles = nil, warmup_cycles: nil, gc: nil, &action) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/diagnostics/sample/sample.rb', line 25

def self.build(cycles=nil, warmup_cycles: nil, gc: nil, &action)
  gc = Defaults.gc if gc.nil?

  result = Result.new
  result.gc = gc

  instance = new(result)

  instance.cycles = cycles unless cycles.nil?
  instance.warmup_cycles = warmup_cycles unless warmup_cycles.nil?

  instance.configure(action: action, gc: gc)
  instance
end

.call(cycles = nil, warmup_cycles: nil, gc: nil, &action) ⇒ Object



40
41
42
43
# File 'lib/diagnostics/sample/sample.rb', line 40

def self.call(cycles=nil, warmup_cycles: nil, gc: nil, &action)
  instance = build(cycles, warmup_cycles: warmup_cycles, gc: gc, &action)
  instance.()
end

Instance Method Details

#callObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/diagnostics/sample/sample.rb', line 45

def call
  logger.trace { "Starting warmup (Warmup Cycles: #{warmup_cycles})" }

  warmup_cycles.times do |iteration|
    elapsed_time_nanoseconds = measure.(iteration)

    elapsed_time = milliseconds(elapsed_time_nanoseconds)

    result.warmup_cycle(elapsed_time)
  end

  logger.debug { "Finished warmup (Warmup Cycles: #{warmup_cycles})" }

  logger.trace { "Starting sample (Cycles: #{cycles}, Warmup Cycles: #{warmup_cycles})" }

  cycles.times do |iteration|
    elapsed_time_nanoseconds = measure.(iteration + self.warmup_cycles)

    elapsed_time = milliseconds(elapsed_time_nanoseconds)

    result.cycle(elapsed_time)
  end

  logger.info { "Finished sample (Cycles: #{cycles}, Warmup Cycles: #{warmup_cycles}, Mean Cycle Time: #{result.mean_cycle_time_milliseconds}ms)" }

  result
end

#configure(action: nil, gc: nil) ⇒ Object



11
12
13
# File 'lib/diagnostics/sample/sample.rb', line 11

def configure(action: nil, gc: nil)
  Measure.configure(self, action: action, gc: gc)
end

#milliseconds(nanoseconds) ⇒ Object



73
74
75
# File 'lib/diagnostics/sample/sample.rb', line 73

def milliseconds(nanoseconds)
  Rational(nanoseconds, 1_000_000)
end