Learn / Performance / Lesson 16

Measuring performance

A change is faster only when a measurement says so, beyond the noise. How to run an optimisation loop that keeps real wins and never trades away correctness or security.

Last updated: 2026-09-19

What it is

Performance work has two kinds of numbers:

An objective can be traded for another objective. A constraint is never traded for anything.

Why it is a rule

Speed changes the world outside the server too.

How to do it

The loop

text
inspect → find the bottleneck → measure a baseline → form a hypothesis
→ make ONE change → run the tests → check security
→ benchmark → compare with the best known → keep or revert → repeat

One change at a time

Two changes measured together cannot be told apart, and a regression hides behind a win.

Know your noise

Run the same benchmark several times without changing anything. The spread you see is the noise. A "win" smaller than the noise is not a win. Keep the change only when it beats the best known result by more than that.

Look at the tail, not only the average

The average hides the slow requests people actually complain about. Report the median (p50), p95 and p99. A change that improves p50 but worsens p99 made things worse for your unluckiest users.

Measure memory with speed

Record steady and peak RAM with every run. A faster change that raises steady-state memory toward the ceiling is a regression on a small server.

Work from the top down

  1. Algorithms: O(n²) to O(n log n), repeated passes to one.
  2. Database: N+1 queries, repeated identical queries, missing indexes, columns nobody reads.
  3. I/O: repeated file reads, network calls, parsing.
  4. Computation repeated with the same inputs.
  5. Memory: copies, allocations, object lifetimes.
  6. Micro-optimisations: last, and only with a measured win.

Keep a log

Every attempt, kept or reverted, with its numbers. The reverted ones stop the next person trying the same idea.

How we do it here

Optimisation passes follow this loop exactly: one change per iteration, the full test suite, a security read of the diff, then a benchmark compared against the best known result, beyond a measured noise threshold. Benchmark results are machine-specific and never committed.

Benefits

Disadvantages

Checklist

Sources

Read this lesson as Markdown