CPUUsage 对象
percentCPUUsagenumber - Percentage of CPU used since the last call to the API that returned this object. 第一次调用返回 0。cumulativeCPUUsagenumber (可选) - 自进程启动以来使用的 CPU 时间总秒数。idleWakeupsPerSecondnumber - The number of average idle CPU wakeups per second since the last call to the API that returned this object. 第一次调用返回 0。 Will always return 0 on Windows.
Both percentCPUUsage and idleWakeupsPerSecond are averages rather than instant samples, and each call starts a fresh measurement interval.
Multiple APIs return CPUUsage objects, and each tracks its own intervals:
process.getCPUUsage()measures the process it is called in. Only calls toprocess.getCPUUsage()in that same process reset its interval.app.getAppMetrics()measures every process in the app, as thecpuproperty of each ProcessMetric. It keeps a separate interval for each process ID, and a single call resets all of them at once, since one call reports on every process.
[!NOTE] Each API's measurement interval is independent. Calling
process.getCPUUsage()does not change whatapp.getAppMetrics()reports and vice-versa. The main process can therefore report differentpercentCPUUsagevalues through each API at the same moment.
[!NOTE] If one of your app's dependencies calls one of these functions, it will start a new interval before you read it. To measure an interval you control, use
cumulativeCPUUsageinstead. Subtract an earlier reading from a later one to get usage over that span. Reading it still means callingapp.getAppMetrics()orprocess.getCPUUsage(), which starts a new interval for everyone else.