|
| 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