Visual debugging and performance analysis of Node.js applications

Visual debugging and performance analysis of Node.js applications

In versions prior to Node.js v6.3, the debugger used the old V8 Debugger Protocol, which can be done through node --debug app.js. In order to perform visual breakpoint debugging in a graphical interface, you need to use editors and plug-ins such as VSCode and WebStorm, or install additional tools such as node-inspector.

For most front-end developers, the DevTools that comes with the Chrome browser are no strangers. The new version of Node.js begins to support the new Chrome DevTools Protocol. You can start the new version of the debugging protocol through node --inspect app.js, and with the help of Chrome DevTools, you can achieve the same experience as debugging web pages in the past.

Some editors also support the new Node.js debugging protocol, or support it by installing the Chrome plugin NIM. But if you have Node.js 6.3+ and Chrome 55+ installed on your computer, you no longer need any additional tools to easily debug your Node.js program.

Breakpoint debugging

First, let's create a new program file app.js to experience the new debugging method:

  1. const http = require( 'http' );
  2. const server = http.createServer( function (req, res) {
  3. res.writeHead(200, { 'content-type' : 'text/html' });
  4. res. end ( '<h1>It works!</h1>' );
  5. });
  6. server.listen(3000, function () {
  7. console.log( 'Listening on http://localhost:3000' );
  8. });

Then execute the following command to start the program and enter debugging mode:

  1. node --inspect app.js  

You can see the following information printed out on the console:

  1. Debugger listening on ws://127.0.0.1:9229/1bde07a4-2afa-44b1-a3bc-45aa9977ff67
  2. For help see https://nodejs.org/en/docs/inspector
  3. Listening on http://localhost:3000

At this point, open the Chrome browser (Chrome 55 or higher is required), enter chrome://inspect in the address bar and press Enter, the following interface will open:

Click the Open dedicated DevTools for Node link on the interface to open the debugging window:

In this window, click the Sources tab and find the source code file app.js under file:// in the left sidebar to see its source code. If you want to perform breakpoint debugging, just click on the line number of the source code:

In the right sidebar of the window, you can see information such as Watch, Call Stack, Scope, etc. You can also modify the code directly in the source code window and press ⌘ + S (Ctrl + S on Windows) to save and it will take effect immediately.

Performance Analysis

The Profiler tab in the debugging interface can be used to analyze which Node.js programs have a high CPU usage:

We can use the wrk command to perform a simple HTTP interface test. First, click the Start button in the interface to start recording CPU usage information, and then execute the following command in the command line window (start 5 threads, 100 concurrent connections, and last for 1 minute):

  1. wrk -c 100 -t 5 -d 1m http://localhost:3000/

Wait for 1 minute. After the command is executed, click the Stop button in the DevTools interface and you will see the following results:

In addition, you can also use the Memory tag to perform memory usage analysis.

Debugging TypeScript Programs

If your program is written in TypeScript, you can use ts-node to start debugging mode:

  1. ts-node --inspect app.ts  

or:

  1. node --inspect --require ts-node/register app.ts  

Debugging a running Node.js process

If the program is started without the --inspect option, you can send a SIGUSR1 signal to the process to put it into debug mode (92801 is the PID of the Node.js process being debugged, replace it according to the actual situation):

  1. kill -s SIGUSR1 92801

You can see that the process console prints the following prompts:

  1. Debugger listening on ws://127.0.0.1:9229/cc35d4da-c8ae-42e3-943a-8ac81d5c067a
  2. For help see https://nodejs.org/en/docs/inspector

At this point, you can use Chrome DevTools to debug according to the above method.

<<:  After Android and iOS, the third largest mobile system reappears

>>:  iOS jailbreaking popularity has dropped significantly, and two major Cydia repositories have been shut down

Recommend

How to write high conversion rate copy? Here are 4 tips!

Whenever we are pleased with the copywriting work...

Why did he almost lose his life just because he was cutting his nose hair?

Many people like to trim their nose hairs, becaus...

Aite Tribe Story Collection (12): Habits lead to skill improvement

[51CTO.com original article] As an ordinary perso...

How to deploy a huge amount of Qianchuan when cold starting a new account?

This article will explain what actions need to be...

How to build your user membership system?

Do you need a membership system? The membership s...

How much does it cost to recruit investors for Dingxi's designated driver app?

Dingxi designated driver mini program investment ...

Programmers, how much programming technology can you really master?

I always see some companies describe their job re...

Star Rating

Source code introduction Source code screenshot v...

Case Study: 12 Gamification Strategies for User Growth

This article uses a case study to introduce how t...