perf_hooks.createSlidingWindowHistogram(options)
options<Object>chunks<number> 保留的直方图分块数量。必须是介于1和1024之间的整数。chunkDuration<number> 每个块的持续时间,以毫秒为单位。必须是介于1和18_446_744_073_709之间的整数。必须仅指定chunkDuration或recordsPerChunk中的一个。recordsPerChunk<number> 分配给每个块的record()调用次数。必须是1和Number.MAX_SAFE_INTEGER之间的整数。必须指定chunkDuration和recordsPerChunk中的一个。lowest<number> | <bigint> 最低可识别的值。必须是大于0的整数值。默认值:1。highest<number> | <bigint> 可记录的最高值。必须是一个整数,并且大于或等于lowest的两倍。默认值:Number.MAX_SAFE_INTEGER。figures<number> 精确度的位数。必须是1到5之间的整数。默认值:3。
- 返回:SlidingWindowHistogram
创建一个 SlidingWindowHistogram,它保留最新的 chunks 直方图块。旋转是懒惰的,不会创建计时器。基于时间的旋转在调用 record() 或 snapshot() 时评估。基于数量的旋转在调用 record() 时评估。
🌐 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.
在构建过程中会分配一个直方图块。额外的块会按需分配。窗口使用的最大本地内存会随着 chunks 以及 lowest、highest 和 figures 直方图选项而变化。
🌐 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.
窗口边界具有块级精度。对于持续时间为 D 的 N 个块,记录的值会保留在 (N - 1) * D 到 N * D 毫秒之间。一旦基于计数的窗口被填充,它会保留 (N - 1) * C + 1 到 N * C 次记录尝试,其中 C 是 recordsPerChunk。超过 highest 的记录尝试会在确定基于计数的轮换时被考虑进去。
🌐 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));