Skip to content
A rolling time window implementation for hdr-histogram-js
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
__tests__
.gitignore
LICENSE
README.md
index.js
jest.config.js
package.json
yarn.lock

README.md

rolling-window

CircleCI codecov npm version

Implements "Reset reservoir periodically by chunks" strategy to use hdr-histogram-js for monitoring. Inspired by vladimir-bukhtoyarov/rolling-metrics.

See rolling-metrics' documentation for the background.

Install

npm install -S hdr-histogram-js @shuhei/rolling-window

Usage

const RollingWindow = require("@shuhei/rolling-window");

// This configuration creates 7 internal histograms (6 + 1) and rotates them
// one by one in each 10 seconds. `getSnapshot()` returns a histogram of the
// last 60 to 70 seconds. These additional 0 to 10 seconds make sure that the
// rolling window can provide a fresh histogram without losing any records at
// any given time.
const rollingWindow = new RollingWindow({
  timeWindow: 1000 * 60,
  numChunks: 6,
});

// Record a value
rollingWindow.recordValue(value);

// Get a snapshot
const snapshot = rollingWindow.getSnapshot();
const p99 = snapshot.getValueAtPercentile(99);

API

new RollingWindow([options])

  • options
    • timeWindow: number The length of a time window in milliseconds. Default: 60000
    • numChunks: number The number of chunks in the time window. Default: 6
    • buildHistogram: () => Histogram A factory function to create a histogram. This will be called multiple times to prepare necessary histograms in the rolling window. Use this to provide custom options to histograms. Default: build from hdr-histogram-js

Creates a rolling window with numChunks + 1 histograms in it and starts rotating chunks with an interval of timeWindow / numChunks.

rollingWindow.stop()

Stop the rotation timer. When you stop using a rolling window, make sure to call this method to avoid memory leak.

rollingWindow.recordValue(value)

  • value: number A numerical value to record. It must not be negative.

rollingWindow.getSnapshot([snapshot])

  • snapshot: Histogram A histogram to accumulate histograms. It is reset before accumulating histograms. If this is not provided, a Histogram is created and kept for reuse.
  • Returns: Histogram
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.