Class: Flamingo::Stats::RateCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/flamingo/stats/rate_counter.rb

Overview

Simple counter for measuring stream rates in events per second

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sample_duration = 60, &block) ⇒ RateCounter

Returns a new instance of RateCounter.



9
10
11
12
13
# File 'lib/flamingo/stats/rate_counter.rb', line 9

def initialize(sample_duration=60, &block)
  @sample_duration = sample_duration
  self.callback = block
  start_sample
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



7
8
9
# File 'lib/flamingo/stats/rate_counter.rb', line 7

def callback
  @callback
end

#rateObject

Returns the value of attribute rate.



7
8
9
# File 'lib/flamingo/stats/rate_counter.rb', line 7

def rate
  @rate
end

Instance Method Details

#event!Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/flamingo/stats/rate_counter.rb', line 15

def event!
  @count += 1
  if (diff = (now - @sample_start_time)) >= @sample_duration
    self.rate = (@count / diff.to_f)
    if callback
      callback.call(rate)
    end
    start_sample
  end
end