class Spectator::Counter

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

Public Class Methods

new(id) click to toggle source

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

# File lib/spectator/counter.rb, line 9
def initialize(id)
  @id = id
  @count = AtomicNumber.new(0)
end

Public Instance Methods

count() click to toggle source

Read the current count. Calls to measure will reset it

# File lib/spectator/counter.rb, line 25
def count
  @count.get
end
increment(delta = 1) click to toggle source

Increment the counter by delta

# File lib/spectator/counter.rb, line 15
def increment(delta = 1)
  @count.add_and_get(delta)
end
measure() click to toggle source

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

# File lib/spectator/counter.rb, line 20
def measure
  [Measure.new(@id.with_stat('count'), @count.get_and_set(0))]
end
to_s() click to toggle source

Get a string representation for debugging purposes

# File lib/spectator/counter.rb, line 30
def to_s
  "Counter{id=#{@id}, count=#{@count.get}}"
end