ewlr

0.1.2 • Public • Published

ewlr

Stability: 1 - Experimental

NPM version

Exponentially Weighted Loss Rate.

Contents

Installation

npm install ewlr

Overview

EWLR is an Exponentially Weighted Loss Rate calculation module to estimate event loss rate.

Usage

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
 

Tests

npm test

Documentation

EWLR

Public API

EWLR.getTime()

  • Return: Number Time in milliseconds (for example: 109.372263)

EWLR.tickIfNecessary(lastTick, tickInterval)

  • 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 milliseconds
    • requiredTicks: Number The number of ticks to execute to catch up.

Determines if and how many ticks are necessary.

new EWLR(config)

  • config: Object
    • timePeriod: Number (Default: 1000) Time period in milliseconds.
    • tickInterval: Number (Default: 1000) Tick inteval in milliseconds.

Creates a new EWLR instance.

ewlr.rate()

  • Return: Object
    • rate: Number Rate of events. Rate is returned so that it can be determined if lossRate 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 events
    • lossRate: 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.

ewlr.tick()

Update the rate and loss rate estimates in accordance with time period and tick interval.

ewlr.update([n], [lost])

  • n: Integer (Default: 1) Number of events to update with.
  • lost: Integer (Default: 0) Number of lost events to update with (0 < lost <= n)

Package Sidebar

Install

npm i ewlr

Weekly Downloads

1

Version

0.1.2

License

MIT

Last publish

Collaborators

  • tristanls