Home>Article>Web Front-end> Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

青灯夜游
青灯夜游 forward
2022-08-11 20:14:27 2153browse

Why is performance monitoring needed? This article will take you throughNode.jsperformance monitoring. I hope it will be helpful to you!

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

Why performance monitoring is needed

NodeAs a runtime (Runtime) of Javascript on the server side, It greatly enriches the application scenarios of Javascript.

But Node.js Runtime itself is a black box. Wecannot perceivethe status of the runtime, and it isdifficult to reproduceonline problems.

ThereforePerformance monitoringis the cornerstone of the "normal operation" of Node.js applications. Not only can various runtime indicators be monitored at any time, but it can also help troubleshoot abnormal scenario problems.

Components

Performance monitoring can be divided into two parts:

  • Collection and display of performance indicators

    • Process-level data: CPU, Memory, Heap, GC, etc.
    • System-level data: disk occupancy, I/O load, TCP/UDP connection status, etc.
    • Application layer data: QPS, slow HTTP, business processing link logs, etc.
  • Capture and analysis of performance data

    • Heapsnapshot: Heap memory snapshot
    • Cpuprofile: CPU snapshot
    • Coredump: Application crash snapshot

Scheme comparison

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

From the above figure you can see the advantages and disadvantages of the three current mainstream Node.js performance monitoring solutions. The following is a brief introduction to the composition of these three solutions:

  • Prometheus

    • prom-clientis the nodejs implementation of prometheus, used to collect performance indicators
    • grafanais A visualization platform used to display various data charts and support access to prometheus
    • Only supports the collection and display of performance indicators. Other snapshot tools are needed to troubleshoot problems to form a closed loop
  • AliNode

    • alinode is an extended runtime compatible with official nodejs, providing some additional features:

      • v8 Runtime memory status monitoring
      • libuv's runtime status monitoring
      • Online fault diagnosis functions: heap snapshot, CPU Profile, GC Trace, etc.
    • agenthubis a resident process used to collect performance indicators and report

    • The whole system forms a closed loop from monitoring, display, snapshot, and analysis. The access is convenient and simple, but there are still risks in expanding the runtime

  • Easy-Monitor

    • xprofilerResponsible for real-time runtime status sampling and output performance logs (that is, performance data Fetching)
    • xtransitResponsible for the collection and transmission of performance logs
    • The biggest difference from AliNode is the use ofNode.js Addonfor implementation Sampler

Performance Indicators

CPU

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

##Through

process.cpuUsage()you can obtain the CPU consumption data of the current process. The unit of the return value is microseconds

    user: the CPU consumed by the process itself during execution Time
  • system: The CPU time consumed by the system when the process is executed

Memory

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring##Pass

process.memoryUsage()

You can get the memory allocation data of the current process. The unit of the return value is bytes

rss: resident memory, the total memory size allocated by the node process
  • heapTotal: The heap memory size applied for by v8
  • heapUsed: The heap memory size used by v8
  • external: The memory size occupied by C managed by v8
  • arrayBuffers: Memory size allocated to ArrayBuffer

#

As can be seen from the above figure,rssincludes code segment (Code Segment), stack memory (Stack), and heap memory (Heap)

  • Code Segment: stores code segments
  • Stack: stores local variables and management function calls
  • Heap: stores objects, closures, or others Everything

Heap

can be obtained from v8 throughv8.getHeapStatistics()andv8.getHeapSpaceStatistics()Analysis data of heap memory and heap space. The following figure shows the distribution of heap memory composition of v8:

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

The heap memory space is first divided into spaces, and the space is divided into Page, memory is paged according to 1MB alignment.

  • New Space: New generation space, used to store some object data with a relatively short life cycle, divided into two spaces (space type issemi space):from space,to space

    • Promotion conditions: Still surviving after two GCs in New space
  • Old Space: Old generation space, used to storeNew SpacePromoted objects

  • Code Space: Store v8 JIT compiled executable code

  • Map Space: Stores the pointer object of the hidden class pointed to by Object. The hidden class pointer is the object layout structure recorded by v8 at runtime and is used to quickly access object members

  • Large Object Space: used to store objects larger than 1MB that cannot be allocated to pages

GC

v8 Garbage collection algorithms are divided into two categories:

  • Major GC: uses theMark-Sweep-Compactalgorithm for object recycling in the old generation
  • Minor GC : Uses theScavengealgorithm for object recycling in the new generation

Scavenge

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

Premise:New spaceis divided into two object spaces:fromandto

Trigger timing: whenNew spacespace Full

Steps:

  • Infrom space, perform a breadth-first traversal

  • Discover The surviving (reachable) object

    • has survived once (experienced a Scavange) and was promoted toOld space
    • Others are copied toto space
  • When the copy ends, there are only surviving objects into space, andfrom spaceis cleared.

  • ##Exchange

    from spaceandto space, and start the next roundScavenge

Suitable for objects with frequent recycling and small memory. It is a typical space-for-time strategy. The disadvantage is that twice as much space is wasted

Mark- Sweep-Compact

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

Three steps: mark, clear, organize

Trigger time: when

Old spaceis full

Steps:

  • Marking (three-color marking method)

      White: represents recyclable objects
    • Black : Represents a non-recyclable object, and all the references generated by it have been scanned
    • Gray: Represents a non-recyclable object, and the references generated by it have not been scanned
    • Directly place the V8 root object The referenced objects are put into a
    • marking queue(explicit stack), and these objects are marked gray
    • Start from these objects and do a depth-first traversal. Each time an object is accessed, the The object comes out of the
    • marking queuepopand is marked black
    • Then all white objects under the object reference are marked gray,
    • pushGo tomarking queue, and repeat
    • until all objects on the stack are popped. There are only two types of objects in the old generation: black (non-recyclable) and white (can be recycled).
    • PS: When an object is too large to be pushed to the stack with limited space, v8 will keep the object in gray and skip it, marking the entire stack as overflowed. After the stack is cleared, it will be pushed again Traverse the mark, which will require an additional scan of the heap
  • Sweep

      Clearing the white objects
    • will cause the memory space to be insufficient Continuous
  • Compact

    • Since Sweep will cause the memory space to be discontinuous, it is not conducive for new objects to enter the GC
    • Move the black (survival) object to one end of theOld space, so that it can be cleared out The space is continuous and complete
    • Although it can solve the problem of memory fragmentation, it will increase the pause time (slow execution speed)
    • It is only used when there is insufficient space to allocate objects promoted from the new generation. mark-compact

##Stop-The-World

When v8 initially performs garbage collection, you need to stop the running of the program. The program will not be re-run until the entire heap is scanned and the memory is reclaimed. This behavior is called full stop (

Stop-The-World)

Although the new generation active objects are smaller and recycled frequently, full stop has little impact, but there are many surviving objects in the old generation. And if it is large, the pauses caused by marking, cleaning, sorting, etc. will be more serious.

Optimization strategy

    Incremental Marking: In the Marking phase, when the heap reaches a certain size, incremental GC starts, each allocation After a certain amount of memory is obtained, the program is paused, marking is done for a few milliseconds to tens of milliseconds, and then the program is resumed.
This concept is actually a bit like the Fiber architecture in the React framework. Only during the browser's free time will it traverse the Fiber Tree to perform the corresponding tasks. Otherwise, the execution will be delayed to affect the main thread as little as possible. tasks, avoid application lags, and improve application performance.

    Concurrent Sweeping (Concurrent Sweeping): Let other threads do sweeping at the same time without worrying about conflict with the main thread of the executing program
  • Parallel Sweeping (Parallel Sweeping): Let multiple Sweeping threads work at the same time, improving the throughput of sweeping and shortening the entire GC cycle

Space adjustment

Due to the default space limitations of v8 for the new and old generations The size is

  • New spaceDefault limit: 32M for 64-bit system, 16M for 32-bit system
  • Old spaceDefault limit: The 64-bit system is 1400M, and the 32-bit system is 700M
Therefore,

nodeprovides two parameters for adjusting the upper limit of the space of the new and old generations

  • --max-semi-space-size: Set the maximum value ofNew Space
  • --max-old-space-size: Set the maximum value ofOld Space

View GC log

nodeis also provided Three ways to view GC logs:

  • --trace_gc: One line of log briefly describes the time, type, heap size changes and causes of each GC
  • --trace_gc_verbose: Display the detailed status of each V8 heap space after each GC
  • --trace_gc_nvp: Detailed key-value pair information of each GC , including GC type, pause time, memory changes, etc.
Since the GC log is relatively primitive and needs secondary processing, you can use

v8-gc-log-parser## developed by the AliNode team.

#Snapshot Tool

Heapsnapshot

Performs snapshot sampling of the

heap memory

of the running program, Can be used to analyze memory consumption and changes

Generation method

There are several ways to generate the

.heapsnapshot

file:

    Use
  • heapdump

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

    Use v8’s
  • heap- profile

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

    Use the api provided by the nodejs built-in v8 module
    • v8.getHeapSnapshot()

    1Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

      ##v8.writeHeapSnapshot(fileName)

    1Why do you need performance monitoring? Let’s talk about Node.js performance monitoring##Use

    v8-profiler-next

Analysis method1Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

The generated.heapsnapshotfile can be uploaded in Memory on the Chrome devtools toolbar and selected , the display result is as follows:

The default view is theSummaryview. Here we want to focus on the two rightmost columns:Shallow SizeandRetained Size

  • Shallow Size: Indicates the size of the object itself allocated in v8 heap memory
  • Retained Size: Indicates theShallow Size# of all referenced objects of the object ##sum
When

Retained Sizeis found to be particularly large, there may be a memory leak inside the object, and you can further expand to locate the problem

and

ComparisonThe view is used to compare and analyze heap snapshots of two different periods. TheDeltacolumn can be used to filter out the objects with the largest memory changes

1Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

Cpuprofile

Snapshot sampling of the

CPUof the running program can be used to analyze the CPU time consumption and proportion

Generation method

There are several ways to generate

.cpuprofilefiles:

This is a CPU Profile sample collected for 5 minutes

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

Analysis method

Generated

.cpuprofilefile can be displayed inJavascript Profilerof the Chrome devtools toolbar (not in the default tab, you need to open it in More on the right side of the toolbar). After selecting the upload file, the results will be displayed. As shown below:

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

The default view is the

Heavyview. Here we see two columns:Self TimeandTotal Time

  • Self Time: Represents the execution time of this function itself (excluding other calls)
  • Total Time: Represents the total execution time of this function (including other calling functions)
When the deviation between

Total TimeandSelf Timeis found to be large , this function may be time-consuming and CPU-intensive calculations, you can also conduct further troubleshooting

Codedump

When the application unexpectedly crashes and terminates, the system will automatically Record the memory allocation information, Program Counter and stack pointer and other key information at the moment the process crashes to generate the core file

Generation method

Generate

.coreThree methods for files:

  • ulimit -c unlimitedOpen kernel limit
  • node --abort-on-uncaught-exceptionAdd this parameter when node starts, you can generate a core file when an uncaught exception occurs in the application
  • gcore Manually generate the core file

Analysis method

After obtaining the

.corefile, you can use mdb, gdb, lldb and other tools to analyze and diagnose the cause of the actual process crash

  • llnode `which node` -c /path/to/core/dump

Case analysis

Observation

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

It can be observed from monitoring that the heap memory continues to increase, so heap snapshots are needed for troubleshooting

Analysis

1Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

According to

heapsnapshot, we can analyze and find out that there is anewThingobject that has always maintained a relatively large memory

Troubleshooting

Why do you need performance monitoring? Let’s talk about Node.js performance monitoring

You can see from the code that although the

unusedmethod is not called,newThingThe object is referenced fromtheThing, causing it to always exist in the execution context ofreplaceThingand has not been released. This is a typical case of memory leak caused by closure

Summary

Common memory leaks include the following situations:

  • Global variables
  • Closure
  • Timer
  • Event listening
  • Cache

So in In the above situations, you must carefully consider whether the object will be automatically recycled in the memory. If it will not be automatically recycled, you need to manually recycle it, such as manually setting the object tonulland removing the timing. Device, unbinding event monitoring, etc.

Summary

So far, this article has given a detailed introduction to the entire Node.js performance monitoring system.

First of all, it introduces the problems solved by performance monitoring, its components and a comparison of the advantages and disadvantages of mainstream solutions.

Then, the two major performance indicators and snapshot tools are introduced in detail.

  • The performance indicators mainly focus on CPU, memory, heap space, and GC indicators, and are introduced at the same time. Understand v8's GC strategy and GC optimization plan,
  • The snapshot tools mainly include heap snapshot, CPU snapshot and Coredump when crashing

Finally, reproduce a simple from observation, analysis and troubleshooting memory leak cases, and summarizes common memory leak situations and solutions.

I hope this article can help everyone understand the entire Node.js performance monitoring system.

For more node-related knowledge, please visit:nodejs tutorial!

The above is the detailed content of Why do you need performance monitoring? Let’s talk about Node.js performance monitoring. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete