JMH (Java Microbenchmark Harness)

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/55

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

56 Terms

1
New cards

What is JMH?

Java Microbenchmark Harness, a toolkit for building, running, and analyzing Java benchmarks.

2
New cards

Who developed JMH?

The same team at Oracle responsible for the Java Virtual Machine (JVM).

3
New cards

What is the main purpose of JMH?

To create accurate and reliable benchmarks for Java code.

4
New cards

Why not use System.nanoTime() directly for benchmarking?

It is prone to noise, JIT optimizations, and can yield misleading results without warmup and control.

5
New cards

What does JMH help avoid?

Common benchmarking pitfalls such as dead-code elimination, loop unrolling, and JVM warmup effects.

6
New cards

How do you include JMH in a Maven project?

Use the jmh-generator-annprocess and jmh-core dependencies.

7
New cards

What is the annotation used to mark a benchmark method?

@Benchmark

8
New cards

What does the @Benchmark annotation indicate?

That the annotated method should be measured by the JMH harness.

9
New cards

What is @State in JMH?

Marks a class that holds benchmark state; it determines the lifecycle of benchmark instances.

10
New cards

What are the common @State scopes in JMH?

Thread, Group, Benchmark.

11
New cards

What does @State(Scope.Thread) mean?

Each thread gets its own instance of the benchmark class.

12
New cards

What does @BenchmarkMode do?

Defines how the benchmark method’s performance is measured.

13
New cards

What are the common BenchmarkMode options?

Throughput, AverageTime, SampleTime, SingleShotTime, AllModes.

14
New cards

What is BenchmarkMode.Throughput?

Measures how many operations are performed per time unit.

15
New cards

What is BenchmarkMode.AverageTime?

Measures the average time taken per operation.

16
New cards

What is BenchmarkMode.SampleTime?

Measures time for individual samples and provides distribution.

17
New cards

What is BenchmarkMode.SingleShotTime?

Measures time for a single invocation, used for cold-start benchmarking.

18
New cards

What does @OutputTimeUnit do?

Specifies the time unit for the benchmark output (e.g., seconds, milliseconds).

19
New cards

What is the purpose of @Fork?

Specifies how many times the benchmark should be forked in a new JVM instance.

20
New cards

What is the default fork count in JMH?

1

21
New cards

Why does JMH fork JVMs?

To ensure a clean environment and isolate benchmarks from JVM optimizations.

22
New cards

What does @Warmup do in JMH?

Defines how many iterations should run before measurement begins to allow JVM warmup.

23
New cards

Why is warmup necessary in benchmarking?

To allow the JVM to optimize the code and reach steady-state performance.

24
New cards

What does @Measurement do in JMH?

Specifies the number of measurement iterations and duration of each.

25
New cards

What is @Param in JMH?

Used to define parameters that can be injected into benchmark methods to test with multiple values.

26
New cards

What is the purpose of blackhole in JMH?

To consume values in a way that prevents the compiler from optimizing them away.

27
New cards

What class is used for blackhole consumption?

org.openjdk.jmh.infra.Blackhole

28
New cards

How do you run a JMH benchmark from command line?

Use the generated JAR with java -jar benchmarks.jar

29
New cards

What does the JMH plugin do in Maven?

Generates the benchmark source files and executable JARs.

30
New cards

How do you benchmark multiple methods?

Create multiple methods annotated with @Benchmark in the same class.

31
New cards

Can JMH benchmark private methods?

No, benchmark methods must be public.

32
New cards

Can JMH benchmark static methods?

Yes, as long as they are annotated with @Benchmark and public.

33
New cards

What is a common pitfall in benchmarking Java code?

Letting the JIT compiler eliminate or optimize away the code under test.

34
New cards

What is the recommended iteration time for warmup and measurement in JMH?

At least 1 second per iteration is recommended.

35
New cards

How do you configure JMH benchmark parameters programmatically?

Use OptionsBuilder and Runner classes.

36
New cards

Can JMH benchmark multithreaded code?

Yes, using @Threads or @Group annotations.

37
New cards

What does @Threads do?

Defines the number of threads to run a benchmark method concurrently.

38
New cards

What is @Group used for in JMH?

To define group benchmarks for measuring interaction of multiple methods.

39
New cards

What is false sharing, and how can JMH detect it?

False sharing occurs when threads write to variables on the same cache line; JMH can help detect it via @Contended (Java 8+) or careful structuring.

40
New cards

What is the use of -prof perfasm option in JMH?

To use the perf profiler and disassemble JIT-compiled code for analysis.

41
New cards

What are common profilers available in JMH?

gc, stack, perfasm, async, jfr, dtrace.

42
New cards

Can JMH benchmarks be used in production?

No, they are intended for microbenchmarking in test environments.

43
New cards

How does JMH prevent dead-code elimination?

By consuming outputs using Blackhole or returning results from benchmark methods.

44
New cards

What does the @Setup annotation do?

Initializes state before each iteration, trial, or invocation.

45
New cards

What does @TearDown do in JMH?

Cleans up resources after each iteration, trial, or invocation.

46
New cards

What levels are available for @Setup and @TearDown?

Level.Invocation, Level.Iteration, Level.Trial

47
New cards

What does Level.Invocation mean?

The method runs before/after every single benchmark invocation.

48
New cards

What does Level.Iteration mean?

The method runs before/after each benchmark iteration.

49
New cards

What does Level.Trial mean?

The method runs once per forked JVM instance.

50
New cards

Can JMH benchmark allocations?

Yes, using the -prof gc or -prof perfasm profilers or analyzing GC metrics.

51
New cards

How to benchmark with different input sizes in JMH?

Use @Param to vary input values across iterations.

52
New cards

How to include multiple benchmark classes in one run?

Use includes in OptionsBuilder or wildcards in CLI.

53
New cards

Can JMH benchmark across different JVMs or vendors?

Yes, it's common to test performance across OpenJDK, Oracle JDK, GraalVM, etc.

54
New cards

Is JMH suitable for benchmarking large-scale applications?

No, it's best for fine-grained microbenchmarking.

55
New cards

What is the format of JMH output?

Typically includes score, error margin, unit, and optional throughput or time per op.

56
New cards

Can JMH benchmarks be used for regression tracking?

Yes, to detect performance regressions across code or JVM versions.