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.
technical spec:
node version 0.8.14
ubuntu 11.10
Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
gems 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",
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 resultsthis 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
This feels like a repost to this question. You will need to write a script to record every request and then replay it. When you find the function that is causing the trouble you can troubleshoot it.