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 11 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 32 def count @count.get end
increment(delta = 1)
click to toggle source
Increment the counter by delta
# File lib/spectator/counter.rb, line 17 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 22 def measure cnt = @count.get_and_set(0) if cnt.positive? [Measure.new(@id.with_default_stat('count'), cnt)] else [] end end
to_s()
click to toggle source
Get a string representation for debugging purposes
# File lib/spectator/counter.rb, line 37 def to_s "Counter{id=#{@id}, count=#{@count.get}}" end