Tuesday 2 August 2022

JMH: Specify the time unit to report the results

@OutputTimeUnit annotation used to customize the timeunit to report the results.

 

Example

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void measureInSeconds() {
    sleepNMilliSeconds(500);
}

 

Above snippet measure the throughput in seconds.

 

Find the below working application.


 

OutputTimeUnitDemo.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
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 OutputTimeUnitDemo {
    private void sleepNMilliSeconds(int n) {
        try {
            TimeUnit.MILLISECONDS.sleep(n);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    @OutputTimeUnit(TimeUnit.SECONDS)
    public void measureInSeconds() {
        sleepNMilliSeconds(500);
    }
    
    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    @OutputTimeUnit(TimeUnit.MINUTES)
    public void measureInMinutes() {
        sleepNMilliSeconds(500);
    }
    
    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(OutputTimeUnitDemo.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.OutputTimeUnitDemo.measureInMinutes

# Run progress: 0.00% complete, ETA 00:02:20
# Fork: 1 of 1
# Warmup Iteration   1: 119.419 ops/min
# Warmup Iteration   2: 119.426 ops/min
# Warmup Iteration   3: 119.266 ops/min
Iteration   1: 119.354 ops/min
Iteration   2: 119.373 ops/min
Iteration   3: 119.299 ops/min
Iteration   4: 119.423 ops/min


Result "com.sample.app.OutputTimeUnitDemo.measureInMinutes":
  119.362 ±(99.9%) 0.332 ops/min [Average]
  (min, avg, max) = (119.299, 119.362, 119.423), stdev = 0.051
  CI (99.9%): [119.030, 119.694] (assumes normal distribution)


# 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.OutputTimeUnitDemo.measureInSeconds

# Run progress: 50.00% complete, ETA 00:01:10
# Fork: 1 of 1
# Warmup Iteration   1: 1.988 ops/s
# Warmup Iteration   2: 1.989 ops/s
# Warmup Iteration   3: 1.992 ops/s
Iteration   1: 1.988 ops/s
Iteration   2: 1.989 ops/s
Iteration   3: 1.990 ops/s
Iteration   4: 1.987 ops/s


Result "com.sample.app.OutputTimeUnitDemo.measureInSeconds":
  1.989 ±(99.9%) 0.010 ops/s [Average]
  (min, avg, max) = (1.987, 1.989, 1.990), stdev = 0.002
  CI (99.9%): [1.978, 1.999] (assumes normal distribution)


# Run complete. Total time: 00:02:21

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
OutputTimeUnitDemo.measureInMinutes  thrpt    4  119.362 ± 0.332  ops/min
OutputTimeUnitDemo.measureInSeconds  thrpt    4    1.989 ± 0.010    ops/s

 

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment