Skip to content
Ameen Abozaid
Planned lessonSRC-016-L02

Microbenchmarks with JMH

  • Lesson 2 of 8
  • Senior C
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

measure small code paths without common traps.

Planned topics

  • JMH (Java Microbenchmark Harness) handles warm-up, forking, and measurement iterations.
  • Traps: dead-code elimination (use a Blackhole or return values), constant folding, a loop the JIT optimizes away, shared state, unrepresentative data.
  • Run several forks and report variance, not one number.
  • Microbenchmarks answer narrow questions; confirm that the code path matters in the whole system.
Draft outline details

Demonstration

java
@Benchmark
public int parseTotal(Fixture f, Blackhole bh) {
    return f.parser.total(f.line);   // input from @State, result consumed
}

Use @Fork, @Warmup, @Measurement with values suited to the case; verify JMH version before recording.

Check your understanding

Why return or consume the result in a benchmark?

Show answer

Otherwise the JIT may remove the computation as dead code.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.