如何调试导致 100% cpu 使用率的 node.js?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10167477/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:32:51  来源:igfitidea点击:

how to debug node.js causing 100% cpu usage?

debuggingnode.jsexpress

提问by Tim

I have a node app that uses express and redis. On our development server, after a bit of use node starts to use 100% cpu. The application still responds but top reports node using 100%. The cpu doesn't drop until node is restarted.

我有一个使用 express 和 redis 的节点应用程序。在我们的开发服务器上,使用一段时间后,节点开始使用 100% 的 CPU。应用程序仍然响应,但顶部报告节点使用 100%。在节点重新启动之前,cpu 不会下降。

I have not nailed it down to any particular route or function that is causing it.

我没有将其确定为导致它的任何特定路线或功能。

What is the best way to diagnose this problem?

诊断此问题的最佳方法是什么?

I looked at node-inspector with the v8-profiler and it gave me the same error that is reported here https://github.com/dannycoates/v8-profiler/issues/10

我用 v8-profiler 查看了节点检查器,它给了我这里报告的相同错误 https://github.com/dannycoates/v8-profiler/issues/10

回答by laggingreflex

You can profile your app with node-tick.

您可以使用node-tick分析您的应用程序。

  1. Install node-tickby sudo npm -g install tick
  2. Run your app with enabled profile node --prof ./app.js
  3. After some time with CPU 100% usage stop your app
  4. You can see v8.log in your app directory, now you can read it with node-tick-processor
  5. Run node-tick-processorand explain results
  6. Load v8.log into chrome://tracing to analyse as tree.
  1. 安装node-tick方式sudo npm -g install tick
  2. 使用启用的配置文件运行您的应用程序 node --prof ./app.js
  3. 在 CPU 100% 使用一段时间后停止您的应用程序
  4. 您可以在您的应用程序目录中看到 v8.log,现在您可以使用 node-tick-processor 读取它
  5. 运行node-tick-processor并解释结果
  6. 将 v8.log 加载到 chrome://tracing 以分析为树。

node js cpu 100%

节点 js cpu 100%

回答by Tim

I found the problem by writing a script to record every request and then replay them.

我通过编写脚本来记录每个请求然后重放它们来发现问题。

The problem was caused because I had a callback that was not being returned.

问题是因为我有一个没有被返回的回调。

myAsncFunc(function(err, data) {

    if (err) { callback(err) }

    //node kept going after the error was returned to the user.
    // make sure you, return callback(err)

})

Here was my replay.js code for anyone interested.

这是我为感兴趣的人提供的 replay.js 代码。

var request = require('request');
var async = require('async');
var redis = require('redis');


var host = 'http://myhost.com';
var jobs = true;

var client = redis.createClient();

async.whilst(
    function () { return jobs; },
    function (callback) {
        client.lpop('history', function(err, url) {
            console.log(url);
            if (!url) {
                jobs = false;
                callback();
            }
            request.get({url:host+url}, function() {
                callback();
            });
        })
    },
    function (err) {
        console.log('done')
    }
);

And in you're express app.

在你的快递应用程序中。

app.get('/*', function(req, res, next) {
    var url = req.originalUrl;
    redis.rpush('history', url);   
    next();
});

It's cool because every history item that is played will be added again to the queue so it continually loops and every time you visit a new page, it will add that one to the queue.

这很酷,因为播放的每个历史项目都将再次添加到队列中,因此它会不断循环,每次访问新页面时,它都会将该页面添加到队列中。

回答by Valentin Heinitz

I experienced also 100% CPU usage till i switched off supervisor mode (causing node to restart, when a file changes).

我也经历了 100% CPU 使用率,直到我关闭主管模式(导致节点重新启动,当文件更改时)。

This probably does not answer this question, but in case some novice like me worries about CPU usage, this could be the case.

这可能不能回答这个问题,但如果像我这样的新手担心 CPU 使用率,情况可能就是这样。

回答by akim lyubchenko

If you are using UI app with webpack, pay attention on watchOptionsor watch. For me, disabling poll solve the problem

如果你使用带有 webpack 的 UI 应用程序,请注意watchOptionswatch。对我来说,禁用投票可以解决问题

watchOptions: {
            poll: false
        }

Or you can set a time, when poll will be triggered like poll: 3000(once in 3sec) https://webpack.js.org/configuration/watch/#watchoptionsignored

或者您可以设置一个时间,何时触发轮询poll: 3000(每 3 秒一次) https://webpack.js.org/configuration/watch/#watchoptionsignored

回答by STREET MONEY

In case you are using nodemonto watch over your files, kindly consider using paths to folders with less files. e.g. Letting nodemonwatch library folders installed with bower or npm causes the high CPU usage due to thousands of files contained in there.

如果您nodemon用来监视文件,请考虑使用文件较少的文件夹的路径。例如,让nodemon使用 bower 或 npm 安装的监视库文件夹会导致 CPU 使用率高,因为其中包含数千个文件。

Here is my sample nodemon.jsonfile:

这是我的示例nodemon.json文件:

{
    "watch": ["views","routes"],
    "ext": "html, js"
}

Works like a charm.

奇迹般有效。

回答by Jakub Oboza

maybe you have some computation somewhere using nextTickthat is trashing CPU constantly.

也许你在某个地方有一些计算nextTick正在不断地破坏 CPU。

If you can't run profile then its hard to find out which method is trashing cpu. One more thing is to examine express log by using logger middleware http://senchalabs.github.com/connect/middleware-logger.html

如果您无法运行配置文件,则很难找出哪种方法正在破坏 CPU。另一件事是使用记录器中间件http://senchalabs.github.com/connect/middleware-logger.html检查快速日志

回答by user9564965

It maybe because you are amounts of files in the direct. e.g. node_modules folder. you need to use the -i param to ignore that folder. so it should just like this: supervisor -i ./node_modules app.

这可能是因为您直接拥有大量文件。例如 node_modules 文件夹。您需要使用 -i 参数来忽略该文件夹。所以它应该就像这样: supervisor -i ./node_modules app