A generic latency monitor for node/browers
npm install latency-monitor[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url]
> A monitor that periodically times how long it takes for a callback to be called. Think event loop latency.
Example event loop monitor (default).
``javascript
import LatencyMonitor from 'latency-monitor';
const monitor = new LatencyMonitor();
console.log('Event Loop Latency Monitor Loaded: %O', {
latencyCheckIntervalMs: monitor.latencyCheckIntervalMs,
dataEmitIntervalMs: monitor.dataEmitIntervalMs
});
monitor.on('data', (summary) => console.log('Event Loop Latency: %O', summary));
/*
* In console you will see something like this:
* Event Loop Latency Monitor Loaded:
* {dataEmitIntervalMs: 5000, latencyCheckIntervalMs: 500}
* Event Loop Latency:
* {avgMs: 3, events: 10, maxMs: 5, minMs: 1, lengthMs: 5000}
*/
`setTimeout
More Theory
We use to pick when to run the next test. We do this so we can add in some randomness to avoid aligningcb
our events with some external event (e.g. another timer that triggers a slow event). When we are monitoring event loop latency
(i.e. no async function provided), then we simply record how long getting the callback really took.
When we measure an async function, we only time how long that async function took to call the passed in .
When used in a browser, this tool disables itself if the page is hidden because of restrictions with how often we can
call setTimeout see this.
When monitoring event loop latency, we add in 1ms to all measurements. setTimeout is not more accurate than 1ms, so this ensures
every number is greater than 0. To remove this offset, simply subtract 1 from all stats.
TLDR; event loop latency monitoring does NOT have sub-millisecond accuracy, even if the emitted numbers show this.
Install latency-monitor as a dependency:
`shell`
npm install --save latency-monitor
Load via script element (paste into page's html):`html`
Load via script in JavaScript (paste in your devtools - e.g. Chrome DevTools):`javascript`
var el = document.createElement('script');
el.setAttribute('src', 'https://cdn.jsdelivr.net/latency-monitor/0.2.1/EventLoopPrinterWebpacked.js')
document.body.appendChild(el)
Load via jquery (if you have $ loaded on the page already, paste this in Chrome DevTools or in your code):`javascript`
$.getScript('//cdn.jsdelivr.net/latency-monitor/0.2.1/EventLoopPrinterWebpacked.js');
Last resort: Load via XMLHTTPRequest:
`javascript`
var xhr = new XMLHttpRequest();
xhr.open("GET", "//cdn.jsdelivr.net/latency-monitor/0.2.1/EventLoopPrinterWebpacked.js", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// WARNING! Might be evaluating an evil script!
var resp = eval(xhr.responseText);
}
};
xhr.send();
Note: Remember to change 0.2.1 above to whatever is the latest version of latency-monitor.
or in a browser localStorage.debug='latency-monitor'` to see debugging output.License
-------------
Apache-2.0 License
[npm-url]: https://npmjs.org/package/latency-monitor
[npm-image]: https://badge.fury.io/js/latency-monitor.svg
[travis-url]: http://travis-ci.org/mlucool/latency-monitor
[travis-image]: https://secure.travis-ci.org/mlucool/latency-monitor.png?branch=master
[coveralls-url]: https://coveralls.io/github/mlucool/latency-monitor?branch=master
[coveralls-image]: https://coveralls.io/repos/mlucool/latency-monitor/badge.svg?branch=master&service=github
[depstat-url]: https://david-dm.org/mlucool/latency-monitor
[depstat-image]: https://david-dm.org/mlucool/latency-monitor.png