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.

example

const monitor = new LatencyMonitor(); monitor.on('data', (summary) => console.log('Event Loop Latency: %O', summary));

example

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

Hierarchy

  • EventEmitter
    • LatencyMonitor

Index

References

LatencyMonitorOptions

Re-exports LatencyMonitorOptions

SummaryObject

Re-exports SummaryObject

Constructors

constructor

  • new LatencyMonitor(__namedParameters?: { asyncTestFn: undefined | Function; dataEmitIntervalMs: undefined | number; latencyCheckIntervalMs: undefined | number; latencyRandomPercentage: undefined | number }): LatencyMonitor
  • Parameters

    • Optional __namedParameters: { asyncTestFn: undefined | Function; dataEmitIntervalMs: undefined | number; latencyCheckIntervalMs: undefined | number; latencyRandomPercentage: undefined | number }
      • asyncTestFn: undefined | Function
      • dataEmitIntervalMs: undefined | number
      • latencyCheckIntervalMs: undefined | number
      • latencyRandomPercentage: undefined | number

    Returns LatencyMonitor

Properties

Private _checkLatency

_checkLatency: any

Randomly calls an async fn every roughly latencyCheckIntervalMs (plus some randomness). If no async fn is found, it will simply report on event loop latency.

_checkLatencyID

_checkLatencyID: Timeout | undefined

_emitIntervalID

_emitIntervalID: Timeout | undefined

Private _emitSummary

_emitSummary: any

Emit summary only if there were events. It might not have any events if it was forced via a page hidden/show

_latencyData

_latencyData: { events: number; maxMs: number; minMs: number; startTime: any; totalMs: number } | undefined

Private _startTimers

_startTimers: any

Start internal timers

Private _stopTimers

_stopTimers: any

Stop internal timers

Static defaultMaxListeners

defaultMaxListeners: number

Static Readonly errorMonitor

errorMonitor: unique symbol

This symbol shall be used to install a listener for only monitoring 'error' events. Listeners installed using this symbol are called before the regular 'error' listeners are called.

Installing a listener using this symbol does not change the behavior once an 'error' event is emitted, therefore the process will still crash if no regular 'error' listener is installed.

Methods

_initLatencyData

  • _initLatencyData(): { events: number; maxMs: number; minMs: number; startTime: any; totalMs: number }
  • Returns { events: number; maxMs: number; minMs: number; startTime: any; totalMs: number }

    • events: number
    • maxMs: number
    • minMs: number
    • startTime: any
    • totalMs: number

addListener

  • addListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

eventNames

  • eventNames(): Array<string | symbol>

getMaxListeners

  • getMaxListeners(): number

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 SummaryObject

listenerCount

  • listenerCount(event: string | symbol): number

listeners

  • listeners(event: string | symbol): Function[]

off

  • off(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

on

  • on(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

once

  • once(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependListener

  • prependListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

  • rawListeners(event: string | symbol): Function[]

removeAllListeners

  • removeAllListeners(event?: string | symbol): this

removeListener

  • removeListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

setMaxListeners

  • setMaxListeners(n: number): this

Static listenerCount

  • listenerCount(emitter: EventEmitter, event: string | symbol): number
  • deprecated

    since v4.0.0

    Parameters

    • emitter: EventEmitter
    • event: string | symbol

    Returns number