node.js 节点 js cpu 100%
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13375735/
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
node js cpu 100%
提问by dortzur
We're having a problem where every once in a while one of our environments our node app runs on 100% CPU. The server isn't very active and usually runs on 0%-2% CPU. I was wondering what are the common issues that might cause this problem and what would be the best way to find out what causing this issue.
我们遇到了一个问题,我们的节点应用程序每隔一段时间就会在 100% 的 CPU 上运行。服务器不是很活跃,通常在 0%-2% 的 CPU 上运行。我想知道可能导致此问题的常见问题是什么,以及找出导致此问题的原因的最佳方法是什么。
Server specs:
服务器规格:
node version 0.8.14
ubuntu 11.10
Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
节点版本 0.8.14
ubuntu 11.10
Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
Node packages used:
使用的节点包:
"express" : 2.5.x,
"log" : "1.2.x",
"redis" : "0.8.x",
"socket.io" : "0.9.x",
"mongodb": ">= 0.9.6-7",
"passport" : "0.x.x",
"passport-local" : "0.x.x",
回答by Vadim Baryshev
You can profile your app with node-tick.
您可以使用node-tick分析您的应用程序。
- Install
node-tickbynpm -g install tick - Run your app with enabled profile
node --prof ./app.js - After some time with CPU 100% usage stop your app
- You can see v8.log in your app directory, now you can read it with node-tick-processor
- Run
node-tick-processorand explain results
- 安装
node-tick方式npm -g install tick - 使用启用的配置文件运行您的应用程序
node --prof ./app.js - 在 CPU 100% 使用一段时间后停止您的应用程序
- 您可以在您的应用程序目录中看到 v8.log,现在您可以使用 node-tick-processor 读取它
- 运行
node-tick-processor并解释结果
回答by ZEE
UPDATE 2019 !!
2019 年更新!!
You better use the built in --prof-processto process v8 profiling data, the generated file is no longer v8.log and node-tick-processorwon't help you much, so to profile your app and read the v8 profiling data you can proceed as follows:
您最好使用内置--prof-process来处理 v8 分析数据,生成的文件不再是 v8.log 并且node-tick-processor对您没有太大帮助,因此要分析您的应用程序并读取 v8 分析数据,您可以按以下步骤操作:
node --prof your_script.js
Node will generate a file with a name like this isolate-0x103800000-v8.login your current directory, now you use this file to generate a profiling report
Node 会isolate-0x103800000-v8.log在你当前的目录中生成一个名字像这样的文件,现在你使用这个文件来生成一个分析报告
node --prof-process isolate-0x103800000-v8.log > processed.txt
回答by akim lyubchenko
If you are using UI app with webpack, pay attention on watchOptionsor watchFor me, disabling poll solve the problem
如果你使用带有 webpack 的 UI 应用程序,请注意watchOptions或者watch对我来说,禁用 poll 解决问题
watchOptions: {
poll: false
}
https://webpack.js.org/configuration/watch/#watchoptionsignored
https://webpack.js.org/configuration/watch/#watchoptionsignored
回答by thegrunt
this is what i found:
这是我发现的:
#!/usr/bin/env node
require(__dirname+"/processor-usage.js").startWatching();
var shouldRun = true;
var desiredLoadFactor = .5;
function blockCpuFor(ms) {
var now = new Date().getTime();
var result = 0
while(shouldRun) {
result += Math.random() * Math.random();
if (new Date().getTime() > now +ms)
return;
}
}
function start() {
shouldRun = true;
blockCpuFor(1000*desiredLoadFactor);
setTimeout(start, 1000* (1 - desiredLoadFactor));
}
setInterval(function() {
console.log("current process cpu usage: "+(global.processCpuUsage || 0)+"%");}
, 1000);
if (process.argv[2]) {
var value = parseFloat(process.argv[2]);
if (value < 0 || value > 1) {
console.log("please give desired load value as a range [0..1]");
process.exit(-1);
} else {
desiredLoadFactor = value;
}
}
start();
on http://blackholethought.blogspot.de/2012/08/measuring-cpu-usage-of-nodejs-from.html
在http://blackholethought.blogspot.de/2012/08/measuring-cpu-usage-of-nodejs-from.html

