Profiling Node.js

(For returning readers, this is basically a “tl;dr” version of my previous post on Node.js performance. The post below also appears on the Node.js blog.)

It’s incredibly easy to visualize where your Node program spends its time using DTrace and node-stackvis (a Node port of Brendan Gregg’s FlameGraph tool):

  1. Run your Node.js program as usual.
  2. In another terminal, run:
    $ dtrace -n 'profile-97/execname == "node" && arg1/{
        @[jstack(100, 8000)] = count(); } tick-60s { exit(0); }' > stacks.out

    This will sample about 100 times per second for 60 seconds and emit results to stacks.out. Note that this will sample all running programs called “node”. If you want a specific process, replace execname == "node" with pid == 12345 (the process id).

  3. Use the “stackvis” tool to transform this directly into a flame graph. First, install it:
    $ npm install -g stackvis

    then use stackvis to convert the DTrace output to a flamegraph:

    $ stackvis dtrace flamegraph-svg < stacks.out > stacks.svg
  4. Open stacks.svg in your favorite browser.

You’ll be looking at something like this:

This is a visualization of all of the profiled call stacks. This example is from the “hello world” HTTP server on the Node.js home page under load. Start at the bottom, where you have “main”, which is present in most Node stacks because Node spends most on-CPU time in the main thread. Above each row, you have the functions called by the frame beneath it. As you move up, you’ll see actual JavaScript function names. The boxes in each row are not in chronological order, but their width indicates how much time was spent there. When you hover over each box, you can see exactly what percentage of time is spent in each function. This lets you see at a glance where your program spends its time.

That’s the summary. There are a few prerequisites:

There are a few other notes:

For more on the underlying pieces, see my previous post on Node.js profiling and Brendan’s post on Flame Graphs.


Posted on April 25, 2012 at 10:21 am by dap · Permalink
In: DTrace, Joyent, Node.js, SmartOS · Tagged with: , , ,

4 Responses

Subscribe to comments via RSS

  1. Written by Brendan Gregg
    on April 25, 2012 at 2:38 pm


    If people are using this, I’d recommend keeping either the folded-stack file or the original stack file (which is larger but more detailed), so that they can be reprocessed as we develop the flame graph software further (via either the node.js or Perl versions).

  2. Written by Node.js - Techniques | Pearltrees
    on April 26, 2012 at 1:33 am

    [...] (For returning readers, this is basically a “tl;dr” version of my previous post on Node.js performance . Dave Pacheco's Blog » Profiling Node.js [...]

  3. Written by Pie in the Sky (May 11, 2012) | MSDN Blogs
    on May 11, 2012 at 10:59 am

    [...] Profiling Node.js: Dave Pacheoco walks through using DTrace and node-stackvis to figure out where your program is spending all it’s time. [...]

  4. Written by Eric
    on May 22, 2012 at 2:58 pm

    Interesting analysis of Node.js performance. Would you mind if I republished on We’ve got a readership of advanced developers who are becoming more interested in high performance.

Subscribe to comments via RSS