Class: Spectator::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/spectator/counter.rb

Overview

A counter is used to measure the rate at which an event is occurring

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Counter

Initialize a new instance setting its id, and starting the count at 0



9
10
11
12
# File 'lib/spectator/counter.rb', line 9

def initialize(id)
  @id = id
  @count = AtomicNumber.new(0)
end

Instance Method Details

#countObject

Read the current count. Calls to measure will reset it



25
26
27
# File 'lib/spectator/counter.rb', line 25

def count
  @count.get
end

#increment(delta = 1) ⇒ Object

Increment the counter by delta



15
16
17
# File 'lib/spectator/counter.rb', line 15

def increment(delta = 1)
  @count.add_and_get(delta)
end

#measureObject

Get the current count as a list of Measure and reset the count to 0



20
21
22
# File 'lib/spectator/counter.rb', line 20

def measure
  [Measure.new(@id.with_stat('count'), @count.get_and_set(0))]
end

#to_sObject

Get a string representation for debugging purposes



30
31
32
# File 'lib/spectator/counter.rb', line 30

def to_s
  "Counter{id=#{@id}, count=#{@count.get}}"
end