Class: Spectator::DistributionSummary

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

Overview

Track the sample distribution of events. An example would be the response sizes for requests hitting an http server.

The class will report measurements for the total amount, the count, max, and the total of the square of the amounts recorded (useful for computing a standard deviation)

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ DistributionSummary

Initialize a new DistributionSummary instance with a given id



12
13
14
15
16
17
18
# File 'lib/spectator/distribution_summary.rb', line 12

def initialize(id)
  @id = id
  @count = AtomicNumber.new(0)
  @total_amount = AtomicNumber.new(0)
  @total_sq = AtomicNumber.new(0)
  @max = AtomicNumber.new(Float::NAN)
end

Instance Method Details

#countObject

Get the current amount



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

def count
  @count.get
end

#measureObject

Get a list of measurements, and reset the stats The stats returned are the current count, the total amount, the sum of the square of the amounts recorded, and the max value



42
43
44
45
46
47
48
49
50
51
# File 'lib/spectator/distribution_summary.rb', line 42

def measure
  cnt = Measure.new(@id.with_stat('count'), @count.get_and_set(0))
  tot = Measure.new(@id.with_stat('totalAmount'),
                    @total_amount.get_and_set(0))
  tot_sq = Measure.new(@id.with_stat('totalOfSquares'),
                       @total_sq.get_and_set(0))
  mx = Measure.new(@id.with_stat('max'), @max.get_and_set(Float::NAN))

  [cnt, tot, tot_sq, mx]
end

#record(amount) ⇒ Object

Update the statistics kept by the summary with the specified amount.



21
22
23
24
25
26
27
# File 'lib/spectator/distribution_summary.rb', line 21

def record(amount)
  return if amount < 0
  @count.add_and_get(1)
  @total_amount.add_and_get(amount)
  @total_sq.add_and_get(amount * amount)
  @max.max(amount)
end

#total_amountObject

Return the total amount



35
36
37
# File 'lib/spectator/distribution_summary.rb', line 35

def total_amount
  @total_amount.get
end