Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

like image 305
Tim Avatar asked Apr 16 '12 00:04

Tim


People also ask

Why is Node taking up so much CPU?

Huge payloads from Node. js services also can be a problem, because Node. js stringifies objects to JSON first and then sends them to the client. All of these operations can cause high CPU, make sure that payload size is not huge, use pagination, and don't prepopulate unnecessary data.

How do I check CPU usage in node JS?

To get % in this specific case it is enough to do usageInPercent = (usage. user + usage. system)/500 .

How do I debug a node js file?

Open the starting file (typically index. js ), activate the Run and Debug pane, and click the Run and Debug Node. js (F5) button. The debugging screen is similar to Chrome DevTools with a Variables, Watch, Call stack, Loaded scripts, and Breakpoints list.


2 Answers

You can profile your app with node-tick.

  1. Install node-tick by 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-processor and explain results
  6. Load v8.log into chrome://tracing to analyse as tree.

node js cpu 100%

like image 185
3 revs, 2 users 89% Avatar answered Sep 23 '22 13:09

3 revs, 2 users 89%


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.

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.

like image 29
Tim Avatar answered Sep 19 '22 13:09

Tim