Skip to content

Commit 47aee06

Browse files
JMH Benchmark for implementations
1 parent adac516 commit 47aee06

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<jspecify.version>1.0.0</jspecify.version>
4242
<junit.version>5.13.4</junit.version>
4343
<assertj.version>3.27.4</assertj.version>
44+
<jmh.version>1.37</jmh.version>
4445

4546
<!-- Plugins versions -->
4647
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
@@ -70,6 +71,18 @@
7071
<scope>test</scope>
7172
</dependency>
7273

74+
<dependency>
75+
<groupId>org.openjdk.jmh</groupId>
76+
<artifactId>jmh-core</artifactId>
77+
<version>${jmh.version}</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.openjdk.jmh</groupId>
81+
<artifactId>jmh-generator-annprocess</artifactId>
82+
<version>${jmh.version}</version>
83+
<!-- <scope>provided</scope>-->
84+
</dependency>
85+
7386
</dependencies>
7487

7588
<build>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.github.computerdaddyguy.jfiletreeprettyprinter.benchmarks;
2+
3+
import io.github.computerdaddyguy.jfiletreeprettyprinter.FileTreePrettyPrinter;
4+
import io.github.computerdaddyguy.jfiletreeprettyprinter.PrettyPrintOptions.Implementation;
5+
import java.nio.file.Path;
6+
import java.util.concurrent.TimeUnit;
7+
import org.openjdk.jmh.annotations.Benchmark;
8+
import org.openjdk.jmh.annotations.BenchmarkMode;
9+
import org.openjdk.jmh.annotations.Level;
10+
import org.openjdk.jmh.annotations.Mode;
11+
import org.openjdk.jmh.annotations.OutputTimeUnit;
12+
import org.openjdk.jmh.annotations.Param;
13+
import org.openjdk.jmh.annotations.Scope;
14+
import org.openjdk.jmh.annotations.Setup;
15+
import org.openjdk.jmh.annotations.State;
16+
17+
/**
18+
* JMH benchmark to compare performance of different FileTreePrettyPrinter implementations.
19+
*/
20+
@BenchmarkMode(Mode.AverageTime)
21+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
22+
@State(Scope.Benchmark)
23+
public class FileTreePrettyPrinterBenchmark {
24+
25+
@Param({ "VISITOR", "RECURSIVE" })
26+
private String implementationName;
27+
28+
private FileTreePrettyPrinter printer;
29+
30+
private Path inputPath;
31+
32+
/**
33+
* To run from your IDE.
34+
*/
35+
public static void main(String[] args) throws Exception {
36+
org.openjdk.jmh.Main.main(args);
37+
}
38+
39+
@Setup(Level.Trial)
40+
public void setup() {
41+
Implementation impl = Implementation.valueOf(implementationName);
42+
printer = FileTreePrettyPrinter.builder()
43+
.customizeOptions(options -> options.withImplementation(impl))
44+
.build();
45+
46+
inputPath = Path.of("src/example/resources/children_limit_dynamic");
47+
}
48+
49+
@Benchmark
50+
public String benchmarkPrettyPrint() {
51+
return printer.prettyPrint(inputPath);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)