performance.eventLoopUtilization([utilization1[, utilization2]])
utilization1<Object> 前一次调用eventLoopUtilization()的结果。utilization2<Object> 在utilization1之前对eventLoopUtilization()的先前调用的结果。- 返回:<Object>
eventLoopUtilization() 方法返回一个对象,该对象包含事件循环在空闲和活动状态下的累计时间,以高精度毫秒计时器表示。utilization 值是计算得到的事件循环利用率(ELU)。
【The eventLoopUtilization() method returns an object that contains the
cumulative duration of time the event loop has been both idle and active as a
high resolution milliseconds timer. The utilization value is the calculated
Event Loop Utilization (ELU).】
如果主线程上的自举尚未完成,这些属性的值为 0。由于自举在事件循环中进行,ELU 在 工作线程 上可以立即使用。
【If bootstrapping has not yet finished on the main thread the properties have
the value of 0. The ELU is immediately available on Worker threads since
bootstrap happens within the event loop.】
utilization1 和 utilization2 都是可选参数。
【Both utilization1 and utilization2 are optional parameters.】
如果传入 utilization1,则会计算并返回当前调用的 active 和 idle 时间之间的差值,以及相应的 utilization 值(类似于 process.hrtime())。
【If utilization1 is passed, then the delta between the current call's active
and idle times, as well as the corresponding utilization value are
calculated and returned (similar to process.hrtime()).】
如果同时传入 utilization1 和 utilization2,则会计算这两个参数之间的差值。这是一个方便的选项,因为与 process.hrtime() 不同,计算 ELU 比单一减法更复杂。
【If utilization1 and utilization2 are both passed, then the delta is
calculated between the two arguments. This is a convenience option because,
unlike process.hrtime(), calculating the ELU is more complex than a
single subtraction.】
ELU 类似于 CPU 利用率,不同之处在于它只衡量事件循环的统计数据,而不包括 CPU 使用情况。它表示事件循环在事件提供者(例如 epoll_wait)之外所花费时间的百分比。其他的 CPU 空闲时间不予考虑。下面是一个示例,说明一个大部分时间处于空闲状态的进程将具有较高的 ELU。
【ELU is similar to CPU utilization, except that it only measures event loop
statistics and not CPU usage. It represents the percentage of time the event
loop has spent outside the event loop's event provider (e.g. epoll_wait).
No other CPU idle time is taken into consideration. The following is an example
of how a mostly idle process will have a high ELU.】
import { eventLoopUtilization } from 'node:perf_hooks';
import { spawnSync } from 'node:child_process';
setImmediate(() => {
const elu = eventLoopUtilization();
spawnSync('sleep', ['5']);
console.log(eventLoopUtilization(elu).utilization);
});'use strict';
const { eventLoopUtilization } = require('node:perf_hooks').performance;
const { spawnSync } = require('node:child_process');
setImmediate(() => {
const elu = eventLoopUtilization();
spawnSync('sleep', ['5']);
console.log(eventLoopUtilization(elu).utilization);
});虽然在运行此脚本时 CPU 大部分处于空闲状态,但 utilization 的值是 1。这是因为对 child_process.spawnSync() 的调用阻塞了事件循环的进行。
【Although the CPU is mostly idle while running this script, the value of
utilization is 1. This is because the call to
child_process.spawnSync() blocks the event loop from proceeding.】
传入用户定义的对象而不是之前对 eventLoopUtilization() 调用的结果将导致未定义行为。返回值不能保证反映事件循环的任何正确状态。
【Passing in a user-defined object instead of the result of a previous call to
eventLoopUtilization() will lead to undefined behavior. The return values
are not guaranteed to reflect any correct state of the event loop.】