Sub topic 16
Warmup and Benchmarking Pitfalls
Assume you know Java methods, loops, and the command line. You should also know that javac creates bytecode and that the JVM runs that bytecode.
A Java program does not usually run at its final speed from its first instruction. HotSpot starts by interpreting bytecode. As methods and loops become hot, tiered compilation sends them through C1 and possibly C2. Compilation happens in the background, so the early part of a run can include interpretation, compilation, class loading, and other startup work.
What warmup measures
Warmup is the period in which the JVM gathers profile data and compiles frequently used code. A loop can also be compiled while it is already running through on-stack replacement.
The first timing often includes work that will not occur in later iterations. For example, it may include class initialization, allocation of compiler data structures, and time spent by compiler threads. Discard early measurements or run a separate warmup phase.
A warmup phase must exercise the same code and input patterns as the measured phase. If the warmup uses only one implementation of an interface but the measurement uses several, the JVM may need to change its speculative optimizations during measurement.
A simple warmup experiment
This program performs several warmup batches before recording batches. The exact numbers depend on your computer, JVM version, and system load.
public class WarmupDemo {
static long calculate(int count) {
long result = 0;
for (int i = 0; i < count; i++) {
result += (long) (i % 1000) * (i % 1000);
}
return result;
}
public static void main(String[] args) {
final int count = 5_000_000;
long checksum = 0;
for (int i = 0; i < 5; i++) {
checksum += calculate(count);
}
for (int i = 0; i < 5; i++) {
long start = System.nanoTime();
checksum += calculate(count);
long elapsed = System.nanoTime() - start;
System.out.printf("batch %d: %.3f ms%n", i, elapsed / 1_000_000.0);
}
System.out.println("checksum = " + checksum);
}
}The checksum makes the result observable. Without it, an optimizer may remove work whose result is never used. This is one reason hand-written microbenchmarks can report unrealistically low times.
Common benchmarking errors
- Timing only one method call. Startup and timer overhead can dominate the result.
- Including
System.out.printlnin the timed section. Console output is much slower and has different buffering behavior. - Reusing one JVM process for unrelated tests. Earlier tests can warm shared code and affect later tests.
- Ignoring garbage collection. Allocation-heavy tests may measure a particular collection pause rather than normal execution.
- Running with a changing workload. Different inputs can change inlining, branch profiles, and allocation behavior.
- Comparing one run on a busy machine with another run on an idle machine.
- Treating
-Xintor-XX:TieredStopAtLevel=1results as production performance. These flags intentionally change normal execution.
A fork starts a separate JVM process. Separate forks reduce contamination from previous tests and reveal run-to-run variation. For application-level measurements, test a realistic workload after the application has reached its normal operating state instead of timing an isolated method.
Exercise
Why can the first measured iteration of a Java loop be slower than later iterations?
Show answer
It can include interpretation, class initialization, profiling, JIT compilation, compiler-thread activity, and other startup work that later iterations avoid.