如何找出 Node.js 进程的 CPU 使用百分比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7773826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out the % CPU usage for Node.js process?
提问by murvinlai
is there a way to find out the % cpu usage for a node.js process with the code? so that when the node.js server is running and detect the CPU is over certain%, then it will put an alert or console output.
有没有办法通过代码找出 node.js 进程的 cpu 使用百分比?这样当 node.js 服务器运行并检测到 CPU 超过一定百分比时,它就会发出警报或控制台输出。
采纳答案by CrazyDart
Try looking at this code: https://github.com/last/healthjs
尝试查看此代码:https: //github.com/last/healthjs
Network service for getting CPU of remote system and receiving CPU usage alerts...
Health.js serves 2 primary modes: "streaming mode" and "event mode". Streaming mode allows a client to connect and receive streaming CPU usage data. Event mode enables Health.js to notify a remote server when CPU usage hits a certain threshold. Both modes can be run simultaneously...
用于获取远程系统的 CPU 和接收 CPU 使用率警报的网络服务...
Health.js 提供两种主要模式:“流模式”和“事件模式”。流式模式允许客户端连接并接收流式 CPU 使用率数据。事件模式使 Health.js 能够在 CPU 使用率达到某个阈值时通知远程服务器。两种模式可以同时运行...
回答by Zack Bloom
On *nix systems can get process stats by reading the /proc/[pid]/stat virtual file.
在 *nix 系统上可以通过读取 /proc/[pid]/stat 虚拟文件来获取进程统计信息。
For example this will check the CPU usage every ten seconds, and print to the console if it's over 20%. It works by checking the number of cpu ticks used by the process and comparing the value to a second measurement made one second later. The difference is the number of ticks used by the process during that second. On POSIX systems, there are 10000 ticks per second (per processor), so dividing by 10000 gives us a percentage.
例如,这将每十秒检查一次 CPU 使用率,如果超过 20%,则打印到控制台。它的工作原理是检查进程使用的 cpu 滴答数,并将该值与一秒后的第二次测量进行比较。不同之处在于该进程在该秒内使用的滴答数。在 POSIX 系统上,每秒(每个处理器)有 10000 个滴答,因此除以 10000 得出一个百分比。
var fs = require('fs');
var getUsage = function(cb){
fs.readFile("/proc/" + process.pid + "/stat", function(err, data){
var elems = data.toString().split(' ');
var utime = parseInt(elems[13]);
var stime = parseInt(elems[14]);
cb(utime + stime);
});
}
setInterval(function(){
getUsage(function(startTime){
setTimeout(function(){
getUsage(function(endTime){
var delta = endTime - startTime;
var percentage = 100 * (delta / 10000);
if (percentage > 20){
console.log("CPU Usage Over 20%!");
}
});
}, 1000);
});
}, 10000);
回答by Garrows
You can use the os module now.
您现在可以使用 os 模块。
var os = require('os');
var loads = os.loadavg();
This gives you the load average for the last 60seconds, 5minutes and 15minutes. This doesnt give you the cpu usage as a % though.
这为您提供了过去 60 秒、5 分钟和 15 分钟的平均负载。不过,这不会以 % 的形式为您提供 CPU 使用率。
回答by Arunoda Susiripala
see node-usagefor tracking process CPU and Memory Usage (not the system)
请参阅node-usage以跟踪进程 CPU 和内存使用情况(不是系统)
回答by fider
Use node process.cpuUsagefunction (introduced in node v6.1.0). It shows time that cpu spent on your node process. Example taken from docs:
使用 node process.cpuUsage函数(在 node v6.1.0 中引入)。它显示了 CPU 在您的节点进程上花费的时间。取自文档的示例:
const previousUsage = process.cpuUsage();
// { user: 38579, system: 6986 }
// spin the CPU for 500 milliseconds
const startDate = Date.now();
while (Date.now() - startDate < 500);
// At this moment you can expect result 100%
// Time is *1000 because cpuUsage is in us (microseconds)
const usage = process.cpuUsage(previousUsage);
const result = 100 * (usage.user + usage.system) / ((Date.now() - startDate) * 1000)
console.log(result);
// set 2 sec "non-busy" timeout
setTimeout(function() {
console.log(process.cpuUsage(previousUsage);
// { user: 514883, system: 11226 } ~ 0,5 sec
// here you can expect result about 20% (0.5s busy of 2.5s total runtime, relative to previousUsage that is first value taken about 2.5s ago)
}, 2000);
回答by Krzysztof Kazmierczyk
Another option is to use node-red-contrib-ospackage
另一种选择是使用node-red-contrib-os包

