Search code examples
javajsonbenchmarkingjmh

JMH: Returning the benchmark results as a json object


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?


Solution

  • 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