search
HomeWeb Front-endJS TutorialQuickly understand the performance indicators in Node.js

This article will take you through the performance indicators of Node.js, I hope it will be helpful to you!

Quickly understand the performance indicators in Node.js

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.

Application scenarios

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.

  • BFFThe middle layer, the proxy of the interface, acts as the gateway layer

  • Development and build toolgulp, webpackBased on Node.js

  • Desktop applicationElectron combined with Node.js

  • SSRServer-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/Oand Disk will have a high occupancy rate;

In most scenarios, CPU, Memory and NetworkIt can be said to be the main performance bottleneck of Node.

Advantages and Disadvantages

  • 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

Event loop (libuv)

Here is a picture quoted from the official website, showing a simplified overview of the event loop operation sequence

Quickly understand the performance indicators in Node.js

Phase description

  • Timer: This stage executes the scheduling callback functions that have been setTimeout() and setInterval().
  • Pending callback: I/O callback whose execution is delayed until the next loop iteration.
  • idle, prepare: Only used internally by the system.
  • Polling: retrieves new I/O events; executes I/O-related callbacks (in almost all cases, except for shutdown callbacks, those driven by timers and setImmediate() Except for scheduling), in other cases node will block here at the appropriate time.
  • Detection: setImmediate() The callback function is executed here.
  • Closed callback function: Some closed callback functions, such as: socket.on('close', ...)

V8 GC mechanism

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

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:

  • Memory usage

  • ##CPU

    Usage rate

  • System load, number of processes in use/waiting for progress
  • System
  • QPS

  • Hard Performance Indicators
  • Disk Usage
  • GC

    Statistics

    ##… …
Process level

For each Node.js process, the following monitoring indicators are provided:

In-heap (total and used) and off-heap memory statistics
  • Memory statistics occupied by each memory space in the heap
  • Garbage collection (GC) accounts for the proportion of the entire process running time
  • QPS
  • ##CPU statistics based on 1s, 15s, 30s, and 60s

  • libuv handle, timer statistics

  • ......

  • How to obtain

How do we obtain the performance indicators mentioned above?

System level

System-level indicators can be obtained in the following two ways

1. os module

const os = requie('os')

Code example

Quickly understand the performance indicators in Node.js

Execution results

Quickly understand the performance indicators in Node.js

2. Use the top and iostat commands to check the memory and hard disk.

top [parameter]

Quickly understand the performance indicators in Node.js

iostat[Parameter]

Quickly understand the performance indicators in Node.js

Memory usage

//该方法返回 Node.js 进程的内存使用情况的对象
process.memoryUsage()

Code example

Quickly understand the performance indicators in Node.js

Execution result:

{
  rss: 4935680,
  heapTotal: 1826816,
  heapUsed: 650472,
  external: 49879
}复制代码

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, memory leaks may occur

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 CPU Profile

It can be obtained in the following two ways:

Quickly understand the performance indicators in Node.js

GC trace

V8 provides many parameter options for node.js program startup. You can obtain GC log information through the following example code

Quickly understand the performance indicators in Node.js

node --trace_gc execution results

You can see that V8 uses different GC processes for new and old memories

Quickly understand the performance indicators in Node.js

Memory Snapshot

Quickly understand the performance indicators in Node.js

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 heapdump will be introduced later

Stress test

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

1Quickly understand the performance indicators in Node.js

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

Memory leak

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

Tool usage

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

const heapdump = require('heapdump')

Get

Method 1: Commandkill -USR2 <pid></pid>

1Quickly understand the performance indicators in Node.js

方式二:调用writeSnapshot

heapdump.writeSnapshot(&#39;./&#39; + Date.now() + &#39;.heapsnapshot&#39;);

chrome面板分析

1Quickly understand the performance indicators in Node.js

二. alinode

阿里云也提供了Nodejs应用的性能平台alinode,可以很方便、全面的为我们收集性能指标数据,同时以可视化图表的方式,更加的直观。接入alinode可参考5分钟快速入门

以下是部分采集数据图表展示

1Quickly understand the performance indicators in Node.js

一些指标描述

Memory

  • memory_sys:系统内存使用百分比。
  • memory_node: 所有 Node.js 进程内存使用之和占系统内存的百分比。

CPU

  • cpu_sys:系统 CPU 使用百分比。
  • cpu_node:所有 Node.js 进程 CPU 使用百分比之和。

Load

  • 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 较高,需要引起关注。

QPS

该实例所有 Node.js 进程每秒钟处理的 HTTP 请求数之和。

GC

  • gc_avg:所有 Node.js 进程垃圾回收时间占比平均值。
  • gc_max:每分钟内垃圾回收时间最多的 Node.js 进程的垃圾回收时间占比。

三. 开源Easy-Monitor

企业级 Node.js 应用性能监控与线上故障定位解决方案。

Easy-Monitor是一款轻量级的Node性能监控工具。快速入口

我们也可以给予它的基础之上去搭建部署一套自己内部的性能平台。

总结

以上是我关于Node.js性能指标以及获取的简单介绍,更多的是对包含性能点的一个整体上的介绍,那针对每个性能指标我们都可以去再做更深入的研究。希望这篇文章能够帮助你,同时也感谢你的阅读,期待再见~

参考

Node.js 性能平台
如何分析 Node.js 中的内存泄漏
本文示例代码

原文地址:https://juejin.cn/post/7008006326857138184

作者:比心FE

更多编程相关知识,请访问:编程入门!!

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!

Statement
This article is reproduced at:掘金--比心FE. If there is any infringement, please contact admin@php.cn delete
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools