|
| 1 | +package com.github.blalasaadri; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.openjdk.jmh.results.RunResult; |
| 5 | +import org.openjdk.jmh.runner.Runner; |
| 6 | +import org.openjdk.jmh.runner.RunnerException; |
| 7 | +import org.openjdk.jmh.runner.options.Options; |
| 8 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 9 | +import org.openjdk.jmh.runner.options.TimeValue; |
| 10 | + |
| 11 | +import java.text.DecimalFormat; |
| 12 | +import java.util.Collection; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 16 | + |
| 17 | +/** |
| 18 | + * Heavily inspired by <a href="https://www.retit.de/continuous-benchmarking-with-jmh-and-junit/">Continuous Benchmarking with JMH and JUnit</a>. |
| 19 | + */ |
| 20 | +class MyBenchmarkTest { |
| 21 | + |
| 22 | + private static DecimalFormat df = new DecimalFormat("0.000"); |
| 23 | + private static final double REFERENCE_SCORE = 37.132; |
| 24 | + |
| 25 | + @Test |
| 26 | + public void runJmhBenchmark() throws RunnerException { |
| 27 | + Options opt = new OptionsBuilder() |
| 28 | + .measurementTime(TimeValue.seconds(1)) |
| 29 | + .measurementIterations(3) |
| 30 | + .forks(3) |
| 31 | + .include(MyBenchmark.class.getSimpleName()) |
| 32 | + .build(); |
| 33 | + Collection<RunResult> runResults = new Runner(opt).run(); |
| 34 | + assertFalse(runResults.isEmpty()); |
| 35 | + |
| 36 | + assertThat(runResults) |
| 37 | + .allSatisfy(runResult -> assertDeviationWithin(runResult, REFERENCE_SCORE, 0.05)); |
| 38 | + } |
| 39 | + |
| 40 | + private static void assertDeviationWithin(RunResult result, double referenceScore, double maxDeviation) { |
| 41 | + double score = result.getPrimaryResult().getScore(); |
| 42 | + double deviation = Math.abs(score / referenceScore - 1); |
| 43 | + String deviationString = df.format(deviation * 100) + "%"; |
| 44 | + String maxDeviationString = df.format(maxDeviation * 100) + "%"; |
| 45 | + String errorMessage = "Deviation " + deviationString + " exceeds maximum allowed deviation " + maxDeviationString; |
| 46 | + assertThat(deviation) |
| 47 | + .withFailMessage(errorMessage) |
| 48 | + .isLessThan(maxDeviation); |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments