Wednesday 3 August 2022

Run JMH benchmarking from a junit test

Define a benchmark method in the test class.

@Benchmark
    public void test() {
        String result = StringUtil.replaceMultipleSpaces("Hello there how are you....  ");
        assertEquals("Hello there how are you.... ", result);
    }

Call the runner in the junit test method.

@Test
    public void benchmark() throws Exception {
        Options opt = new OptionsBuilder()
                .include(this.getClass().getSimpleName())
                .forks(2)
                .measurementIterations(1)
                .warmupIterations(1)
                .build();
        

        new Runner(opt).run();
    }

Find the below working application.

 

JunitTestDemo.java

package com.sample.app;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.sample.app.util.StringUtil;


public class JunitTestDemo {

    @Benchmark
    public void test() {
        String result = StringUtil.replaceMultipleSpaces("Hello there how are you....  ");
        assertEquals("Hello there how are you.... ", result);
    }

    @Test
    public void benchmark() throws Exception {
        Options opt = new OptionsBuilder()
                .include(this.getClass().getSimpleName())
                .forks(2)
                .measurementIterations(1)
                .warmupIterations(1)
                .build();
        

        new Runner(opt).run();
    }

}

Run the class ‘JunitTestDemo’ as junit test, you will get the jmh messages in system console.

# 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: -ea -Dfile.encoding=UTF-8 -XX:+ShowCodeDetailsInExceptionMessages
# Blackhole mode: full + dont-inline hint (auto-detected, use -Djmh.blackhole.autoDetect=false to disable)
# Warmup: 1 iterations, 10 s each
# Measurement: 1 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.sample.app.JunitTestDemo.test

# Run progress: 0.00% complete, ETA 00:00:40
# Fork: 1 of 2
# Warmup Iteration   1: 2162584.129 ops/s
Iteration   1: 2341717.486 ops/s

# Run progress: 50.00% complete, ETA 00:00:20
# Fork: 2 of 2
# Warmup Iteration   1: 2077664.321 ops/s
Iteration   1: 2353474.007 ops/s


Result "com.sample.app.JunitTestDemo.test":
  2347595.747 ops/s


# Run complete. Total time: 00:00:40

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
JunitTestDemo.test  thrpt    2  2347595.747          ops/s




 

Previous                                                 Next                                                 Home

No comments:

Post a Comment