On this page

    M

    perf_hooks.createSlidingWindowHistogram

    History
    perf_hooks.createSlidingWindowHistogram(options): SlidingWindowHistogram
    Attributes
    options:Object
    chunks:number
    The number of histogram chunks retained. Must be an integer between 1 and 1024.
    chunkDuration:number
    The duration of each chunk in milliseconds. Must be an integer between 1 and 18_446_744_073_709. Exactly one of chunkDuration and recordsPerChunk must be specified.
    recordsPerChunk:number
    The number of calls to record() assigned to each chunk. Must be an integer between 1 and Number.MAX_SAFE_INTEGER. Exactly one of chunkDuration and recordsPerChunk must be specified.
    lowest?:number | bigint
    The lowest discernible value. Must be an integer value greater than 0. Default: 1.
    highest?:number | bigint
    The highest recordable value. Must be an integer value that is equal to or greater than two times lowest. Default: Number.MAX_SAFE_INTEGER.
    figures?:number
    The number of accuracy digits. Must be an integer between 1 and 5. Default: 3.
    Returns:SlidingWindowHistogram

    Creates a SlidingWindowHistogram that retains the latest chunks histogram chunks. Rotation is lazy and does not create a timer. Time-based rotation is evaluated when record() or snapshot() is called. Count-based rotation is evaluated when record() is called.

    One histogram chunk is allocated during construction. Additional chunks are allocated lazily. The maximum native memory used by the window scales with chunks and with the lowest, highest, and figures histogram options.

    The window boundary has chunk-level precision. With N chunks of duration D, a recorded value is retained for between (N - 1) * D and N * D milliseconds. Once a count-based window is populated, it retains between (N - 1) * C + 1 and N * C recording attempts, where C is recordsPerChunk. Recording attempts which exceed highest are included when determining count-based rotation.

    const { createSlidingWindowHistogram } = require('node:perf_hooks');
    
    const window = createSlidingWindowHistogram({
      chunks: 6,
      chunkDuration: 10_000,
    });
    
    window.record(20_000_000);
    
    // Materialize the current window as an independent Histogram.
    const snapshot = window.snapshot();
    console.log(snapshot.percentile(99));