Warmup and measurement
What you will learn
Warmup gives the JVM time to load classes, gather profiles and compile hot code. Measurement records observations under specified conditions. A fresh JVM fork separates runtime histories; neither warmup nor forks guarantee steady state.
- Distinguish warmup, iterations and forks.
- Calculate nearest-rank percentiles and throughput.
- Interpret variation without claiming a universal winner.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples; measurements are separate. JMH is source-reviewed, not executed.
Select a nearest-rank percentile
public class Main { static long percentile(long[] samples,int p) { if(samples.length==0 || p<1 || p>100) throw new IllegalArgumentException("invalid percentile"); long[] sorted=samples.clone(); for(long n:sorted) if(n<0) throw new IllegalArgumentException("negative duration"); java.util.Arrays.sort(sorted); int rank=(int)((p*(long)sorted.length+99)/100); return sorted[rank-1]; } public static void main(String[] args) { long[] a={50,10,20,30,40}; System.out.println(percentile(a,50)); System.out.println(percentile(a,95)); System.out.println(a[0]); }}Expected output:
305050Synthetic milliseconds. p95 selects the fifth value; sorting a copy preserves input.
Plan repetitions
JMH separates warmup and measurement: @Warmup(iterations=3,time=1), @Measurement(iterations=5,time=1) and @Fork(3) are instructional settings. AverageTime reports time per operation, Throughput reports operations per unit time, and SampleTime samples operation durations. Choose a mode and unit. Iteration averages are not request percentiles.
Read the actual experiment
The companion run_experiment.py launches four independent JVMs with fixed heap, active processor count and seeded inputs. Both variants get equal warmup and measurement rounds; order alternates across forks. Raw measured nanoseconds and checked results are retained. Batch means hide tails. Other host load is uncontrolled and variants share profiles within each fork. This is exploratory, not a JMH result.
Rates and tails
Nearest-rank p95 selects rank ceil(0.95 × n) in sorted samples, counting from one. Other methods interpolate. Small samples make extreme percentiles coarse. Throughput is completed operations divided by elapsed seconds. Include errors and dropped work; never average percentiles across instances.
Compute completed throughput
public class Main { static double rate(long completed,long nanos) { if(completed<0||nanos<=0) throw new IllegalArgumentException(); return completed/(nanos/1_000_000_000.0); } public static void main(String[] args) { System.out.println(rate(1200,2_000_000_000L)); }}Expected output:
600.0Synthetic 1,200 completions in two seconds.
Separate warmup observations
public class Main {
public static void main(String[] args) { long[] warm={90,70}; long[] measured={40,42,38}; System.out.println(java.util.Arrays.stream(warm).average().orElseThrow()); System.out.println(java.util.Arrays.stream(measured).average().orElseThrow()); }}Expected output:
80.040.0Fictional warmup values are not mixed into the measured mean.
Common mistakes
Do not use wall-clock timestamps for elapsed durations, interpret batch averages as request p99, or report just the fastest fork.
Practice
Calculate p50 and p100 for 2, 9, 4, 6 milliseconds using nearest rank. Print 4 and 9.
Expected output:
49Compare with the manual review criteria.
Further reading: JMH · Constant folding · Load. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Warmup and measurement — knowledge check
Performance engineering reasoning.
The first two batches are much slower than later batches. What is the correct treatment?
Why repeat in independent JVM processes rather than only adding iterations in one process?
Sorted durations are 10, 20, 30, 40, 50 ms. Using nearest-rank p95, which value is selected?
A two-second observation window contains 100 completed operations. What is completed throughput?
Two batches both average 10 ms. What can be concluded about individual-operation p99?