Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 20701f6

Browse files
committed
Setup project for building with Travis CI
1 parent 74df531 commit 20701f6

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

.travic-ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
dist: bionic
2+
language: java
3+
jdk:
4+
- openjdk8
5+
- oraclejdk8
6+
- oraclejdk9
7+
- openjdk10
8+
- oraclejdk11
9+
- openjdk11
10+
- oraclejdk12
11+
- oraclejdk13
12+
- oraclejdk14

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ THE POSSIBILITY OF SUCH DAMAGE.
7474
<version>${jmh.version}</version>
7575
<scope>provided</scope>
7676
</dependency>
77+
78+
<dependency>
79+
<groupId>org.junit.jupiter</groupId>
80+
<artifactId>junit-jupiter-api</artifactId>
81+
<version>5.6.0</version>
82+
<scope>test</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>org.junit.jupiter</groupId>
86+
<artifactId>junit-jupiter-engine</artifactId>
87+
<version>5.6.0</version>
88+
<scope>test</scope>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.assertj</groupId>
92+
<artifactId>assertj-core</artifactId>
93+
<version>3.11.1</version>
94+
<scope>test</scope>
95+
</dependency>
7796
</dependencies>
7897

7998
<build>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)