Wednesday, 3 August 2022

Jmh: be cautious with dead code

What is dead code?

Dead code is the one that never executed in run time.

 

DeadCodeEx.java

package com.sample.app;

public class DeadCodeEx {
    public static void main(String[] args) {
        final boolean b = true;
        
        if(b) {
            System.out.println("b is set to true");
        }else {
            System.out.println("b is set to false");
        }
    }
}

 

Notice above code snippet, the code is else block will never get executed, this is an example of dead code.

 

Now  a days compilers are smart enough to identify and delete the dead code. If the eliminated part was our benchmarked code, then we will get wrong benchmarking results.

 

Let me explain it with an example.

 

DeadcodeEliminationDemo.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.annotations.Scope;
import org.openjdk.jmh.annotations.State;
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;

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class DeadcodeEliminationDemo {

    private double fib(int n) {
        if (n <= 1) {
            return n;
        }
        
        int fib = 1;
        int prevFib = 1;

        for (int i = 2; i < n; i++) {
            int temp = fib;
            fib += prevFib;
            prevFib = temp;
        }
        return fib;

    }

    @Benchmark
    public void baseline() {
        // do nothing
    }

    @Benchmark
    public void measureWrong() {
        fib(20);
    }

    @Benchmark
    public double measureRight() {
        // Here result is returned to the calling function.
        return fib(20);
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder().include(DeadcodeEliminationDemo.class.getSimpleName()).forks(1)
                .measurementIterations(2).warmupIterations(2).build();

        new Runner(opt).run();
    }

}

When I ran the application, I got below benchmark results.

Benchmark                             Mode  Cnt  Score   Error  Units
DeadcodeEliminationDemo.baseline      avgt    2  0.398          ns/op
DeadcodeEliminationDemo.measureRight  avgt    2  2.398          ns/op
DeadcodeEliminationDemo.measureWrong  avgt    2  0.570          ns/op

Even though both ‘measureWrong’ and ‘measureRight’ methods calling fib(20) internally, measureWrong method took 0.57 nano seconds per operation, where as measureRight  took 2.398 nano seconds per operation.

 


 

 

Why measureWrong taking very less time as compared to measureRight?

As you see the definition of ‘measureWrong’ method, it is not using the result of fib(20) anywhere, so compiler treat this as dead code and eliminate it.


public void measureWrong() {
        fib(20);
}

Be cautious, while measuring the benchmark with these kind of scenarios.

There are two approaches to handle this scenario.

 

Approach 1: Just return the created data.

public void measureWrong() {
        return fib(20);
}

 

Approach 2: Let the Blackhole consume the data.

@Benchmark
public void measureWrong(Blackhole blackhole) {
	blackhole.consume(fib(20));
}

 

 

Previous                                                 Next                                                 Home

Jmh: @Setup, @TearDown fixture methods

@Setup

Setup marks the fixture method to be run before the benchmark.

 

@TearDown

TearDown marks the fixture method to be run after the benchmark

 

Since fixture methods (methods annotated with @Setup, @TearDown) manage the State lifecycles, these can only be declared in State classes.

Fixtures have different Levels to control when they are about to run.

 

Example

@State(Scope.Benchmark)
public static class MyState {

    @Setup(Level.Trial)
    public void setupTrailLevel() {
        System.out.println("setupTrailLevel called");
    }
    
    @Setup(Level.Iteration)
    public void setupIterationLevel() {
        System.out.println("setupIterationLevel called");
    }
    
    @Setup(Level.Invocation)
    public void setupInvocationLevel() {
        System.out.println("setupInvocationLevel called");
    }

    @TearDown(Level.Trial)
    public void tearDownTrailLevel() {
        System.out.println("tearDownTrailLevel called");
    }
    
    @TearDown(Level.Iteration)
    public void tearDownIterationLevel() {
        System.out.println("tearDownIterationLevel called");
    }
    
    @TearDown(Level.Invocation)
    public void tearDownInvocationLevel() {
        System.out.println("tearDownInvocationLevel called");
    }
}

Notice above snippet, @Setup, @TearDown annotations takes a Level attribute. Possible values of Level are documented in below table.

 

Level

Description

Level.Trial

Annotated method is called before/after each run of the benchmark.

Level.Iteration

Annotated method is called before/after each iteration of the benchmark.

Level.Invocation

Annotated method is called once for each call to the benchmark method.

 

 Find the below working application, observe the output to confirm the same on fixture methods with level attribute.

 


SetupTeardownDemo.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.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
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 SetupTeardownDemo {
    
    @State(Scope.Benchmark)
    public static class MyState {

        @Setup(Level.Trial)
        public void setupTrailLevel() {
            System.out.println("\nsetupTrailLevel called\n");
        }
        
        @Setup(Level.Iteration)
        public void setupIterationLevel() {
            System.out.println("\n\tsetupIterationLevel called");
        }
        
        @Setup(Level.Invocation)
        public void setupInvocationLevel() {
            System.out.println("\n\t\tsetupInvocationLevel called");
        }

        @TearDown(Level.Trial)
        public void tearDownTrailLevel() {
            System.out.println("\ntearDownTrailLevel called\n");
        }
        
        @TearDown(Level.Iteration)
        public void tearDownIterationLevel() {
            System.out.println("\ttearDownIterationLevel called");
        }
        
        @TearDown(Level.Invocation)
        public void tearDownInvocationLevel() {
            System.out.println("\t\ttearDownInvocationLevel called");
        }
    }
    
    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    public void test(MyState myState) throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("\t\ttest method executed");
    }
    
    
    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(SetupTeardownDemo.class.getSimpleName())
                .forks(2)
                .measurementIterations(1)
                .warmupIterations(1)
                .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: 1 iterations, 10 s each
# Measurement: 1 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.sample.app.SetupTeardownDemo.test

# Run progress: 0.00% complete, ETA 00:00:40
# Fork: 1 of 2
# Warmup Iteration   1: 
setupTrailLevel called


    setupIterationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called
    tearDownIterationLevel called
2.002 s/op
Iteration   1: 
    setupIterationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called
    tearDownIterationLevel called

tearDownTrailLevel called

2.003 s/op

# Run progress: 50.00% complete, ETA 00:00:25
# Fork: 2 of 2
# Warmup Iteration   1: 
setupTrailLevel called


    setupIterationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called
    tearDownIterationLevel called
2.002 s/op
Iteration   1: 
    setupIterationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called

        setupInvocationLevel called
        test method executed
        tearDownInvocationLevel called
    tearDownIterationLevel called

tearDownTrailLevel called

2.004 s/op


Result "com.sample.app.SetupTeardownDemo.test":
  2.003 s/op


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

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
SetupTeardownDemo.test  avgt    2  2.003           s/op

 


  

Previous                                                 Next                                                 Home

Tuesday, 2 August 2022

JMH: State objects

State objects encapsulate the state on which benchmark is working on.

 

For example, I want to know how many times a method gets executed while performing the benchmark. To do this, I can maintain a counter that can be shared across all the benchmark execution.

 

State variables are declared in special state classes, and an instance of that state class can then be provided as a parameter to the benchmark method.

 

Step 1: Define a State class.

@State(Scope.Benchmark)
public static class BenchmarkState {
    private AtomicLong totalMethodExecutionCount;

    @Setup(Level.Trial)
    public void doSetup() {
        totalMethodExecutionCount = new AtomicLong(0);
        System.out.println("Do Setup");
    }

    @TearDown(Level.Trial)
    public void doTearDown() {
        System.out.println("Benchmark Method executed " + totalMethodExecutionCount.longValue() + " times");
    }
}

 

Step 2: Pass the BenchmarkState variable as an argument to benchmark method.

@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void measureShared1(BenchmarkState state) throws InterruptedException {
    TimeUnit.MILLISECONDS.sleep(100);
    state.totalMethodExecutionCount.getAndIncrement();
}

 

With this logic, I can check how many times the method ‘measureShared1’ is called by benchmarking.

 

Find the below working application.

 

BenchmarkStateDemo.java

package com.sample.app;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
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 BenchmarkStateDemo {

    @State(Scope.Benchmark)
    public static class BenchmarkState {
        private AtomicLong totalMethodExecutionCount;
        private String methodName = null;

        @Setup(Level.Trial)
        public void doSetup() {
            totalMethodExecutionCount = new AtomicLong(0);
            System.out.println("Do Setup");
        }

        @TearDown(Level.Trial)
        public void doTearDown() {
            System.out.println("Benchmark Method " + methodName + " executed " + totalMethodExecutionCount.longValue() + " times");
        }
    }

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    public void measureShared1(BenchmarkState state) throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(100);
        state.methodName = "measureShared1";
        state.totalMethodExecutionCount.getAndIncrement();
    }
    
    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    public void measureShared2(BenchmarkState state) throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(200);
        state.methodName = "measureShared2";
        state.totalMethodExecutionCount.getAndIncrement();
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(BenchmarkStateDemo.class.getSimpleName())
                .forks(1)
                .measurementIterations(2)
                .warmupIterations(2).build();

        Runner runner = new Runner(opt);
        runner.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: 2 iterations, 10 s each
# Measurement: 2 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.sample.app.BenchmarkStateDemo.measureShared1

# Run progress: 0.00% complete, ETA 00:01:20
# Fork: 1 of 1
# Warmup Iteration   1: Do Setup
0.103 s/op
# Warmup Iteration   2: 0.103 s/op
Iteration   1: 0.103 s/op
Iteration   2: Benchmark Method measureShared1 executed 392 times
0.103 s/op


Result "com.sample.app.BenchmarkStateDemo.measureShared1":
  0.103 s/op


# 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: 2 iterations, 10 s each
# Measurement: 2 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.sample.app.BenchmarkStateDemo.measureShared2

# Run progress: 50.00% complete, ETA 00:00:45
# Fork: 1 of 1
# Warmup Iteration   1: Do Setup
0.203 s/op
# Warmup Iteration   2: 0.203 s/op
Iteration   1: 0.203 s/op
Iteration   2: Benchmark Method measureShared2 executed 200 times
0.203 s/op


Result "com.sample.app.BenchmarkStateDemo.measureShared2":
  0.203 s/op


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

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
BenchmarkStateDemo.measureShared1  avgt    2  0.103           s/op
BenchmarkStateDemo.measureShared2  avgt    2  0.203           s/op

From the output, you can see the below lines.


Iteration   2: Benchmark Method measureShared1 executed 392 times
Iteration   2: Benchmark Method measureShared2 executed 200 times

 

A state object can have following scopes.

 

a.   Benchmark: Benchmark state scope. All threads running the benchmark share the same state object.

b.   Group: Group state scope. Each thread group running the benchmark will create its own instance of the state object.

c.    Thread: Each thread running the benchmark will create its own instance of the state object.

 

Let’s confirm it by below example.

 


StatesDemo.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.Scope;
import org.openjdk.jmh.annotations.State;
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 StatesDemo {
    
    @State(Scope.Benchmark)
    public static class BenchmarkState {
    }

    @State(Scope.Thread)
    public static class ThreadState {
    }

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    public void benchmarkState(BenchmarkState benchmarkState) throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        String threadName = Thread.currentThread().getName();
        String threadGroupName = Thread.currentThread().getThreadGroup().getName();

        System.out.println("threadName : %s, threadGroupName %s, benchmarkState : %s".formatted(threadName,
                threadGroupName, benchmarkState));
    }

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    public void threadState(ThreadState threadState) throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        String threadName = Thread.currentThread().getName();
        String threadGroupName = Thread.currentThread().getThreadGroup().getName();

        System.out.println("threadName : %s, threadGroupName %s, threadState : %s".formatted(threadName,
                threadGroupName, threadState));
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(StatesDemo.class.getSimpleName())
                .threads(3)
                .threadGroups(2)
                .forks(1)
                .measurementIterations(1)
                .warmupIterations(1)
                .build();
        

        Runner runner = new Runner(opt);
        runner.run();
     
    }
}

 

Run the above application, you will observe following messages in the console.

threadName : com.sample.app.StatesDemo.benchmarkState-jmh-worker-1, threadGroupName main, benchmarkState : com.sample.app.jmh_generated.StatesDemo_BenchmarkState_jmhType@148ccf8e
threadName : com.sample.app.StatesDemo.benchmarkState-jmh-worker-2, threadGroupName main, benchmarkState : com.sample.app.jmh_generated.StatesDemo_BenchmarkState_jmhType@148ccf8e
threadName : com.sample.app.StatesDemo.benchmarkState-jmh-worker-4, threadGroupName main, benchmarkState : com.sample.app.jmh_generated.StatesDemo_BenchmarkState_jmhType@148ccf8e
threadName : com.sample.app.StatesDemo.benchmarkState-jmh-worker-2, threadGroupName main, benchmarkState : com.sample.app.jmh_generated.StatesDemo_BenchmarkState_jmhType@148ccf8e

 

Same benchmark state object ‘com.sample.app.jmh_generated.StatesDemo_BenchmarkState_jmhType@148ccf8e’ is shared across all the method invocations.

 

threadName : com.sample.app.StatesDemo.threadState-jmh-worker-4, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@148ccf8e
threadName : com.sample.app.StatesDemo.threadState-jmh-worker-3, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@32263bb4
threadName : com.sample.app.StatesDemo.threadState-jmh-worker-2, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@3e078932
threadName : com.sample.app.StatesDemo.threadState-jmh-worker-1, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@261a9ff6
threadName : com.sample.app.StatesDemo.threadState-jmh-worker-4, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@148ccf8e
threadName : com.sample.app.StatesDemo.threadState-jmh-worker-3, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@32263bb4
threadName : com.sample.app.StatesDemo.threadState-jmh-worker-2, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@3e078932
threadName : com.sample.app.StatesDemo.threadState-jmh-worker-1, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@261a9ff6
threadName : com.sample.app.StatesDemo.threadState-jmh-worker-4, threadGroupName main, threadState : com.sample.app.jmh_generated.StatesDemo_ThreadState_jmhType@148ccf8e

 

Each worker thread maintains a separate ThreadState object.

 

Previous                                                 Next                                                 Home

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