Class: Statue::Stopwatch

Inherits:
Object
  • Object
show all
Defined in:
lib/statue/stopwatch.rb

Instance Method Summary collapse

Constructor Details

#initialize(name:, now: Clock.now_in_ms, reporter: Statue) ⇒ Stopwatch

Returns a new instance of Stopwatch.



4
5
6
7
8
# File 'lib/statue/stopwatch.rb', line 4

def initialize(name:, now: Clock.now_in_ms, reporter: Statue)
  @reporter = reporter
  @name     = name
  @start    = @partial = now
end

Instance Method Details

#partial(suffix = nil, now: Clock.now_in_ms, **options) ⇒ Object



10
11
12
13
14
# File 'lib/statue/stopwatch.rb', line 10

def partial(suffix = nil, now: Clock.now_in_ms, **options)
  previous, @partial = @partial, now

  @reporter.report_duration(metric_name(suffix || "runtime.partial"), @partial - previous, **options)
end

#reset(now: Clock.now_in_ms) ⇒ Object



24
25
26
# File 'lib/statue/stopwatch.rb', line 24

def reset(now: Clock.now_in_ms)
  @start = @partial = now
end

#stop(suffix = nil, now: Clock.now_in_ms, report_partial: false, **options) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/statue/stopwatch.rb', line 16

def stop(suffix = nil, now: Clock.now_in_ms, report_partial: false, **options)
  partial(report_partial.is_a?(String) ? report_partial : nil, now: now, **options) if report_partial

  previous, @start = @start, now

  @reporter.report_duration(metric_name(suffix || "runtime.total"), @start - previous, **options)
end