Class: Drone::Metrics::Meter

Inherits:
Drone::Metric show all
Defined in:
lib/drone/metrics/meter.rb

Overview

This meter measures mean throughput and one-, five-, and fifteen-minute exponentially-weighted moving average throughputs.

Constant Summary collapse

INTERVAL =
5

Instance Attribute Summary

Attributes inherited from Drone::Metric

#name

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Meter

Returns a new instance of Meter.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/drone/metrics/meter.rb', line 16

def initialize(name)
  super(name)
  @start_time = Drone::request_number("#{name}:start_time", Time.now)
  @next_tick = Drone::request_number("#{name}:next_tick_lock", 1)
  
  @count = Drone::request_number("#{name}:count", 0)
  @rates = {
    1   => EWMA.one_minute_ewma("#{name}:rate1"),
    5   => EWMA.five_minutes_ewma("#{name}:rate5"),
    15  => EWMA.fifteen_minutes_ewma("#{name}:rate15")
  }
  
  Drone::schedule_periodic(INTERVAL, &method(:tick))
end

Instance Method Details

#countObject



53
54
55
# File 'lib/drone/metrics/meter.rb', line 53

def count
  @count.get
end

#fifteen_minutes_rateObject



74
75
76
# File 'lib/drone/metrics/meter.rb', line 74

def fifteen_minutes_rate
  @rates[15].rate()
end

#five_minutes_rateObject



70
71
72
# File 'lib/drone/metrics/meter.rb', line 70

def five_minutes_rate
  @rates[5].rate()
end

#mark(events = 1) ⇒ Object



46
47
48
49
50
51
# File 'lib/drone/metrics/meter.rb', line 46

def mark(events = 1)
  @count.inc(events)
  @rates.each do |_, r|
    r.update(events)
  end
end

#mean_rateObject



57
58
59
60
61
62
63
64
# File 'lib/drone/metrics/meter.rb', line 57

def mean_rate
  count = @count.get
  if count == 0
    0.0
  else
    count / (Time.now.to_f - @start_time.get.to_f)
  end
end

#one_minute_rateObject



66
67
68
# File 'lib/drone/metrics/meter.rb', line 66

def one_minute_rate
  @rates[1].rate()
end

#tickObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/drone/metrics/meter.rb', line 31

def tick
  # init if required
  @local_next_tick ||= @next_tick.get
  
  # ensure only one process will trigger the tick
  if @next_tick.compare_and_set(@local_next_tick, @local_next_tick + 1)
    @rates.values.each(&:tick)
    @local_next_tick += 1
  else
    # reset the tick counter to give a chance to this
    # process to trigger the next tick
    @local_next_tick = @next_tick.get()
  end
end