2026-04-05 14:31:17 +00:00
|
|
|
package com.benchmark.runner;
|
|
|
|
|
|
|
|
|
|
import com.benchmark.benchmarks.DataAccessBenchmark;
|
2026-04-05 15:11:36 +00:00
|
|
|
import org.openjdk.jmh.annotations.Mode;
|
|
|
|
|
import org.openjdk.jmh.results.Result;
|
|
|
|
|
import org.openjdk.jmh.results.RunResult;
|
2026-04-05 14:58:49 +00:00
|
|
|
import org.openjdk.jmh.runner.options.CommandLineOptions;
|
2026-04-05 14:31:17 +00:00
|
|
|
import org.openjdk.jmh.runner.Runner;
|
2026-04-05 14:58:49 +00:00
|
|
|
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
|
2026-04-05 14:31:17 +00:00
|
|
|
import org.openjdk.jmh.runner.options.Options;
|
|
|
|
|
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
|
|
|
|
|
2026-04-05 15:11:36 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
2026-04-05 14:31:17 +00:00
|
|
|
public class BenchmarkMain {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2026-04-05 14:58:49 +00:00
|
|
|
if (isJmhListingOrHelpCommand(args)) {
|
|
|
|
|
org.openjdk.jmh.Main.main(args);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandLineOptions commandLineOptions = new CommandLineOptions(args);
|
|
|
|
|
|
|
|
|
|
ChainedOptionsBuilder builder = new OptionsBuilder()
|
|
|
|
|
.parent(commandLineOptions)
|
|
|
|
|
.jvmArgsPrepend(
|
|
|
|
|
"--enable-native-access=ALL-UNNAMED",
|
|
|
|
|
"--sun-misc-unsafe-memory-access=allow",
|
|
|
|
|
"--add-opens=java.base/java.lang=ALL-UNNAMED",
|
2026-04-05 15:11:36 +00:00
|
|
|
"--add-opens=java.base/java.nio=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=java.base/sun.nio.ch=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=java.base/sun.nio.ch=ALL-UNNAMED",
|
2026-04-05 14:58:49 +00:00
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
|
|
|
|
|
"--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
|
|
|
|
|
"--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (args.length == 0) {
|
|
|
|
|
builder.include(DataAccessBenchmark.class.getSimpleName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Options opt = builder.build();
|
2026-04-05 15:11:36 +00:00
|
|
|
Collection<RunResult> runResults = new Runner(opt).run();
|
|
|
|
|
printSortedSummary(runResults);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void printSortedSummary(Collection<RunResult> runResults) {
|
|
|
|
|
if (runResults == null || runResults.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<Mode, List<RunResult>> byMode = new LinkedHashMap<>();
|
|
|
|
|
for (RunResult runResult : runResults) {
|
|
|
|
|
byMode.computeIfAbsent(runResult.getParams().getMode(), m -> new ArrayList<>()).add(runResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("\n=== Sorted summary by performance ===");
|
|
|
|
|
for (Map.Entry<Mode, List<RunResult>> entry : byMode.entrySet()) {
|
|
|
|
|
Mode mode = entry.getKey();
|
|
|
|
|
List<RunResult> sorted = new ArrayList<>(entry.getValue());
|
|
|
|
|
boolean lowerIsBetter = mode == Mode.AverageTime || mode == Mode.SampleTime || mode == Mode.SingleShotTime;
|
|
|
|
|
|
|
|
|
|
sorted.sort((a, b) -> {
|
|
|
|
|
double scoreA = a.getPrimaryResult().getScore();
|
|
|
|
|
double scoreB = b.getPrimaryResult().getScore();
|
|
|
|
|
return lowerIsBetter ? Double.compare(scoreA, scoreB) : Double.compare(scoreB, scoreA);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
System.out.printf("%n[%s]%n", mode);
|
|
|
|
|
System.out.printf("%-24s %14s %14s %s%n", "fixtureType", "score", "error", "unit");
|
|
|
|
|
for (RunResult runResult : sorted) {
|
|
|
|
|
Result<?> result = runResult.getPrimaryResult();
|
|
|
|
|
String fixtureType = runResult.getParams().getParam("fixtureType");
|
|
|
|
|
System.out.printf(
|
|
|
|
|
"%-24s %14.3f %14.3f %s%n",
|
|
|
|
|
fixtureType,
|
|
|
|
|
result.getScore(),
|
|
|
|
|
result.getScoreError(),
|
|
|
|
|
result.getScoreUnit()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 14:31:17 +00:00
|
|
|
}
|
2026-04-05 14:58:49 +00:00
|
|
|
|
|
|
|
|
private static boolean isJmhListingOrHelpCommand(String[] args) {
|
|
|
|
|
for (String arg : args) {
|
|
|
|
|
if ("-l".equals(arg) || "-lp".equals(arg) || "-lprof".equals(arg)
|
|
|
|
|
|| "-h".equals(arg) || "--help".equals(arg)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-04-05 14:31:17 +00:00
|
|
|
}
|