Jmh: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 4: Line 4:


=Overview=
=Overview=
Executes methods annotated with @Benchmark for a fixed time interval (by default a second) and measure the average execution time per execution.


Introduced by Java 8 & 9 in Action.
Introduced by Java 8 & 9 in Action.
Line 21: Line 23:
JMH will produce the generated benchmark code for this method during compilation, register this method as the benchmark in the benchmark list, read out the default values from the annotations, and generally prepare the environment for the benchmark to run.
JMH will produce the generated benchmark code for this method during compilation, register this method as the benchmark in the benchmark list, read out the default values from the annotations, and generally prepare the environment for the benchmark to run.


Benchmarks may use annotations to control different things in their operations.
By default, jmh executes 20 warmup iterations and 20 measurement iterations per benchmark per fork. These values can be changed on command line or by configuring the annotation appropriately.
* See {@link org.openjdk.jmh.annotations} package for available annotations, read their
 
* Javadocs, and/or look through
==@Fork==
* <a href="http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/">
 
* JMH samples</a> for their canonical uses. As the rule of thumb, most annotations
  @Fork(value=2, jvmArgs={"-Xms4G", "-Xmx4G"})
* may be placed either at the {@link Benchmark} method, or at the enclosing class,
 
* to be inherited by all {@link Benchmark} methods in the class.</p>
Specifies the number of executions in newly forked JVMs, to increase the reliability of the results, and JVM configuration to use.
*
* <p>{@link Benchmark} demarcates the benchmark payload, and JMH treats it specifically
* as the wrapper which contains the benchmark code. In order to run the benchmark reliably,
* JMH enforces a few stringent properties for these wrapper methods, including, but not
* limited to:</p>
* <ul>
*    <li>Method should be public</li>
  *    <li>Arguments may only include either {@link State} classes, which JMH will inject
*    while calling the method (see {@link State} for more details), or JMH infrastructure
*    classes, like {@link org.openjdk.jmh.infra.Control}, or {@link org.openjdk.jmh.infra.Blackhole}</li>
*    <li>Method can only be synchronized if a relevant {@link State} is placed
*    on the enclosing class.</li>
* </ul>
*
* <p>If you want to benchmark methods that break these properties, you have to write them
* out as distinct methods and call them from {@link Benchmark} method.</p>
*
* <p>Benchmark method may declare Exceptions and Throwables to throw. Any exception actually
* raised and thrown will be treated as benchmark failure.</p>

Latest revision as of 20:29, 6 April 2018

External

Overview

Executes methods annotated with @Benchmark for a fixed time interval (by default a second) and measure the average execution time per execution.

Introduced by Java 8 & 9 in Action.

Example

https://github.com/NovaOrdis/playground/tree/master/java/jmh

The Maven shade plugin configuration available here: Maven Shade Plugin.

Annotations

@Benchmark

@Benchmark annotates the benchmark method.

JMH will produce the generated benchmark code for this method during compilation, register this method as the benchmark in the benchmark list, read out the default values from the annotations, and generally prepare the environment for the benchmark to run.

By default, jmh executes 20 warmup iterations and 20 measurement iterations per benchmark per fork. These values can be changed on command line or by configuring the annotation appropriately.

@Fork

@Fork(value=2, jvmArgs={"-Xms4G", "-Xmx4G"})

Specifies the number of executions in newly forked JVMs, to increase the reliability of the results, and JVM configuration to use.