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
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.
To get % in this specific case it is enough to do usageInPercent = (usage. user + usage. system)/500 .
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.
You can profile your app with node-tick.
node-tick
by sudo npm -g install tick
node --prof ./app.js
node-tick-processor
and explain resultsnode js cpu 100%
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With