Class: Statue::Stopwatch

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

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Stopwatch

Returns a new instance of Stopwatch.



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

def initialize(name, options = {})
  @name     = name
  @reporter = options[:reporter] || Statue
  @start    = @partial = options[:now] || Clock.now_in_ms
end

Instance Method Details

#partial(options = {}) ⇒ Object



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

def partial(options = {})
  suffix = options.delete(:suffix)
  now = options.delete(:now) || Clock.now_in_ms
  previous, @partial = @partial, now

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

#reset(options = {}) ⇒ Object



30
31
32
# File 'lib/statue/stopwatch.rb', line 30

def reset(options = {})
  @start = @partial = options[:now] || Clock.now_in_ms
end

#stop(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/statue/stopwatch.rb', line 18

def stop(options = {})
  suffix = options.delete(:suffix)
  now = options.delete(:now) || Clock.now_in_ms
  report_partial = options.delete(:report_partial) || false

  partial(options.merge(now: now, suffix: report_partial.is_a?(String) ? report_partial : nil)) if report_partial

  previous, @start = @start, now

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