In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the example analysis of performance indicators in Node.js, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
For us front-end engineers, mastering Node.js application development is the only way for us to become senior / experts. In addition, Node.js is a server-side language, so we should not only be able to complete development tasks, but also pay more attention to server performance.
Application scenario
Before introducing NodeJS performance metrics, let's take a look at its application scenarios. For different application scenarios, the performance metrics we should pay attention to are different.
The BFF middle layer, the proxy of the interface, acts as the gateway layer
The development build tool gulp,webpack is based on Node.js
Combination of desktop application Electron and Node.js
SSR server rendering
If Node.js is used for front-end SSR, then CPU and network will become the main performance bottlenecks
If NodeJS is used for work related to data persistence, then Icano and disk will have a high occupancy rate.
In most scenarios, CPU, memory and network can be said to be the main performance bottlenecks of Node.
Advantages and disadvantages
Node.js is fault tolerant and its performance is not very good
Node.js is not professional in operating database.
Node.js processing asynchronous io strong
Io intensive, not suitable for cpu intensive
Event Loop (libuv)
Here is a picture from the official website to show a simplified overview of the sequence of event loop operations.
Stage description
Timer: this phase executes the scheduling callback function that has been setTimeout () and setInterval ().
Pending callback: the execution of the Imax O callback deferred to the next iteration of the loop.
Idle, prepare: for internal use only.
Polling: retrieves the new Iamp O event; executes callbacks related to IWeiO (in almost all cases, except for the callback functions that are turned off, those scheduled by timer and setImmediate ()), the node will block here at the appropriate time.
Detection: the setImmediate () callback function is executed here.
Closed callback functions: some closed callback functions, such as: socket.on ('close',...)
V8 GC mechanism
We know that Node.js ®is a JavaScript runtime environment based on the Chrome V8 engine, while it is single-threaded.
Among them, V8 memory is divided into Cenozoic and old generations:
New generation: using from space-> to space memory recovery scavenge algorithm
Old age: memory collection in the form of reference tags and defragmentation
If the GC time is too long, it will cause js thread blocking and affect service performance. Improper use of memory will cause memory overflow. After understanding the basics of Node.js above, let's look at performance metrics.
Performance index
We describe the performance metrics at the system level and the Node.js process level.
System level
For the server (physical machine, virtual machine, Docker, etc.) level, the following monitoring metrics are provided:
Memory usage
CPU utilization rate
System load, number of processes in use / waiting for progress
System QPS
Hard performance index
Disk utilization
GC statistics
……
Process level
For each Node.js process, the following monitoring metrics are provided:
In-heap (total and used) and out-of-heap memory statistics
Memory occupancy statistics for each memory space in the heap
Garbage collection (GC) as a percentage of the whole process running time
QPS
CPU statistics by 1s, 15s, 30s and 60s
Libuv handle, timer statistics
……
How to get
How do we get the performance metrics mentioned above?
System level
Indicators at the system level can be obtained in two ways
1. Os module
Const os = requie ('os')
Code example
Execution result
2. Top and iostat commands to view memory and hard disk usage
Top [parameters]
Iostat [parameters]
Memory usage
/ / this method returns the object process.memoryUsage () of the memory usage of the Node.js process.
Code example
Execution result:
{rss: 4935680, heapTotal: 1826816, heapUsed: 650472, external: 49879} copy the code
The noun explains:
Rss is the size of the resident set and how much physical memory is allocated to this process (as part of the total allocated memory)
Generally speaking, when this index rises, memory leaks may occur.
HeapTotal and heapUsed represent the memory usage of V8.
External represents the memory usage of C++ objects managed by V8 and bound to Javascript.
CPU profile
Generally speaking, if memory leaks are involved, you can take a snapshot of the heap, and if the CPU is abnormally high, you can grab CPU Profile.
It can be obtained in the following two ways:
GC trace
V8 provides many parameter options for starting node.js programs, and the information of the GC log can be obtained through the following example code
Node-- the implementation result of trace_gc
You can see that V8 uses different GC processes for new and old memory
Memory snapshot
If you use node-inspector, there will be front-end variable interference in the snapshot. It is recommended to use heapdump to save memory snapshots and devtool to view memory snapshots. When you use heapdump to save memory snapshots, only objects in the Node.js environment will not be disturbed. The use of heapdump will be introduced later.
Pressure testing
There is a stress test before the project is launched, which is used to find out if there is a memory leak.
Stress testing tools: ab Test (ApacheBench), autocannon
The following shows the results of the stress test using the ab function
The above running results can be seen.
One of our QPS:4301.28/sec
Average time per request: 23ms
Transfer rate: 617.47kb/s
Memory leak
Node.js is relatively professional back-end language Java, PHP and so on, some of the basic construction is relatively imperfect. Coupled with the characteristics of single thread, in large-scale applications, it is easy to cause the performance bottleneck of the server or Node.js process.
There are usually three conditions that cause node memory leaks:
Memory limitation of node v8 itself: 64-bit system is about 1.4GB 0.7GB 32-bit system.
Improper use of programs: global variable references, improper use of closures, event monitoring not destroyed
Large file applications: buffer operation should be used, buffer does not occupy v8 memory
So how to troubleshoot memory leaks? You can use the following tools
Tool use
one。 Heapdump: generate memory snapshot + chrome panel analysis
It should be noted that printing memory snapshots is a CPU-intensive operation that may affect online business.
Introduce
Const heapdump = require ('heapdump')
Get
Method 1: command kill-USR2
Method 2: call writeSnapshot
Heapdump.writeSnapshot ('. /'+ Date.now () + '.heapsnapshot')
Chrome panel analysis
two。 Alinode
Aliyun also provides a performance platform for Nodejs applications, alinode, which can collect performance indicator data for us conveniently and comprehensively, and at the same time, it is more intuitive in the way of visual charts. For access to alinode, you can refer to 5-minute Quick start.
The following is a chart showing some of the collected data
Description of some indicators
Memory
Memory_sys: percentage of system memory usage.
Memory_node: the sum of memory used by all Node.js processes as a percentage of system memory.
CPU
Cpu_sys: percentage of system CPU usage.
Cpu_node: the sum of the percentage of CPU used by all Node.js processes.
Load
Average Load within load1:1 minutes.
Average Load within load5:5 minutes.
Average Load within load15:15 minutes.
Here are some references to Load (Load has been normalized, if it is an N-core CPU, then the corresponding Load * N):
0.7
< Load < 1:不错的状态,有新任务也可以及时处理; Load = 1:即将有任务需要额外的等待时间才能被处理,需要引起关注; Load >5: the task needs to wait for a long time and requires intervention.
Usually first look at load15, if very high, then look at load1 and load5 to see if there is a downward trend. Load1 is greater than 1 in a short period of time. Don't worry. If Load is high for a long time, it needs attention.
QPS
The sum of the number of HTTP requests processed per second by all Node.js processes in this instance.
GC
Gc_avg: average garbage collection time for all Node.js processes.
Gc_max: the percentage of garbage collection time of the Node.js process with the most garbage collection time per minute.
three。 Open source Easy-Monitor
Enterprise Node.js application performance monitoring and online fault location solution.
Easy-Monitor is a lightweight Node performance monitoring tool. Quick entrance
We can also build and deploy our own internal performance platform on the basis of it.
The above is all the content of the article "sample Analysis of performance indicators in Node.js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.