04.5.3 · Skill in Performance engineering

Profiling

Measuring where time and resources actually go.

Profiling replaces guesses with measurements. CPU profiles, flame graphs, query plans and browser dev tools show the real bottleneck, which is rarely where you expected. Optimize only after profiling.

Your status · Profiling

Saved in this browser. Sign in to keep it on every device.It colours this skill's tile on your map.

Why it matters for a product engineer

Profiling replaces guesses with measurements. The slow part of a system is rarely where the team expects it to be. A profile shows a product engineer where time and memory really go, so a fix targets the cause, and its effect can be checked afterwards.

How to do it

  1. Start from a symptom people feel and a number: a slow page, a slow endpoint, a p95 that grew.
  2. Reproduce it with realistic data, or profile production with a sampling profiler. Sampling costs little, so it changes the program’s behaviour less than tracing every call.
  3. Choose the profile that fits the symptom: CPU for busy code, a query plan for a slow query, the browser’s Performance panel for the interface, a memory profile for growth over time.
  4. Look for the widest part of the profile, not the most surprising one. Fix one thing at a time.
  5. Measure again under the same conditions. Keep the numbers from before and after with the change.

Common mistakes

  • Optimizing before profiling.
  • Profiling on a laptop with a tiny database, and trusting the result.
  • Tracing every function call, which slows the program and changes what you measure.
  • Reading the x-axis of a flame graph as time.
  • Stopping after one fix without measuring again.

Example

Brendan Gregg invented flame graphs while working on a MySQL performance problem. The usual profilers gave him “walls of text”. Tracing every function call cost too much and disturbed the program, so he switched to timed sampling. Samples lose the order of calls, so he gave up time on the x-axis and sorted the stacks to merge identical frames. The picture became readable, and he released flame graphs in December 2011.

Side by side

A flame graph and a flame chart look alike

Flame graphFlame chart
The x-axisStacks sorted by nameTime
A wide frameIn more samplesRan for longer
Shows patterns over timeNoYes
Many threads in one viewYesNo

In a flame graph, left to right is only alphabetical order. Look for the widest frames along the top edge: that is where the CPU spends its time.

ReportedBrendan Gregg, Flame graphs

Sources: Flame graphs (Brendan Gregg) ↗

Resources

1
  • articleGo deeper
    Flame graphs ↗
    Brendan Gregg · 20 min

    From the person who invented them: how to read a flame graph and find where CPU time really goes.

Tools

2
Tools for Performance engineering