简体   繁体   English

尝试获取节点进程的标准化 CPU 使用率

[英]Trying to get normalized CPU usage for node process

I'm trying to calculate the normalized cpu percentage for my node process.我正在尝试计算我的节点进程的标准化 cpu 百分比。 My goal is to get it to match the output that I have in my htop for the pid but I'm unable to do so.我的目标是让它与我在htop中的 pid 输出相匹配,但我无法这样做。 My code is below along with my htop output.下面是我的代码以及我的 htop 输出。

import { cpuUsage } from "node:process";
import { cpus } from "os";

function basicCpuUsage() {
  const startTime = process.hrtime();
  const startUsage = cpuUsage();
  const numCpus = cpus().length;

  const add = 1 + 1; // make cpu do work?

  const usageDiff = cpuUsage(startUsage); // get diff time from start
  const endTime = process.hrtime(startTime); // total amount of time that has elapsed
  const usageMS = (usageDiff.user + usageDiff.system) / 1e3;
  const totalMS = endTime[0] * 1e3 + endTime[1] / 1e6;

  const cpuPercent = (usageMS / totalMS) * 100;
  const normTotal = usageMS / numCpus; // average usage time per cpu
  const normPercent = (normTotal / totalMS) * 100;

  console.log({
    cpuPercent: cpuPercent.toFixed(2),
    normPercent: normPercent.toFixed(2),
  });
}

process.title = "CPU Test";
const { pid } = process;
console.log({ pid });
const title = setInterval(() => {
  basicCpuUsage();
}, 1000);

Here is my output, as you can see my code cpu output does no match my htop cpu output.这是我的输出,您可以看到我的代码 cpu 输出与我的 htop cpu 输出不匹配。 Which part of my calculation is incorrect?我的计算哪一部分不正确? I was thinking this might have to do with my setInterval function call, but not sure.我在想这可能与我的setInterval函数调用有关,但不确定。 I am attempting to create a long running process where I can view the cpu usage.我正在尝试创建一个长时间运行的进程,我可以在其中查看 cpu 使用情况。

顶层输出

Turns out I was making an incorrect calculation.结果我计算错了。 I was dividing my totalTimeMS twice when I should have done it once.当我应该做一次时,我将我的totalTimeMS划分了两次。 Additionally I moved the currentUsage and currentTime to the outside of the function.此外,我将 currentUsage 和 currentTime 移到了函数的外部。

Below is the correct calculation:下面是正确的计算:

import { cpus } from "os";

let currentUsage = process.cpuUsage();
let currentTime = process.hrtime();

function basicCpuUsage() {
  const numCpus = cpus().length;

  const usageDiff = process.cpuUsage(currentUsage); // get diff time from start
  const endTime = process.hrtime(currentTime); // total amount of time that has elapsed

  currentUsage = process.cpuUsage()
  currentTime = process.hrtime();

  const usageMS = (usageDiff.user + usageDiff.system) / 1e3;
  const totalMS = endTime[0] * 1e3 + endTime[1] / 1e6;

  const cpuPercent = (usageMS / totalMS) * 100;
  const normPercent = (usageMS / totalMS / numCpus) * 100; // average usage time per cpu

  console.log({
    cpuPercent: cpuPercent.toFixed(2),
    normPercent: normPercent.toFixed(2),
  });
}

process.title = "CPU Test";

setInterval(() => {
 for (let i = 0; i < 25; i++) {
    const add = 1 + 1;
  }
  basicCpuUsage();
}, 5000);

CPU使用率

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM