Phase 08 — Performance Engineering
Why This Phase Matters
An Apache committer's most common review comment isn't about style — it's "do you have numbers to back this up?"
Performance claims without measurements are opinions. JMH-backed numbers are facts you can put in a commit message, a PR description, or an ASF mailing list discussion.
This phase gives you the toolkit to measure, interpret, and defend performance claims for JVM code at the level Apache reviewers expect.
Labs
| # | Title | Core skill | Key class |
|---|---|---|---|
| 01 | JMH Microbenchmarks | Write and interpret JMH benchmarks | StringConcatBenchmark, CollectionLookupBenchmark |
| 02 | GC & Allocation Profiling | Measure autoboxing and allocation overhead | BoxingBenchmark, StreamVsLoopBenchmark |
| 03 | Lock Contention Analysis | Compare synchronization primitives under thread contention | ContentionBenchmark, ContentionCorrectnessTest |
Core Concepts
| Concept | What it means | Why it matters |
|---|---|---|
| Dead code elimination | JIT removes code whose result is never used — defeats naive benchmarks | JMH uses Blackhole to prevent this |
| Constant folding | JIT computes 2 + 2 at compile time — benchmark measures nothing | JMH uses @State fields to prevent this |
| JIT warm-up | First 10k–50k iterations are interpreted; they are not representative | JMH @Warmup discards early iterations |
| Coordinated omission | Recording only when you're ready to record hides high-latency tails | Use JMH SampleTime mode for latency distributions |
| False sharing | Two fields on the same cache line — writing one invalidates the other | Use @Contended / padding for high-frequency shared fields |
What You'll Build
- Lab 01: Three JMH benchmarks that expose common Java anti-patterns (
+=in loops, O(n) contains, double map lookups). Reproducible numbers you can quote in code reviews. - Lab 02: JMH benchmarks measuring autoboxing overhead and stream vs imperative loop cost. Correctness tests verifying that the "fast" versions produce identical results.
- Lab 03: Four
Counterimplementations (synchronized,ReentrantLock,AtomicLong,LongAdder) benchmarked under 4-thread contention. Correctness test confirms thread safety.
Prerequisites
- Phase 07 Lab 01 (Raft) — concurrency concepts
- Java 11+, Maven 3.8+
Interview Relevance
"Why is
synchronizedsometimes faster thanAtomicLongat low thread counts?"
"What's wrong with usingSystem.currentTimeMillis()for benchmarking?"
"How would you measure the overhead of autoboxing in a hot path?"
All three have direct answers from this phase's lab output.