When running your Node.js application in Azure Linux App Service, you may encounter High CPU consumption issue.
v8-profiler-node8 is one of the tools that can help us profile the CPU usage of a Node.js application.
Normally, we need to explicitly insert code to control where to start and stop profiling in the application code. But for complex applications running in production mode, it's hard to decide at which position of the code to start/stop profiling. Also, it will generate too many profiler result files if we continuously profiling a running App Service.
This article describes an idea of how to control the v8 profiler using process signal in Linux App Service.
You can find the detailed steps of:
npm install v8-profiler-node8
For example, in my Node Express project, I put the following line in my app.js file.
var heapdump = require(' v8-profiler-node8 ')
const profiler = require('v8-profiler-node8');
const fs = require('fs');
var profilerRunning = false;
function toggleProfiling () {
if (profilerRunning) {
const profile = profiler.stopProfiling();
console.log('>>>>stopped profiling at '+Date.now());
profile.export()
.pipe(fs.createWriteStream('./v8-'+Date.now()+'.cpuprofile'))
.once('error', profiler.deleteAllProfiles)
.once('finish', profiler.deleteAllProfiles);
profilerRunning = false;
return;
}
profiler.startProfiling();
profilerRunning = true;
console.log('>>>>>>started profilingat '+Date.now());
}
process.on('SIGUSR1', toggleProfiling);
# ps aux | grep node
Load the v8-<timestamp>.cpuprofile file into it:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.