This article will take you through the performance indicators of Node.js, I hope it will be helpful to you!
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. We must not only be able to complete development tasks, but also pay attention to server performance. [Recommended study: "nodejs Tutorial"]
This article will give some preliminary introduction to the basics and performance indicators of Node.js.
Before introducing NodeJS
performance indicators, let’s first take a look at its application scenarios. For different application scenarios, the performance indicators to focus on are Something different.
BFF
The middle layer, the proxy of the interface, acts as the gateway layer
Development and build toolgulp
, webpack
Based on Node.js
Desktop applicationElectron
combined with Node.js
SSR
Server-side rendering
If Node.js is used for the front-end SSR
, then CPU
and Network
will become the main performance bottleneck
;
If you use NodeJS to perform data persistence related work, thenI/O
and Disk
will have a high occupancy rate;
In most scenarios, CPU
, Memory
and Network
It can be said to be the main performance bottleneck of Node.
node.js is fault-tolerant and its performance is not very good
node.js operation database is not professional
node.js is strong in handling asynchronous io
io-intensive, not suitable for cpu-intensive
Here is a picture quoted from the official website, showing a simplified overview of the event loop operation sequence
Phase description
setTimeout()
and setInterval()
. setImmediate()
Except for scheduling), in other cases node will block here at the appropriate time. setImmediate()
The callback function is executed here. socket.on('close', ...)
We know that Node.js® is a JavaScript runtime environment based on Chrome V8 engine, and it is single-threaded.
V8 memory
is divided into new generation and old generation:
New generation: Using from space->to space memory recycling scavenge algorithm
Old generation: Memory recycling in the form of reference marking and defragmentation
If the GC time is too long, it will cause the js thread to be blocked and affect service performance. Improper use of memory will cause memory overflow. After understanding the above basic knowledge of Node.js, let's look at the performance indicators
We describe a performance indicator from the system level and the Node.js process level
System level
##For servers (physical machines,
virtual machines,
Docker, etc. ) level, providing the following monitoring indicators:
Usage rate
Statistics
For each Node.js process, the following monitoring indicators are provided:
In-heap (total and used) and off-heap memory statistics##CPU statistics based on 1s, 15s, 30s, and 60s
libuv handle, timer statistics
......
System level
System-level indicators can be obtained in the following two ways 1. os module Code example Execution results 2. Use the top and iostat commands to check the memory and hard disk. top [parameter] iostat[Parameter] Memory usage Code example Execution result: Explanation of noun: rss is resident Set size is how much physical memory is allocated to this process (a part of the total allocated memory) Generally if this indicator increases, heapTotal and heapUsed represents V8's memory usage. external represents the memory usage of C objects bound to Javascript managed by V8. CPU profile Generally speaking, if it involves a memory leak, you can grab a heap snapshot, then If the CPU is abnormally high, you can grab It can be obtained in the following two ways: GC trace V8 provides many parameter options for node.js program startup. You can obtain GC log information through the following example code node --trace_gc execution results You can see that V8 uses different GC processes for new and old memories Memory Snapshot If node-inspector is used, 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 a memory snapshot, only the objects in the Node.js environment will be uninterrupted. The use of Before the project goes online, a stress test is required to find out whether there is memory through the stress test Leakage Stress testing tools: ab test (ApacheBench), autocannon The following shows the results of stress testing using the ab function You can see the above running results One of our QPS: 4301.28/sec The average duration of each request: 23ms Transmission rate: 617.47kb/s Node.js is relatively professional back-end language Java, PHP, etc. Some basic construction is relatively Not perfect enough. Coupled with the characteristics of single thread, in large-scale applications, it is easy to cause performance bottlenecks in the server or Node.js process. There are usually three situations that cause node memory leaks: The memory size limit of node v8 itself: 64-bit system is about 1.4GB, 32-bit system is about 0.7GB. Improper use of programs: global variable references, improper use of closures, event listeners not destroyed Large file applications: buffer operations should be used, The buffer does not occupy v8 memory #So how to check for memory leaks? You can use the following tools 1. heapdump: Generate memory snapshot chrome panel analysis It should be noted that printing memory snapshots is a very CPU-intensive operation and may have an impact on online business. Introduction Get Method 1: Command 方式二:调用writeSnapshot chrome面板分析 二. alinode 阿里云也提供了Nodejs应用的性能平台alinode,可以很方便、全面的为我们收集性能指标数据,同时以可视化图表的方式,更加的直观。接入alinode可参考5分钟快速入门 以下是部分采集数据图表展示 一些指标描述 Memory CPU Load 下面是一些 QPS 该实例所有 Node.js 进程每秒钟处理的 HTTP 请求数之和。 GC 三. 开源Easy-Monitor 企业级 Node.js 应用性能监控与线上故障定位解决方案。 Easy-Monitor是一款轻量级的Node性能监控工具。快速入口 我们也可以给予它的基础之上去搭建部署一套自己内部的性能平台。 以上是我关于Node.js性能指标以及获取的简单介绍,更多的是对包含性能点的一个整体上的介绍,那针对每个性能指标我们都可以去再做更深入的研究。希望这篇文章能够帮助你,同时也感谢你的阅读,期待再见~ Node.js 性能平台 原文地址:https://juejin.cn/post/7008006326857138184 作者:比心FE 更多编程相关知识,请访问:编程入门!!const os = requie('os')
//该方法返回 Node.js 进程的内存使用情况的对象
process.memoryUsage()
{
rss: 4935680,
heapTotal: 1826816,
heapUsed: 650472,
external: 49879
}复制代码
memory leaks may occur
CPU Profile
heapdump
will be introduced laterStress test
Memory leak
Tool usage
const heapdump = require('heapdump')
kill -USR2 <pid>
heapdump.writeSnapshot('./' + Date.now() + '.heapsnapshot');
memory_sys
:系统内存使用百分比。memory_node
: 所有 Node.js 进程内存使用之和占系统内存的百分比。
cpu_sys
:系统 CPU 使用百分比。cpu_node
:所有 Node.js 进程 CPU 使用百分比之和。
load1
:1分钟内平均 Load。load5
:5分钟内平均 Load。load15
:15分钟内平均 Load。Load
的参考信息 (Load
已经归一化处理,如果是 N 核 CPU,那么相应 Load * N
):
0.7 :不错的状态,有新任务也可以及时处理;
Load = 1
:即将有任务需要额外的等待时间才能被处理,需要引起关注;Load > 5
:任务需要等待时间很长,需要干预处理。load15
,如果很高,再看 load1
和 load5
,看是否有下降趋势,短时间内 load1
大于 1,不用担心,如果长时间 Load
较高,需要引起关注。
gc_avg
:所有 Node.js 进程垃圾回收时间占比平均值。gc_max
:每分钟内垃圾回收时间最多的 Node.js 进程的垃圾回收时间占比。总结
参考
如何分析 Node.js 中的内存泄漏
本文示例代码
The above is the detailed content of Quickly understand the performance indicators in Node.js. For more information, please follow other related articles on the PHP Chinese website!