node.js bindings for hdr histogram C implementation(version 0.9.3).
HDR Histogram is designed for recoding histograms of value measurements in latency and performance sensitive applications. Measurements show value recording times as low as 3-6 nanoseconds on modern (circa 2014) Intel CPUs. A Histogram's memory footprint is constant, with no allocation operations involved in recording data values or in iterating through them.
- from hdr histogram website
This library is blazingly fast, and you can use it to record histograms with no overhead. Linux, Mac OS X and Windows are all supported.
npm i native-hdr-histogram --save
If you see any errors, you might need to configure your system to compile native addons: follow the instructions at node-gyp.
'use strict'
const Histogram = require('native-hdr-histogram')
const max = 1000000
const key = 'record*' + max
const histogram = new Histogram(1, 100)
console.time(key)
for (let i = 0; i < max; i++) {
histogram.record(Math.floor((Math.random() * 42 + 1)))
}
console.timeEnd(key)
console.log('80 percentile is', histogram.percentile(80))
console.log('99 percentile is', histogram.percentile(99))
console.log(histogram.percentiles())
Histogram
histogram#record()
histogram#min()
histogram#max()
histogram#mean()
histogram#stddev()
histogram#percentile()
histogram#percentiles()
histogram#encode()
histogram#decode()
Create a new histogram with:
lowest
: is the lowest possible number that can be recorded (default 1).max
: is the maximum number that can be recorded (default 100).figures
: the number of figures in a decimal number that will be maintained, must be between 1 and 5 (inclusive) (default 3).
Record value
in the histogram. Returns true
if the recording was
successful, false
otherwise.
Return the minimum value recorded in the histogram.
Return the maximum value recorded in the histogram.
Return the mean of the histogram.
Return the standard deviation of the histogram.
Returns the value at the given percentile. percentile
must be >
0 and <= 100, otherwise it will throw.
Returns all the percentiles.
Sample output:
[ { percentile: 0, value: 1 },
{ percentile: 50, value: 22 },
{ percentile: 75, value: 32 },
{ percentile: 87.5, value: 37 },
{ percentile: 93.75, value: 40 },
{ percentile: 96.875, value: 41 },
{ percentile: 98.4375, value: 42 },
{ percentile: 100, value: 42 } ]
Returns a Buffer
containing a serialized version of the histogram
Reads a Buffer
and deserialize an histogram.
This project was kindly sponsored by nearForm.
The pre-compilation work of this project is only possible because of mapbox's amazing work on node-pre-gyp. A lot of the functionality enabled is following the example set by their node-sqlite3 library.
This library is licensed as MIT
HdrHistogram_c is licensed as BSD license
zlib is licensed as zlib License
The scripts used in the scripts folder are modified BSD licensed scripts from the node-sqlite3 libary.