node.js performance

Leveraging Google's V8 Profiler
DTVE






July 2013
hunter@skookum.com

profiling


  1. Measure
  2. Analyze
  3. Edit
  4. Repeat

1. Measure


`make profile`

runs: node --prof app.js

creates: v8.log

(not human-readable. gibberish.)

2. Analyze


`make profile-results`

runs: nprof v8.log

top-down: overview of the call stack; system vs application time (total vs nonlib).

bottom-up: "heavy" sections of code. indents underneath mean "called by." * means "optimized by v8"

3. Edit


Evaluate and improve the identified bottlenecks.

Repeat until performance meets requirements.

Profiler Middleware


Web Apps have "special needs."

They're idle most of the time and only ramp up when a user makes a request. Running a blanket profile will hide bottlenecks with very high idle / system usage.

Instead, we turn on and off the profiler to measure only what's important: request handling.

https://github.com/bnoordhuis/node-profiler

lib/profiler.js


I've added lib/profiler to the middleware stack. It ties profiling sessions to actual requests.

It's configurable in config.*.js:

{ profiler: true }

Let's try it


  1. `fakeSlow()` demo
  2. jade 0.31.2 real demo

Tips on Analyzing


  • Look at total vs nonlib before getting hung up on percentages. If JavaScript accounts for a low percentage of total then your app may be running fast even if your nonlib percentages are high.
  • Double-check configuration before optimizing code. Many libraries have configurable performance settings (for example, jade's view cache) that are usually off in development mode.
  • Unusually bad performance is frequently a sign of a bug rather than an algorithm that should be optimized. Check for errors first.

Tips on Speed


  • Run operations in parallel where possible
  • Benchmark regularly
  • Cache where possible (especially with external APIs)
  • Do less (middlewhere only on routes that need it)
  • Offload static files in prod (nginx is faster than node)
  • Concat and minimize in prod (we'll hit this later)

node-performance-profile

By hunterloftis

node-performance-profile

  • 1,358