Stability: 1 - Experimental
Exponentially Weighted Loss Rate.
npm install ewlr
EWLR is an Exponentially Weighted Loss Rate calculation module to estimate event loss rate.
To run the below example run:
npm run readme
"use strict";
var EWLR = require('../index.js');
// keep track of last tick and tick interval
var lastTick = EWLR.getTime();
var tickInterval = 1000; // 1 second (in milliseconds)
var ewlr = new EWLR();
// update with success
ewlr.update();
// update with failure
ewlr.update(1, 1);
// update with 20% loss rate
ewlr.update(1000, 200);
// or a less precise 20% loss rate
for (var i = 0; i < 1000; i++) {
ewlr.update(1, (Math.random() < 0.2 ? 1 : 0));
}
// tick the required number of times in order to get an accurate loss rate
var shouldTick = EWLR.tickIfNecessary(lastTick, tickInterval);
if (shouldTick) {
lastTick = shouldTick.newLastTick;
for (var i = 0; i < shouldTick.requiredTicks; i++) {
ewlr.tick();
}
}
console.dir(ewlr.rate()); // may be 0 if we haven't tick()'ed yet
setTimeout(function () {
shouldTick = EWLR.tickIfNecessary(lastTick, tickInterval);
if (shouldTick) {
lastTick = shouldTick.newLastTick;
for (var i = 0; i < shouldTick.requiredTicks; i++) {
ewlr.tick();
}
}
console.dir(ewlr.rate());
}, 3000); // will get 3 ticks if we wait 3 seconds
npm test
Public API
- EWLR.getTime()
- EWLR.tickIfNecessary(lastTick, tickInterval)
- new EWLR(config)
- ewlr.rate()
- ewlr.tick()
- ewlr.update([n], [lost])
- Return: Number Time in milliseconds (for example: 109.372263)
lastTick
: Number Time of last tick in milliseconds.tickInterval
: Number Tick interval in milliseconds.- Return: undefined If no tick necessary.
- Return: Object If tick is necessary
lastTick
: Number Time of new last tick to set in millisecondsrequiredTicks
: Number The number of ticks to execute to catch up.
Determines if and how many ticks are necessary.
config
: ObjecttimePeriod
: Number (Default: 1000) Time period in milliseconds.tickInterval
: Number (Default: 1000) Tick inteval in milliseconds.
Creates a new EWLR instance.
- Return: Object
rate
: Number Rate of events. Rate is returned so that it can be determined iflossRate
is meaningful. For example, if rate is 2.504763981949714e-32, the loss rate (even if 20%), may not be meaningful since the tiny rate may mean that no updates have happened for a long time.lossRate
: Number Normalized ([0..1]) loss rate of eventslossRate
: Number Normalized ([0..1]) loss rate of events
Returns the event rate and the normalized loss rate. The rate
is only meant as a liveness/recency measure to see if lossRate
is meaningful.
Update the rate and loss rate estimates in accordance with time period and tick interval.
n
: Integer (Default: 1) Number of events to update with.lost
: Integer (Default: 0) Number of lost events to update with (0 < lost <= n)