Skip to content

Instantly share code, notes, and snippets.

@frosas
Last active October 25, 2017 15:37
Show Gist options
  • Save frosas/797924a886bfb458ec213701a78cbd9d to your computer and use it in GitHub Desktop.
Save frosas/797924a886bfb458ec213701a78cbd9d to your computer and use it in GitHub Desktop.
Profiling a Node process using flame graphs

Profiling a Node process using flame graphs

(assuming an Ubuntu machine)

Setup

  • Install perf: sudo apt-get install linux-tools-generic
  • Run perf and install any other listed missing package: (e.g.) sudo apt-get install linux-tools-4.4.0-1022-aws linux-cloud-tools-4.4.0-1022-aws
  • Enable symbols mapping when running the Node process: node --perf-basic-prof ...

Profiling

  • If the process is running within Docker, copy symbols map file from the container to the host:
    • Take note of the Node process PID in the host environment. We'll call it $PID.
    • Take note of the Node process PID within the container. We'll call it $DOCKER_PID.
    • sudo docker cp $CONTAINER_ID:/tmp/perf-$DOCKER_PID.map /tmp/perf-$PID.map
  • Profile the process for a while: sudo perf record -F 99 -p $PID -g -v -- sleep 30
  • Transform the profile data: sudo perf script > perf.data-script
  • TODO Filter out noisy entries (i.e. leave just LazyCompile ones)
  • Install FlameGraph: git clone http://github.com/brendangregg/FlameGraph
  • Build flame graph: sudo cat perf.data-script | ./FlameGraph/stackcollapse-perf.pl | ./FlameGraph/flamegraph.pl > flamegraph.svg
  • Export flame graph to a public space: curl --upload-file flamegraph.svg https://transfer.sh/; echo
  • Open the URL in any browser

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment