Home > Web Front-end > JS Tutorial > Quickly understand the performance indicators in Node.js

Quickly understand the performance indicators in Node.js

青灯夜游
Release: 2021-09-16 10:10:18
forward
3344 people have browsed it

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')
Copy after login

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()
Copy after login

Code example

Quickly understand the performance indicators in Node.js

Execution result:

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

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')
Copy after login

Get

Method 1: Commandkill -USR2 <pid>

1Quickly understand the performance indicators in Node.js

方式二:调用writeSnapshot

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

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!

Related labels:
source:掘金--比心FE
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template