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
+ }
0 commit comments