Video in progress

How do you catch performance regressions in CI before they reach prod: JMH, allocation and GC metrics?

Three layers: JMH benchmarks with -prof gc for critical code (time and allocation per operation), allocation budgets in tests, and a nightly load test with GC and latency numbers. Allocation numbers are much more stable than time, so I often put the gate on those.

📖 Answer

CI machines are noisy: neighboring jobs, turbo boost, different CPUs. A signal like "the benchmark got 8% slower" is often false. So I choose carefully what to put a gate on.

  1. JMH, but with careful comparison. Benchmarks for critical places (a parser, serialization, the calculation core). I store the result with gc.alloc.rate.norm (bytes per operation), which is almost deterministic. For time, I raise a signal only for a big difference (say 25%+) that repeats several times.
java -jar benchmarks.jar -prof gc -rf json -rff result.json
  1. An allocation budget inside a test. For a hot path, an ordinary unit test:
var mx = (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
long before = mx.getCurrentThreadAllocatedBytes();
for (int i = 0; i < 10_000; i++) codec.encode(sample);
long perOp = (mx.getCurrentThreadAllocatedBytes() - before) / 10_000;
assertThat(perOp).isLessThan(512);

(getCurrentThreadAllocatedBytes exists in JDK 14+; do a warm-up loop first.) If someone adds String.format or a stream to the hot path, the test fails right away.

  1. A nightly load test. In a separate, stable environment, with a heap and GC like prod. What is collected: p99, allocation rate, GC CPU share, Old after GC. The results are stored as history and judged by trend, not by one night.

  2. Startup time is a separate metric, because autoscaling depends on it.

Important: every signal should have an owner, and the "green" state should be clearly defined. A benchmark dashboard nobody looks at dies within a month. It is better to have few gates, but reliable ones.