Thomas
Thomas

Reputation: 181745

How to trigger profiling in NodeJS at runtime?

We have a very stateful NodeJS based web server (Meteor) that occasionally, randomly becomes slow in production. The problem is not reproducible in any of our tests, and we don't know what's triggering it.

To diagnose this, we are using the v8-profiler package. This lets us trigger a 10-second CPU profile and download it for offline analysis.

Despite not having received any commits in 3 years, the package used to work fairly well. It has given us compilation trouble in the past, and now it looks like it stopped compiling entirely, breaking our build. The build happens inside a Docker container with all versions pinned, including NodeJS and v8-profiler itself, so it's unlikely that we can fix this on our end.

I'm thinking there must be some alternative, better maintained approach. But where is it?

(Note that restarting the server with additional flags (like --profile) is not an option, because it destroys all the evidence of the problem.)

Upvotes: 8

Views: 1712

Answers (2)

Pawel
Pawel

Reputation: 18222

I just built a tool for this. Called ntop, so it's like "top" but for Node apps https://github.com/DVLP/ntop

The below will enable communication with the CLI. This is designed to not add any overhead when the CLI tool is not used so it can be used in production. The profiler connects/disconnects immediately only when the CLI is doing the profiling.

The app:

import * as ntop from 'ntop'
ntop()

CLI shortcut to get a list of PIDs for convenience:

npx ntop

Outputs PIDs and additionally the command used to create the process for easier recognition.

Process detected at 12345 Details: node ./src/index.js --port 8216

npx ntop 12345

Outputs a list like "Bottom Up" in Chrome Dev Tools

  • (garbage collector) | 16.101ms |
  • shift | 10.038ms | node:internal/priority_queue:98:7
  • (anonymous) | 9.192ms | file:///home/app/src/controllers/Server.js:24:29
  • utils.bulkPreparePacket | 4.924ms | file:///home/app/src/Utils.js:91:26
  • preparePacket | 4.776ms | file:///home/app/src/Model.js:98:54
  • baseGetTag | 1.727ms | file:///home/app/node_modules/lodash/lodash.js:3104:23
  • (anonymous) | 1.702ms | evalmachine.:3:14
  • isPrototype | 1.441ms | file:///home/app/node_modules/lodash/lodash.js:6441:24
  • (program) | 1.411ms |
  • percolateDown | 1.124ms | node:internal/priority_queue:40:15

Upvotes: 0

tatsuya kanemoto
tatsuya kanemoto

Reputation: 2028

I found there has been v8-profiler-next which is a successor of v8-profiler.

I hope this works for you.

Upvotes: 4

Related Questions