I am developing an integrated Benchmarking into an application. I want to use JMH as my framework.
How can I receive the results as an JSON object?
I know I can save it in a file with the following running options:
org.openjdk.jmh.runner.options.Options opt = new OptionsBuilder()
.include(WorkerBenBenchmarkObject.class.getSimpleName())
.shouldDoGC(true)
.resultFormat(ResultFormatType.JSON)
.result("benchmark-result/" + System.currentTimeMillis() + ".json")
.addProfiler(StackProfiler.class)
.jvmArgsAppend("-Djmh.stack.period=1")
.warmupIterations(5)
.measurementIterations(5)
.forks(1)
.build();
new Runner(opt).run();
How can I receive this results without needing to read the file?
There are two ways to run the JMH benchmark, uses Maven or run it via a JMH Runner class directly. 3.1 Maven, package it as a JAR and run it via org. openjdk.
JMH is short for Java Microbenchmark Harness. JMH is a toolkit that helps you implement Java microbenchmarks correctly. JMH is developed by the same people who implement the Java virtual machine, so these guys know what they are doing.
The solution was quite than easy than I thought. The important part is to start the spring-boot application when the benchmark is getting initialized. Define a class level variable for configuration context and give a reference to it during setup of the benchmark. Make a call to the bean method inside the benchmark.
If you run JMH from the command line there is an option (-rf) to set the output format.
For JSON output:
java -jar benchmarks.jar -rf json
To get a list of other formats and the options to select:
java -jar benchmarks.jar -lrf
Available formats: text, csv, scsv, json, latex
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With