NodeJS获取CPU使用率:百分比转换与系统/应用数据区分
Node.js CPU 使用率相关问题解答
问题背景
用户现有代码用于获取Node.js应用的CPU使用时长(微秒转换为秒):
private lastCpuUsage = process.cpuUsage(); getCpuUsage() { const cpuUsage = process.cpuUsage(); const userUsageMicros = cpuUsage.user - this.lastCpuUsage.user; const systemUsageMicros = cpuUsage.system - this.lastCpuUsage.system; this.lastCpuUsage = cpuUsage; console.log({ cpuUserUsage: userUsageMicros / 1e6, // 转换为秒 cpuSystemUsage: systemUsageMicros / 1e6, cpuTotalUsage: (userUsageMicros + systemUsageMicros) / 1e6 }) }
示例输出:
{ "cpuUserUsage": 0.016, "cpuSystemUsage": 0.031, "cpuTotalUsage": 0.047 }
用户提出两个问题:
- 如何将cpuUsage转换为百分比单位?
- 当前输出数据是对应应用本身还是整个主机的?如果仅对应应用,如何获取整机CPU数据?
问题解答
1. 转换为百分比单位
process.cpuUsage()返回的是当前进程占用CPU的绝对时长,要转成百分比,需要结合两次调用的时间差和主机CPU核心数计算:
- 需记录上次调用的时间戳,计算两次调用的实际流逝时间
- 用「进程CPU占用时长 / 流逝时间」得到单核心占比,再乘以核心数得到总占比(因为进程可以同时利用多核心)
修改后的代码示例:
const os = require('os'); private lastCpuUsage = process.cpuUsage(); private lastTimestamp = Date.now(); getCpuUsage() { const cpuUsage = process.cpuUsage(); const currentTimestamp = Date.now(); // 计算两次调用的时间差(微秒) const elapsedMicros = (currentTimestamp - this.lastTimestamp) * 1000; // 计算进程CPU使用时长差值 const userUsageMicros = cpuUsage.user - this.lastCpuUsage.user; const systemUsageMicros = cpuUsage.system - this.lastCpuUsage.system; const totalUsageMicros = userUsageMicros + systemUsageMicros; // 获取CPU核心数 const coreCount = os.cpus().length; // 转换为百分比 const cpuUserPercent = (userUsageMicros / elapsedMicros) * coreCount * 100; const cpuSystemPercent = (systemUsageMicros / elapsedMicros) * coreCount * 100; const cpuTotalPercent = (totalUsageMicros / elapsedMicros) * coreCount * 100; this.lastCpuUsage = cpuUsage; this.lastTimestamp = currentTimestamp; console.log({ cpuUserPercent: cpuUserPercent.toFixed(2), cpuSystemPercent: cpuSystemPercent.toFixed(2), cpuTotalPercent: cpuTotalPercent.toFixed(2) }) }
2. 数据范围与整机CPU获取
- 当前输出的是当前Node.js应用进程自身的CPU使用数据,
process.cpuUsage()仅统计当前进程的CPU占用情况。 - 要获取整机CPU使用率,需用
os模块的os.cpus()接口,该接口返回每个CPU核心的时间统计(包括空闲、用户态、系统态等时长)。通过记录两次统计的差值,计算整机非空闲时间占总时间的比例得到百分比:
示例代码:
const os = require('os'); private lastCpuTimes = this.getTotalCpuTimes(); // 辅助函数:计算所有核心的总CPU时间 getTotalCpuTimes() { const cpus = os.cpus(); return cpus.reduce((total, cpu) => { total.user += cpu.times.user; total.system += cpu.times.sys; total.idle += cpu.times.idle; total.total += cpu.times.user + cpu.times.sys + cpu.times.idle + cpu.times.nice + cpu.times.irq; return total; }, { user: 0, system: 0, idle: 0, total: 0 }); } getHostCpuUsage() { const currentCpuTimes = this.getTotalCpuTimes(); // 计算时间差值 const userDiff = currentCpuTimes.user - this.lastCpuTimes.user; const systemDiff = currentCpuTimes.system - this.lastCpuTimes.system; const idleDiff = currentCpuTimes.idle - this.lastCpuTimes.idle; const totalDiff = currentCpuTimes.total - this.lastCpuTimes.total; // 计算百分比 const cpuUserPercent = (userDiff / totalDiff) * 100; const cpuSystemPercent = (systemDiff / totalDiff) * 100; const cpuIdlePercent = (idleDiff / totalDiff) * 100; const cpuTotalPercent = 100 - cpuIdlePercent; this.lastCpuTimes = currentCpuTimes; console.log({ hostCpuUserPercent: cpuUserPercent.toFixed(2), hostCpuSystemPercent: cpuSystemPercent.toFixed(2), hostCpuIdlePercent: cpuIdlePercent.toFixed(2), hostCpuTotalPercent: cpuTotalPercent.toFixed(2) }) }
内容的提问来源于stack exchange,提问作者great programmer
相关产品推荐
相关产品推荐

