Finding a solution

In this case, Node.js is spending most of the execution time in the https://www.npmjs.com/package/acorn dependency, which is a dependency of https://www.npmjs.com/package/jade.

So we can conclude that template rendering is the bottleneck, our application is spending most of its time in parsing .jade files (through acorn).

In the previous recipe's There's More... section we talked about Profiling for Production. Essentially, if the server is in development mode templates are rendered each time, whereas in production mode templates are cached. Our flamegraph has just made this abundantly clear.

Let's take a look at the resulting flamegraph generating from running the same benchmark on the same server running in production mode.

This time when we use 0x to spin up our server we ensure that NODE_ENV is set to production:

$ NODE_ENV=production 0x server

Now bench it with autocannon in another terminal window:

$ autocannon -c 100 http://localhost:3000/hello
Running 10s test @ http://localhost:3000/hello
100 connections

Stat Avg Stdev Max
Latency (ms) 18.17 14.07 369
Req/Sec 5362.3 773.26 5867
Bytes/Sec 1.85 MB 260.17 kB 2.03 MB

54k requests in 10s, 18.55 MB read

Note the difference in requests per second.

Now we hit Ctrl + C on our server, once the flamegraph is generated it should look something like the following:

The flamegraph for the main example

This flamegraph has a different shape and it's much simpler. It has fewer functions and the hottest areas are where data is being written to a socket, this is ideal since that's the exit point - that should be hot.