Node.js tuning

Introduction

node-profiler is another product of the alinode team that can help you analyze the performance of JavaScript code offline, presenting the performance details of Google V8 in front of you, and optimizing it. For online tuning, please use alinode.

Download and Install

It is recommended to install the tool tnvm, which supports the installation and switching of node, alinode, and profiler.

wget -O- https://raw.githubusercontent.com/aliyun-node/tnvm/master/install.sh | bash

After the installation is complete, you need to add tnvm as a command-line program. Depending on the platform, it may be ~/.bashrc, ~/.profile, or ~/.zshrc.

 tnvm install profiler-v0.12.6
 tnvm use profiler-v0.12.6

Usage Example

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200);
  res.end('hello world!');
}).listen(1334);
$ node-profiler server.js
start agent            
webkit-devtools-agent: A proxy got connected.
webkit-devtools-agent: Waiting for commands...
webkit-devtools-agent: Websockets service started on 0.0.0.0:9999  <==启动成功

If the following appears:

Error: listen EADDRINUSE           <== 可能是由于端口被占用

By default, Collect JavaSript CPU Profile is selected, click Start.

You can use the pressure test script to implement pressure testing on the service to ensure more results:

$ wrk http://localhost:1334/ # Here we use wrk, other tools such as ab can also be used

More information about functions during runtime can be seen.

UI Meaning

Red indicates that the function has not been optimized, and light green indicates that the function has been optimized by V8.

Principle Introduction

  • Based on the built-in sampling collector of V8;

  • Fixed sampling frequency, default 1ms, configurable;

  • The main thread is paused, the function call stack is sampled, and the time is counted;

  • It is necessary to ensure that the sampling is long enough (preheating).

Explanation of two concepts

  • exclusive time: exclusive time

  • inclusive time: inclusive time

function foo() { 
   bar();
}
function bar() {
          <==Sampling point
}
foo();

In this example, the sampling point is in bar(), so the time consumed by bar is called exclusive time, and foo calls bar, so the time consumed by foo includes the time of bar, called inclusive time.

Precautions

  • This tool currently only supports X64 platforms (Linux, Mac).

  • Do not deploy it online. If you need online tuning, please use alinode.

Last updated