The use of Java micro benchmark framework JMH

Keywords: Maven Java github Apache

After checking the usage of JMH, many blogs have to be very complicated. This step is quite simple for me
Using Intellij IDEA2018.2 as the integrated development environment, JMH version is 1.21
Note that when building a project, you must create a maven project and directly import the two jar s, jmh core and jmh generator annprocess, which are useless. The complete dependence is as follows:**

<dependencies>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>1.21</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>1.21</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

This alone is not enough to run. After enabling annotation processing in setting - > build, execution deployment - > compiler - > annotation processors, you can run JMH tests

Set up a project as follows to run

The code is as follows:

package first;

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;

import java.util.HashMap;
import java.util.Map;

public class Hello {
    @Benchmark
    public void testMethod() {
        Map<String, String> map = new HashMap<>();
        map.put("a", "b");
    }

    public static void main(String[] args) {
        final Options build = new OptionsBuilder().include(Hello.class.getSimpleName()).forks(1).build();
        try {
            new Runner(build).run();
        } catch (RunnerException e) {
            e.printStackTrace();
        }
    }
}

Welcome to my Github: https://github.com/Vant1032

Posted by Wolphie on Sat, 04 Jan 2020 09:27:05 -0800