Class: LatencyMonitor

LatencyMonitor

A class to monitor latency of any async function which works in a browser or node. This works by periodically calling the asyncTestFn and timing how long it takes the callback to be called. It can also periodically emit stats about this. This can be disabled and stats can be pulled via setting dataEmitIntervalMs = 0.

The default implementation is an event loop latency monitor. This works by firing periodic events into the event loop and timing how long it takes to get back.


new LatencyMonitor( [latencyCheckIntervalMs] [, dataEmitIntervalMs] [, asyncTestFn] [, latencyRandomPercentage])

Parameters:
Name Type Argument Default Description
latencyCheckIntervalMs Number <optional>
500

How often to add a latency check event (ms)

dataEmitIntervalMs Number <optional>
5000

How often to summarize latency check events. null or 0 disables event firing

asyncTestFn function <optional>

What cb-style async function to use

latencyRandomPercentage Number <optional>
5

What percent (+/-) of latencyCheckIntervalMs should we randomly use? This helps avoid alignment to other events.

Examples
const monitor = new LatencyMonitor();
monitor.on('data', (summary) => console.log('Event Loop Latency: %O', summary));
const monitor = new LatencyMonitor({latencyCheckIntervalMs: 1000, dataEmitIntervalMs: 60000, asyncTestFn:ping});
monitor.on('data', (summary) => console.log('Ping Pong Latency: %O', summary));

Methods


getSummary()

Calling this function will end the collection period. If a timing event was already fired and somewhere in the queue, it will not count for this time period

Returns:
Type
SummaryObject