Microbenchmarks that (in)validate CreednGo rules.
This repository is a Gradle project configured with the JMH benchmarking framework.
Rules often suggest an alternative way of writing small pieces of code. Measuring the effect of a small and isolated amount of code is hard, we need to be cautious. See How NOT to write a microbenchmark, Avoiding Benchmarking Pitfalls on the JVM, the OpenJDK wiki page So, you want to write a microbenchmark, and many others.
To run or create benchmarks, you need a recent JDK. You can use sdkman to setup your environment easily.
By default, this project uses Java 21 (see below to change that).
- Create a new public class in
microbenchs/src/jmh/java/org/greencodeinitiative/creedngo. - Create multiple methods with the
@Benchmarkannotation to compare different scenarios (for example one where the code rule is applied, one where it is not). SeeExample.java.
Use the bench.sh script, which is a wrapper around the JMH jar file. Run ./bench.sh -h too see the available JMH options.
To run a single benchmark function:
./bench.sh BenchmarkClass.benchmarkMethodTo run all the benchmarks in a given class:
./bench.sh BenchmarkClassThe benchmark results are printed to stdout and stored in microbenchs/build/reports/jmh/result.json.
Edit build.gradle.kts and change JavaLanguageVersion.of(XX).
It is recommended to use a recent version of Java (>= 17), as it will benefit from the new compiler blackholes. However, for testing purposes, you may want to run your benchmarks with an older version.