Class: SISFC::Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/sisfc/statistics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Statistics



13
14
15
16
17
18
19
# File 'lib/sisfc/statistics.rb', line 13

def initialize(opts={})
  @n    = 0 # number of requests
  @mean = 0.0
  @m_2  = 0.0
  @longer_than = init_counters_for_longer_than_stats(opts)
  @received = 0
end

Instance Attribute Details

#longer_thanObject (readonly)

Returns the value of attribute longer_than.



8
9
10
# File 'lib/sisfc/statistics.rb', line 8

def longer_than
  @longer_than
end

#meanObject (readonly)

Returns the value of attribute mean.



8
9
10
# File 'lib/sisfc/statistics.rb', line 8

def mean
  @mean
end

#nObject (readonly) Also known as: closed

Returns the value of attribute n.



8
9
10
# File 'lib/sisfc/statistics.rb', line 8

def n
  @n
end

#receivedObject (readonly)

Returns the value of attribute received.



8
9
10
# File 'lib/sisfc/statistics.rb', line 8

def received
  @received
end

Instance Method Details

#record_request(req) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sisfc/statistics.rb', line 25

def record_request(req)
  # get new sample
  x = req.ttr
  raise "TTR #{x} for request #{req.rid} invalid!" unless x > 0.0

  @longer_than.each_key do |k|
    @longer_than[k] += 1 if x > k
  end

  # update counters
  @n += 1
  delta = x - @mean
  @mean += delta / @n
  @m_2  += delta * (x - @mean)
end

#request_receivedObject



21
22
23
# File 'lib/sisfc/statistics.rb', line 21

def request_received
  @received += 1
end

#to_sObject



45
46
47
48
# File 'lib/sisfc/statistics.rb', line 45

def to_s
  "received: #{@received}, closed: #{@n}, " +
  "(mean: #{@mean}, variance: #{variance}, longer_than: #{@longer_than.to_s})"
end

#varianceObject



41
42
43
# File 'lib/sisfc/statistics.rb', line 41

def variance
  @m_2 / (@n - 1)
end