Tuesday 2 August 2022

JMH: Configure a Fork with OptionsBuilder

OptionsBuilder is used to configure a Fork.

 

Example

Options opt = new OptionsBuilder()
.include(OptionsBuilderDemo.class.getSimpleName())
.forks(1)
.measurementIterations(4)
.warmupIterations(3)
.resultFormat(ResultFormatType.JSON)
.build();

 

Above snippet notify JMH to run the bench mark with one fork, where there are

a.   3 warmup iterations,

b.   4 actual iterations

c.    Result format is of type json.

 

Find the below working application.

 


OptionsBuilderDemo.java

 

package com.sample.app;

import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

public class OptionsBuilderDemo {
	private static final String INPUT_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	private static SecureRandom SECURE_RANDOM = new SecureRandom();
	private static final int RAND_STR_LENGTH = 16;

	@Benchmark
	public String randString() throws InterruptedException {

		// Some intentional delay
		TimeUnit.MILLISECONDS.sleep(10);
		final StringBuilder sb = new StringBuilder(RAND_STR_LENGTH);

		for (int i = 0; i < RAND_STR_LENGTH; i++)
			sb.append(INPUT_CHARS.charAt(SECURE_RANDOM.nextInt(INPUT_CHARS.length())));

		return sb.toString();
	}

	public static void main(String[] args) throws RunnerException {
		Options opt = new OptionsBuilder()
				.include(OptionsBuilderDemo.class.getSimpleName())
				.forks(1)
				.measurementIterations(4)
				.warmupIterations(3)
				.resultFormat(ResultFormatType.JSON)
				.build();

		new Runner(opt).run();
	}

}

Output

# JMH version: 1.35
# VM version: JDK 15.0.2, Java HotSpot(TM) 64-Bit Server VM, 15.0.2+7-27
# VM invoker: /Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home/bin/java
# VM options: -Dfile.encoding=UTF-8 -XX:+ShowCodeDetailsInExceptionMessages
# Blackhole mode: full + dont-inline hint (auto-detected, use -Djmh.blackhole.autoDetect=false to disable)
# Warmup: 3 iterations, 10 s each
# Measurement: 4 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.sample.app.OptionsBuilderDemo.randString

# Run progress: 0.00% complete, ETA 00:01:10
# Fork: 1 of 1
# Warmup Iteration   1: 87.126 ops/s
# Warmup Iteration   2: 86.719 ops/s
# Warmup Iteration   3: 87.808 ops/s
Iteration   1: 87.395 ops/s
Iteration   2: 87.326 ops/s
Iteration   3: 86.841 ops/s
Iteration   4: 88.775 ops/s


Result "com.sample.app.OptionsBuilderDemo.randString":
  87.584 ±(99.9%) 5.373 ops/s [Average]
  (min, avg, max) = (86.841, 87.584, 88.775), stdev = 0.831
  CI (99.9%): [82.211, 92.957] (assumes normal distribution)


# Run complete. Total time: 00:01:15

REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
experiments, perform baseline and negative tests that provide experimental control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.

Benchmark                       Mode  Cnt   Score   Error  Units
OptionsBuilderDemo.randString  thrpt    4  87.584 ± 5.373  ops/s

Benchmark result is saved to jmh-result.json

 

From the output, you can confirm that the final result is saved to jmh-result.json file. Generated jms-result.json file is given below.

 

jmh-result.json

[
    {
        "jmhVersion" : "1.35",
        "benchmark" : "com.sample.app.OptionsBuilderDemo.randString",
        "mode" : "thrpt",
        "threads" : 1,
        "forks" : 1,
        "jvm" : "/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home/bin/java",
        "jvmArgs" : [
            "-Dfile.encoding=UTF-8",
            "-XX:+ShowCodeDetailsInExceptionMessages"
        ],
        "jdkVersion" : "15.0.2",
        "vmName" : "Java HotSpot(TM) 64-Bit Server VM",
        "vmVersion" : "15.0.2+7-27",
        "warmupIterations" : 3,
        "warmupTime" : "10 s",
        "warmupBatchSize" : 1,
        "measurementIterations" : 4,
        "measurementTime" : "10 s",
        "measurementBatchSize" : 1,
        "primaryMetric" : {
            "score" : 87.58432197913439,
            "scoreError" : 5.373063970856802,
            "scoreConfidence" : [
                82.21125800827758,
                92.95738594999119
            ],
            "scorePercentiles" : {
                "0.0" : 86.84072256619667,
                "50.0" : 87.36057449834817,
                "90.0" : 88.77541635364449,
                "95.0" : 88.77541635364449,
                "99.0" : 88.77541635364449,
                "99.9" : 88.77541635364449,
                "99.99" : 88.77541635364449,
                "99.999" : 88.77541635364449,
                "99.9999" : 88.77541635364449,
                "100.0" : 88.77541635364449
            },
            "scoreUnit" : "ops/s",
            "rawData" : [
                [
                    87.39480245630831,
                    87.32634654038804,
                    86.84072256619667,
                    88.77541635364449
                ]
            ]
        },
        "secondaryMetrics" : {
        }
    }
]





 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment