Guide Development Guide

Find the performance killer

Performance optimization of online services is an almost superstitious task. A group of programmers faced a lot of incomprehensible code and guessed each other whether the other party was using up all the CPU. Spend a lot of time recording logs, looking through massive logs day and night, trying to find the performance killer.

However, using fibjs, this will become easy. Because fibjs has powerful CPU and memory profiler support, and it is very convenient to use.

logging

Starting the cpu profiler in fibjs is extremely simple, just add options when starting --prof. The default log interval is 1000ms. As optimization deepens, you will need to analyze logs with higher precision. In this case, you can use to --prof-intervalset the interval. The following example records the fibjs work log every 10ms.

1
fibjs --prof --prof-interval=10 main.js

Processing logs

After the operation ends normally, or you use ctrl_c to end the process, a log file will be generated in the current directory. The file name is fibjs-xxxx.log. At this time, you can use --prof-processto process the generated log:

1
fibjs --prof-process fibjs-xxxx.log prof.svg

After the operation is completed, use the browser to open prof.svg to view the flame graph of this log: prof you can click to view the full-size image. In the full-size image, you can operate the mouse to view more detailed information: prof. svg .

Flame diagram interpretation

In the generated flame graph, each color block represents a recording point. The longer the color block, the more times it is recorded; each line represents a layer of call stack, and the more layers, the more layers are called; calls The stack is placed upside down. The lower the color block, the more original the function.

There are two types of color blocks, one is red and the other is blue. In the fibjs profiler, red represents JavaScript operations, and blue represents io operations or Native operations. Depending on the problem you need to solve, the areas you need to focus on will vary. For example, if you need to solve the problem of high CPU usage, you need to pay attention to the red color blocks. If your application has low CPU usage but slow response, you need to pay attention to the blue color blocks. The larger the color block near the top, the more attention and optimization it needs.

👉【Synchronous and Asynchronous