Translate from Node.js Performance Monitoring – Part 1: The Metrics to Monitor
When delving into the performance of Node.js applications and how to improve performance, it is important and helpful to have a few core metrics. Let’s learn how to analyze the performance of Node.js from the following key metrics.
The CPU usage 】
Node applications generally don’t consume a lot of CPU, and a high CPU means your app is doing a lot of synchronization. They will block event loops, which will cause your asynchronous task callbacks to be blocked as well.
If you want to run a CPU-intensive program in a web-server, it is better to keep the task in a separate process. Otherwise, it will slow your service down and affect your users. Finding problems that cause CPU spikes is a good first step to understanding your Node application.
Heap memory usage, memory leaks, garbage collection
Node.js has a unique built-in limitation – a maximum heap size of 1.5GB for a process, regardless of how much memory is available on the machine on which it is running. Understanding this is critical to architecting and testing your application.
Memory leaks are a common problem in Node.js and are caused when an object is referenced for too long – in other words, a variable stores its reference even though it is no longer needed. Normally, the garbage collector frees unused memory so that it can be used by the application. However, the garbage collector cannot free the memory used by variables that are suspended after the expiration date. If your application’s memory usage is growing steadily rather than decreasing periodically through garbage collection, you may experience a memory leak
The Lag in the EventLoop is the Lag in the EventLoop.
One of the core advantages of Node.js is its speed. It is built to handle multiple events quickly and asynchronously. This advantage comes from the event loop, which allows the application to respond quickly to these events. When tuning your application for speed, it is important to understand when and why the event loop slows down. As each loop of the event loop gets slower, each event takes longer to process and manipulate. Functionally, this reduces Node.js to the point of being unresponsive.
Some cases that slow down the event loop:
- Long-running synchronous tasks
Spending too much time on a single tick of the event loop can also be a source of performance problems. You can’t eliminate the CPU time spent executing a task, but we need to be careful how much time is spent at any given time. If the work time is more than our acceptable response time, then it might make sense to perform the work in a different process.
- The tasks of each loop are increasing
Node.js keeps track of all the functions and callbacks that need to be processed at each stage of the event loop. As your server sees an increase in load, the number of tasks per cycle begins to increase. When this count gets too high, your users will start to see an increase in response time. The good news is that expanding the number of processes running your application can usually alleviate this situation and restore your site performance to normal levels