Skip to content

Commit 472ff9a

Browse files
committed
jmh
1 parent 02f062c commit 472ff9a

File tree

6 files changed

+677
-215
lines changed

6 files changed

+677
-215
lines changed

pom.xml

+14-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
<junit-jupiter.version>5.9.0</junit-jupiter.version>
5959
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
6060
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
61-
<mockito-junit-jupiter.version>4.2.0</mockito-junit-jupiter.version>
61+
<mockito-junit-jupiter.version>4.10.0</mockito-junit-jupiter.version>
62+
<jmh.version>1.35</jmh.version>
6263
</properties>
6364

6465
<dependencies>
@@ -96,7 +97,18 @@
9697
<version>${mockito-junit-jupiter.version}</version>
9798
<scope>test</scope>
9899
</dependency>
99-
100+
<dependency>
101+
<groupId>org.openjdk.jmh</groupId>
102+
<artifactId>jmh-core</artifactId>
103+
<version>${jmh.version}</version>
104+
<scope>test</scope>
105+
</dependency>
106+
<dependency>
107+
<groupId>org.openjdk.jmh</groupId>
108+
<artifactId>jmh-generator-annprocess</artifactId>
109+
<version>${jmh.version}</version>
110+
<scope>test</scope>
111+
</dependency>
100112
</dependencies>
101113

102114
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.jnape.palatable.lambda.effect.io.fiber.benchmark;
2+
3+
import org.openjdk.jmh.results.format.ResultFormatType;
4+
import org.openjdk.jmh.runner.Runner;
5+
import org.openjdk.jmh.runner.RunnerException;
6+
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
7+
import org.openjdk.jmh.runner.options.Options;
8+
import org.openjdk.jmh.runner.options.OptionsBuilder;
9+
10+
import java.io.File;
11+
12+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Filter.filter;
13+
import static com.jnape.palatable.lambda.functions.builtin.fn3.FoldLeft.foldLeft;
14+
import static java.lang.String.join;
15+
import static java.util.Arrays.asList;
16+
import static java.util.Arrays.copyOf;
17+
18+
public final class Benchmark {
19+
20+
static final int OPS_PER_BENCHMARK = 1_000_000_000;
21+
22+
private static final String ROOT_PACKAGE_PATH = join("/", "src", "test", "java");
23+
private static final String RESULTS_DIR_NAME = "results";
24+
25+
public static void runBenchmarks(Class<?> benchmarkClass, String... methods) throws RunnerException {
26+
new Runner(options(benchmarkClass, methods)).run();
27+
}
28+
29+
private static Options options(Class<?> benchmarkClass, String[] methods) {
30+
ChainedOptionsBuilder optionsBuilder = new OptionsBuilder()
31+
.resultFormat(ResultFormatType.JSON)
32+
.result(resultsFilePath(benchmarkClass));
33+
34+
String benchmarkName = benchmarkClass.getCanonicalName();
35+
optionsBuilder = methods.length == 0
36+
? optionsBuilder.include(benchmarkName)
37+
: foldLeft((b, m) -> b.include(benchmarkName + "." + m),
38+
optionsBuilder,
39+
asList(methods));
40+
41+
return optionsBuilder.build();
42+
}
43+
44+
private static String resultsFilePath(Class<?> benchmarkClass) {
45+
String packageName = benchmarkClass.getPackage().getName();
46+
String[] nestedClassesAndName = benchmarkClass.getCanonicalName().substring(packageName.length() + 1).split("\\.");
47+
String packagePath = joinPrune("/", packageName.split("\\."));
48+
String nestedClassPath = joinPrune("/", copyOf(nestedClassesAndName, nestedClassesAndName.length - 1));
49+
50+
String resultsDirectoryPath = joinPrune("/", ROOT_PACKAGE_PATH, packagePath, RESULTS_DIR_NAME, nestedClassPath);
51+
String fileName = joinPrune(".", nestedClassesAndName[nestedClassesAndName.length - 1], "jmh", "json");
52+
File resultsDirectory = new File(resultsDirectoryPath);
53+
if (!(resultsDirectory.mkdirs() || resultsDirectory.isDirectory())) {
54+
throw new IllegalStateException("Failed to create JMH results directory: " + resultsDirectoryPath);
55+
}
56+
return join("/", resultsDirectoryPath, fileName);
57+
}
58+
59+
60+
private static String joinPrune(CharSequence delimiter, CharSequence... parts) {
61+
return join(delimiter, filter(s -> !s.isEmpty(), asList(parts)));
62+
}
63+
}

src/test/java/com/jnape/palatable/lambda/effect/io/fiber/benchmark/FiberBenchmark.java

-162
This file was deleted.

0 commit comments

Comments
 (0)