alinode is basically free and open source, not only can run in
ECS, in theory, it can be applied anywhere (as long as the face is passable). This article will repeat the use of alinode.
This article firstly talks about some of the contents included in the performance problem analysis of the
Node.js platform. The second part describes how alinode is used.
Performance problem analysis
To locate the performance problems of
Node.js, it is generally necessary to analyze the performance of the Node process.
Process analysis generally includes analysis of memory, CPU, EventLoop, ActiveHandlers, etc. For developers, there are 3 main tasks:
First, pressure testing applications. Usually what we want to analyze is a situation of the application in actual production operation, and this situation can be simulated by self-testing.
Second, get the data. That is to get our CPU status, memory status and other information. This is generally obtained through some APIs provided by the runtime or the underlying runtime. For example, Node's language execution engine V8 provides cpu profile capabilities.
Third, analyze the data. After getting the data, we need to analyze the data to allow us to quickly locate the problem. The data obtained is generally less readable, so this step is usually to visualize the data. For example, the cpu profile can be converted into a flame graph by the tool. Or provide analysis through other third-party tools such as v8-analysis tool library.
Pressure testing applications
There are several commonly used tools for this, such as autocannon written by Apache ab, wrk, and Node.js
The usage is not specifically described here. Through the pressure test tool, we can quantify the performance indicators of our site and provide us with a basis for troubleshooting performance issues.
In general, our performance indicators need to achieve a certain throughput req / sec, and the time required for a single request should be within the expected range. Otherwise, the time-consuming is too high to affect the user experience, and the throughput is too low will cause some users to fail to access.
data collection
CPU
CPU generally needs to observe some time to get his function execution stack. For example, when alinode dumps, it also needs to wait for 3 minutes.
Method 1: Directly use the API exposed by the v8 bottom layer to Node. We only need to pass in when starting the node application
node --prof app.js
Node.js has a profiler built in since version 4.4.0. Running the application with the --prof command option will generate a performance log file in the current directory
The above command will generate a file in the format of `isolate-xxxxxxx-v8.log` in the root directory of the project. This log file cannot be directly analyzed. Generally, visualization tools require a json file. Therefore, the log needs to be preprocessed, and we can preprocess the log like this:
node --prof-process --preprocess isolate-xxxxxxxxxx-v8.log> v8.json
So you can turn it into a json string file.
Method 2: Use the npm package v8-profile
Examples of use are as follows:
profiler.startProfiling ('', true); setTimeout (function () {var profile = profiler.stopProfiling (''); profile.export () .pipe (fs.createWriteStream (`cpuprofile-$ {Date.now ()} .cpuprofile`)) .on ('finish', () => profile.delete ())}, 1000);
RAM
Method 1: heapdump This module can be used to collect memory data. This package requires intrusive code. After it is referenced in the code, it can be dumped through its API. Or send kill -USR2 <pid> signal to the process externally.
Method 2: v8-profile. This is also a module that needs to invade node code, through which you can get heapdump and cpu profile
data analysis
For analysis, it is nothing more than using some easy-to-view data display tools or positioning tools to help us better display data. For example, the CPU profile can be displayed according to the function's popularity.
Method 1: Use the interface analysis tool provided by v8 itself.
git clone https://github.com/v8/v8.git
Open the v8 / tools / profview / index.html file, which is a static interface. Select the v8.json file just generated in the center of the interface. After the file is parsed successfully, the time information of the function call is displayed on the interface.
Method 2: Use chromedevTools
Specific reference: Node performance troubleshooting guide
I wo n’t go into details here. chromedevtool provides multiple views of heavyvvy and chart, which is still very good.
Method 3: Use v8-analysis tool
The v8-analysis module can parse the cpu & heap-memory logs output by tools such as v8-profiler and heapdump, and can provide:
The function of the v8 engine inverse optimization or optimization failure is shown in red and the reason for optimization failure is displayed
Function execution time exceeds expected red mark
Display of suspicious memory leak points in the current project
Method 4:
For the cpu profile, you can also make your own flame map (this is a bit of a test of hands-on ability). Use the FlameGraph tool.
alinode use
Through the brief introduction above, you can find that there are two pain points in the performance analysis. One is to collect data, you need to invade the code, and manually execute the corresponding instructions. The other is: analysis tools are varied, even with chrome devtool, you have to go through the painful process of downloading and importing the dumped data.
alinode Ali Node.js performance platform integrates the solution process of some of the above problems, provides an easier-to-use experience, and solves most of the pain points. And it's free and open source (basically). alinode monitors node applications through error logs, operating system indicators (cpu / memory / disk / IO), slow logs, exceptions, and other dimensions, and provides tools such as coredump, cpuProfile, and heapdump, as well as online web data visualization of Profile The ability to analyze.
Overall, several tools of alinode provide the performance analysis-related capabilities mentioned at the beginning of this article, namely data extraction and data analysis capabilities (of course, there are additional monitoring notifications, good-looking UI, real-time data display).
Overall architecture
The design architecture of alinode is as follows:

In fact, just look at the technical details. alinode includes the components of these 3 parts, we say from bottom to top:
Node runtime after alinode transformation. This node is basically a native node, but Ali has added a dump data switch in it, so that performance data can be collected remotely. The correspondence between the alinode version and the native version can be poke here. (Big guys do n’t need to care about any backdoor when they use it, because they only add some performance analysis capabilities, and open source is on the way)
agenthub. This is an agent installed on the application server. It is responsible for communicating with the alinode service in the cloud. The cloud management system issues commands and the machine reports data through this agent. Therefore, it is a bridge between the node process and the cloud management platform.
alinode cloud service. In fact, it is a management platform. The cloud will have corresponding services to establish websocket communication with the agenthub on the machine, which is used to issue commands or collect information. You can centrally handle all tasks of performance analysis through a web interface on the cloud management platform.
Steps for usage
I conducted a site performance analysis on the local macbook. Let's take a look at the basic steps used by alinode. It is also very helpful for us to learn the design and ideas of others. In addition to watching the steps here, you can also refer to the official help documentation as an aid.
From the above analysis, we can see that the thing we want to do on this machine is actually to install Ali's modified node runtime (alinode) and agenthub. Here are the specific steps:
Install tnvm. We know that nvm is a node version manager. Taobao has a tnvm here to manage anlinode version. So we install tnvm first, and then install alinode through tnvm.
wget -O- https://raw.githubusercontent.com/aliyun-node/tnvm/master/install.sh | bash
After executing the installation script, we execute
source ~ / .bash_profile, # yours may be ~ / .bash_rc
At this time tnvm installation is successful.
Install alinode
Run the tvnm command to see which versions of alinode remote
tnvm ls-remote alinode
Choose an alinode version to install
tnvm install alinode-vx.y.z # 4.8.0-> 10.16.0
For example, I installed v4.8.0, which corresponds to node v10.16.0
tnvm install alinode-v4.8.0
Next, don't forget to use the version you installed.
tnvm use alinode-v4.8.0 # Don't forget this sentence.
At this point, execute the which node command and you will find that the alinode managed by tnvm has been used:
/Users/cuiyongjian/.tnvm/versions/alinode/v4.8.0/bin/node
agenthub installation
agenthub is just an npm command line tool, and the installation can be completed easily with the following command:
npm install @ alicloud / agenthub -g
Agenthub needs to know the service ID on the cloud to receive and send information. So he needs a configuration to start, we create a new config.json anywhere
1 {
2 "appid": "80535",
3 "secret": "3c4d ************ 5", // When you create a new node monitoring instance on aliyun, you will be given an appid and secret
4 "logdir": "/ tmp"
5}
Then start agenthub:
DEBUG = * agenthub start config.json
Note: / tmp here refers to the directory from which agenthub gets the log files dumped. Therefore, this directory must be the same as the log output directory specified when you start the node application next, otherwise your agenthub will not be able to upload dump information.
Run the node application process
Let's start our application:
NODE_LOG_DIR = / tmp ENABLE_NODE_LOG = YES node app.js
NODE_LOG_DIR = / tmp ENABLE_NODE_LOG = YES pm2 start app.js
For example, my actual project:
sudo NODE_LOG_DIR = / tmp ENABLE_NODE_LOG = YES PORT = 80 MODE = ssr NODE_ENV = production CGI_ENV = online node ./backend/app.js
These two LOG environment variables must be specified. One is the log output directory mentioned above, and the other is whether to enable the log function. If it is not configured like this, you will definitely not be able to perform remote dumping.
Some minor problems
After installing the alinode runtime, don't forget tnvm use; in addition, sometimes when you open a new terminal, your PATH environment variable will be restored. At this time, you can execute source ~ / .bash_profile again to give priority to the node path of tnvm.
When you view data on the management platform or execute the dump command, you may be prompted an error. Here are a few troubleshooting ideas:
Agenthub needs to establish a connection with the Alibaba Cloud management platform, and our company network cannot be connected without an agent. Here you can think of ways to change the agent of agenthub.
You can tail -f ~ / .agenthub.log to see the local agent log. For example, in the log, you will see that you have a problem with the network connection or the log directory is mismatched; don't forget the two LOG environment variables when starting the application.
If you are an egg project, you do not need to start agenthub yourself. If egg is integrated, the agent is already built into the plugin, so just use the plugin directly. I won't go into details here, you can see the documentation
Cloud management interface
Open node.console.aliyun.com and enter the home page of the management console.