Wednesday, September 22, 2021

Investigate Node.js High CPU issue in Linux App Service

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:

  • How to install and inject v8 CPU profiler in your Node.js application code
  • How to capture CPU profiler dump in Linux App Service
  • How to use Google Chrome Developer tools to analyze the profiler file.

 

How to install and inject v8 CPU profiler in your Node.js application code

  1. Install the "v8-profiler-node8" module
    In general, we just need to use npm to install the "heapdump" module 
    npm install v8-profiler-node8​

    For your Node.js application going to deploy to Azure App Service, we can define it in your package.json "dependencies" section like the following:
    Hanli_Ren_1-1630739142227.png

     

  2. Then, you need to load the v8-profiler-node8 module into your application

    For example, in my Node Express project, I put the following line in my app.js file.

    var heapdump = require(' v8-profiler-node8 ')

    Hanli_Ren_2-1630739235879.png

     

  3. To make the CPU profiler able to be controlled by process Signal, we can use the following code in the app.js file
    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);
    

    Hanli_Ren_3-1630739475716.png


How to capture CPU profiler dump in Linux App Service

  1. Login to your Node.js Linux App Service WEBSSH console.
    https://<webapp-name>.scm.azurewebsites.net/webssh/host

     

  2. Find your Node.js application process ID
    # ps aux | grep node 

    Hanli_Ren_1-1630739999129.png

     

  3. Capture CPU profiler while running performance test on the website.
    ○ Start the CPU profiling by send the First -SIGUSR1 signal to the running Node process
    ○ Run test on the website
       For example:
       Accessing the https://<web-app-name>.azurewebsites.net/highCpuCall
    ○ Stop the CPU profiling by send the Second -SIGUSR1 signal to the same Node process
       The v8 profiler will generate a v8-<timestamp>.cpuprofile file under /home/site/wwwroot folder.
    Hanli_Ren_0-1630739926397.png

     

How to use Google Chrome Developer tools to analyze the profiler file

  1. In you local machine, open your Google Chrome Browser.
    Then use F12 to open Chrome Dev Tools.
    Find and open "More tools" -> "JavaScript Profiler"
    Hanli_Ren_0-1630740149705.png

     

    Load the v8-<timestamp>.cpuprofile file into it:
    Hanli_Ren_1-1630740173691.png

     

  2. You can use "Heavy(Bottom Up)" view to check those .js files and functions that consumed most of the CPU time
    Hanli_Ren_2-1630740262059.pngYou can also use "Chart" view to find the function that consumed high CPU time.
    Hanli_Ren_3-1630740284393.png

     

Posted at https://sl.advdat.com/3zxuiPg