perf_hooks.monitorEventLoopDelay([options])


  • options <Object>
    • samplePerIteration <boolean>true 时,每次事件循环迭代都会采样一次。默认值: false
    • resolution <number> 用于基于间隔采样的采样率(毫秒)。必须大于零。当 samplePerIterationtrue 时,此选项将被忽略。默认值: 10
  • 返回:ELDHistogram

此属性是 Node.js 的扩展。在 Web 浏览器中不可用。

🌐 This property is an extension by Node.js. It is not available in Web browsers.

创建一个直方图对象,用来采样并报告事件循环随时间的延迟。这些延迟将以纳秒为单位报告。

🌐 Creates a histogram object that samples and reports the event loop delay over time. The delays will be reported in nanoseconds.

默认情况下,直方图会通过使用配置好的resolution的定时器来更新。当samplePerIterationtrue时,会使用uv_prepare_tuv_check_t钩子在每次事件循环迭代中采样。在这种模式下,直方图不会保持循环运行,也不会在应用空闲时强制进行额外的迭代。这两种采样模式产生的结果差异很大,不应直接比较。

🌐 By default, the histogram is updated by a timer using the configured resolution. When samplePerIteration is true, samples are taken once per event loop iteration using uv_prepare_t and uv_check_t hooks. In that mode, the histogram does not keep the loop alive or force additional iterations when the application is idle. The two sampling modes produce significantly different results and should not be compared directly.

import { monitorEventLoopDelay } from 'node:perf_hooks';

const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
// Do something.
h.disable();
console.log(h.min);
console.log(h.max);
console.log(h.mean);
console.log(h.stddev);
console.log(h.percentiles);
console.log(h.percentile(50));
console.log(h.percentile(99));const { monitorEventLoopDelay } = require('node:perf_hooks');
const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
// Do something.
h.disable();
console.log(h.min);
console.log(h.max);
console.log(h.mean);
console.log(h.stddev);
console.log(h.percentiles);
console.log(h.percentile(50));
console.log(h.percentile(99));