Skip to content
SvenWoltmannPublic

About

Example code for the HappyCoders article on JMH, the Java Microbenchmark Harness – a JMH project as the Maven archetype sets it up, benchmarks for a baseline, dead-code elimination, constant folding, test data, batch size and threads, and the stopwatch programs that show why System.nanoTime() misleads

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

Repository files navigation

JMH: Benchmarking Java Code

Build Maintainability Rating Reliability Rating Security Rating

The benchmarks and stopwatch programs of the HappyCoders.eu article on the Java Microbenchmark Harness (JMH). The article is in preparation; the links follow with its publication.

The project is set up the way the JMH Maven archetype generates it, with the one addition JDK 23 and newer need: the annotation processor jmh-generator-annprocess is named in the annotationProcessorPaths of the compiler plugin.

Running the benchmarks

The sources need Java 25 or newer. Build the benchmark JAR:

mvn clean package

List the benchmarks, then run one of them – the argument is a regular expression matched against package, class and method name:

java -jar target/benchmarks.jar -l
java -jar target/benchmarks.jar 'eu\.happycoders\.jmh\.IntToStringBenchmark'

IntToStringBenchmark runs with the JMH defaults and takes 8 minutes and 20 seconds; the other benchmarks set shorter runs in their annotations.

Benchmarks

All in the package eu.happycoders.jmh:

  • IntToStringBenchmark – the first benchmark: Integer.toString() in one method, everything else left to the JMH defaults
  • BaselineBenchmark – an empty method and one that returns its input: what a call through the harness costs
  • DeadCodeBenchmark – a result that is dropped, returned, or handed to a Blackhole, for Math.log() and for Integer.toString()
  • ConstantFoldingBenchmark – a constant as input against a non-final field, for Integer.parseInt() and for Integer.toString()
  • LoopBenchmark – one conversion per call against a loop in the benchmark method with @OperationsPerInvocation
  • SortedDataBenchmark – a loop with a branch over sorted and over shuffled numbers, for a thousand and for a million elements
  • SingleTypeBenchmark – toString() on an array that holds one type against an array that mixes three
  • ReadWriteBenchmark – three threads read an AtomicLong while a fourth increments it, with @Group and @GroupThreads
  • BatchSizeBenchmark – appending to an ArrayList for one second per iteration against SingleShotTime with a batch size of 10,000

BenchmarkRunner starts IntToStringBenchmark from a main() method with JMH's OptionsBuilder, the way to run a benchmark from the IDE.

Stopwatch programs

The package eu.happycoders.jmh.stopwatch holds what the article measures without JMH, with System.nanoTime():

  • ClockResolution – the step size of System.nanoTime() on the machine it runs on
  • Stopwatch – a million conversions, timed once
  • StopwatchRounds – the same measurement thirty times in one JVM
  • StopwatchLogarithm – a million logarithms per round, added up
  • StopwatchUnusedResult – the same loop without anybody using the logarithms
  • StopwatchTwoCandidates – two ways to convert, one after the other; with the argument reverse in the opposite order

Run them after mvn compile, for example:

java -cp target/classes eu.happycoders.jmh.stopwatch.Stopwatch

Results

results/m5-pro-jdk27/ holds the logs and JSON files of the runs the article quotes, measured on JDK 27 on an Apple M5 Pro, with a summary in its README.md.


Additional Resources


Java Versions PDF Cheat Sheet

Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!

Java Versions PDF Cheat Sheet Mockup

  • Avoid lengthy research with this concise overview of all Java versions from Java 10 to Java 27.
  • Discover the innovative features of each new Java version, summarized on a single page.
  • Impress your team with your up-to-date knowledge of the latest Java version.

👉 Download the free Java Versions PDF

(Hier geht's zur deutschen Version → Java-Versionen PDF)


The Big O Cheat Sheet

With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.

Big O PDF Cheat Sheet Mockup

  • Always choose the most efficient data structures and thus increase the performance of your applications.
  • Be prepared for technical interviews and confidently present your algorithm knowledge.
  • Become a sought-after problem solver and be known for systematically tackling complex problems.

👉 Download the free Big O Cheat Sheet

(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)


HappyCoders Newsletter

👉 Want to stay on top of modern Java? Sign up for the HappyCoders newsletter – Modern Java: new versions & features, performance, and JVM insights – once a month.

(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)


🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen

Trage dich jetzt unverbindlich auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!

Mastering Data Structures Mockup

👉 Zur Warteliste

About

Example code for the HappyCoders article on JMH, the Java Microbenchmark Harness – a JMH project as the Maven archetype sets it up, benchmarks for a baseline, dead-code elimination, constant folding, test data, batch size and threads, and the stopwatch programs that show why System.nanoTime() misleads

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages