Tuesday, 2 August 2022

Run benchmarks using JMH runner

 

Step 1: Create a benchmark.

@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();
}

 

Step 2: Define JMH Runner options.

Options opt = new OptionsBuilder()
.include(JMHRunnerDemo.class.getSimpleName())
.forks(1)
.measurementIterations(4)
.warmupIterations(3)
.build();

Step 3: Run the benchmark with Runner instance.

new Runner(opt).run();

Find the below working application.


 

JMHRunnerDemo.java

package com.sample.app;

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

import org.openjdk.jmh.annotations.Benchmark;
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 JMHRunnerDemo {
	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(JMHRunnerDemo.class.getSimpleName())
				.forks(1)
				.measurementIterations(4)
				.warmupIterations(3)
				.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.JMHRunnerDemo.randString

# Run progress: 0.00% complete, ETA 00:01:10
# Fork: 1 of 1
# Warmup Iteration   1: 87.196 ops/s
# Warmup Iteration   2: 87.520 ops/s
# Warmup Iteration   3: 87.447 ops/s
Iteration   1: 86.724 ops/s
Iteration   2: 86.354 ops/s
Iteration   3: 86.534 ops/s
Iteration   4: 87.832 ops/s


Result "com.sample.app.JMHRunnerDemo.randString":
  86.861 ±(99.9%) 4.296 ops/s [Average]
  (min, avg, max) = (86.354, 86.861, 87.832), stdev = 0.665
  CI (99.9%): [82.566, 91.157] (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
JMHRunnerDemo.randString  thrpt    4  86.861 ± 4.296  ops/s






 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment