Abstract: Using Easy-Monitor, you can pinpoint performance bottlenecks in Node.js applications and help optimize code performance.

When an application has a performance problem, the biggest question is: How do you pinpoint the code that is causing the performance bottleneck? For Node.js developers, here’s a recommendation for Easy-Monitor, which should be developed by a post-90s programmer at Alibaba. This NPM module helps us quickly locate performance bottlenecks.

The problem

When the load is high, the response Time of a back-end module is much slower, and even the timeout error “504 Gateway Time” appears. By looking at the monitoring, you can see that this module is at full CPU usage at peak times, which could be the problem.

Access to theEasy-Monitor

Accessing easy-Monitor is as simple as importing it from the entry js file:


                                                        
if (process.env.NODE_ENV === "development")
{
     const easyMonitor = require("easy-monitor");
     easyMonitor("backend");
}


                                                    Copy the code

Start the application, visit: http://localhost:12333/index, you can view the Easy – Monitor UI interface:






Collecting CPU Data

The ab command can be used to perform a stress test


                                                        
ab -n 1000 -c 10  -T 'application/json' -p data.json http://localhost:3000/data/


                                                    Copy the code

While running the AB test, in the Easy-Monitor screen, select the CPU, and then start. Easy Monitor silently collects CPU data, and after 5 minutes, you can see the statistics:






useFundebug, find and fix application bugs in time, try it for free!!

Fixing performance issues

According to the statistics of Easy-Monitor, function A is the performance bottleneck, consuming the most CPU time.

All that remains, then, is simple enough to optimize the performance of function A. After analysis, function A does A lot of double-counting, and adding two lines of code can greatly optimize its performance. I don’t want to go over the details because they are not the focus of this article.

Comparison of performance before and after optimization

According to the test results of ab command, the average processing per second before optimization is 5.36, after optimization this number becomes 48.35, which is 9 times as before. After deploying this module, the server’s CPU usage dropped significantly and the interface’s response time returned to normal.

conclusion

With Easy-Monitor, performance bottlenecks can be pinpointed to certain functions and then optimized to help fix performance problems quickly.

reference

  • Easy-monitor 2.0: Enable performance monitoring of your Node.js kernel