class Leafy::Core::EWMA

Constants

FIFTEEN_MINUTES
FIVE_MINUTES
FIVE_NANOS
INTERVAL
M15_ALPHA
M1_ALPHA
M5_ALPHA
ONE_MINUTE
ONE_NANOS
SECONDS_PER_MINUTE

Public Class Methods

fifteenMinuteEWMA() click to toggle source

Creates a new EWMA which is equivalent to the UNIX fifteen minute load average and which expects to be ticked every 5 seconds.

@return a fifteen-minute EWMA

# File lib/leafy/core/ewma.rb, line 47
def self.fifteenMinuteEWMA
  EWMA.new(M15_ALPHA);
end
fiveMinuteEWMA() click to toggle source

Creates a new EWMA which is equivalent to the UNIX five minute load average and which expects to be ticked every 5 seconds.

@return a five-minute EWMA

# File lib/leafy/core/ewma.rb, line 39
def self.fiveMinuteEWMA
  EWMA.new(M5_ALPHA);
end
new(alpha) click to toggle source

Create a new EWMA with a specific smoothing constant.

@param alpha the smoothing constant @param interval the expected tick interval @param intervalUnit the time unit of the tick interval

# File lib/leafy/core/ewma.rb, line 58
def initialize(alpha)
  @alpha = alpha;
  @initialized = false # volatile
  @rate = 0.0 # volatile
  @uncounted = Adder.new
end
oneMinuteEWMA() click to toggle source

Creates a new EWMA which is equivalent to the UNIX one minute load average and which expects to be ticked every 5 seconds.

@return a one-minute EWMA

# File lib/leafy/core/ewma.rb, line 31
def self.oneMinuteEWMA
  EWMA.new(M1_ALPHA);
end

Public Instance Methods

rate() click to toggle source

Returns the rate in the given units of time.

@param rateUnit the unit of time @return the rate

# File lib/leafy/core/ewma.rb, line 90
def rate
  @rate * ONE_NANOS
end
tick() click to toggle source

Mark the passage of time and decay the current rate accordingly.

# File lib/leafy/core/ewma.rb, line 73
def tick
  count = @uncounted.sum_then_reset# @uncounted = 0 # uncounted.sumThenReset();
  instantRate = count / FIVE_NANOS;
  if @initialized
    oldRate = @rate
    @rate = oldRate + (@alpha * (instantRate - oldRate))
  else 
    @rate = instantRate
    @initialized = true
  end
  self
end
update(n) click to toggle source

Update the moving average with a new value.

@param n the new value

# File lib/leafy/core/ewma.rb, line 68
def update(n)
  @uncounted.add(n)
end