JMH( Java Microbenchmark Harness )是一个用于微测的工具,可以类比成JUNIT,后者用于代码正确性,前者用于代码的性能测试。
简单步骤概括:
1.直接打开命令行/console ,通过maven archetype 生成jmh项目
2.然后在项目中编写需要进行微测的方法(可以指定检测输出,循环几次,以及测试数据)
3.maven clean install 打包,然后运行benchmark.jar
//mvn创建命令,此处为了展示方便添加了换行符
//window执行每行末尾可以加上 ^ ,unix执行每行末尾加上 /
mvn archetype:generate
-DinteractiveMode=false
-DarchetypeGroupId=org.openjdk.jmh
-DarchetypeArtifactId=jmh-java-benchmark-archetype
-DarchetypeVersion=1.25
-DgroupId=com.willl
-DartifactId=benchmark-test
-Dversion=1.0
DarchetypeVersion=1.25 //jmh版本
DgroupId=com.willl //改成自己的groupId,如果想要整合成项目中的module,此处记得和父项目相同
DartifactId=benchmark-test //生成的项目名
//cmd中执行注意空格之类的,linux中^换成/
mvn archetype:generate ^
-DinteractiveMode=false ^
-DarchetypeGroupId=org.openjdk.jmh ^
-DarchetypeArtifactId=jmh-java-benchmark-archetype ^
-DarchetypeVersion=1.25 ^
-DgroupId=com.willl ^
-DartifactId=benchmark-test ^
-Dversion=1.0
使用maven提供的默认类作为参考编写,先不用纠结注解的具体使用
package com.willl;
import org.openjdk.jmh.annotations.Benchmark;
@BenchmarkMode(Mode.AverageTime) //标识测试平均时长
@Warmup(iterations = 2) //理解成热身
@Measurement(iterations = 2) //理解成循环几次,然后算平均
public class MyBenchmark {
@Benchmark //标识需要进行微测
public void testMethod() {
//随意代码
for(int i = 0;i<100000;i++){
}
}
}
第三步,打包,运行jar包
mvn clean install
java -jar benchmark.jar
这是我测试(indexof、regex find 以及match)的结果
Benchmark Mode Cnt Score Error Units
* Ext_Test.test_indexof avgt 20 0.218 ± 0.006 ms/op
* Ext_Test.test_regFind avgt 20 59.516 ± 2.447 ms/op
* Ext_Test.test_regMatch avgt 20 140.552 ± 4.284 ms/op
项目结构就是一个simple的maven项目,主要是pom文件
1.引入2个依赖
<dependencies>
<dependency>
<groupId>org.openjdk.jmhgroupId>
<artifactId>jmh-coreartifactId>
<version>${jmh.version}version>
dependency>
<dependency>
<groupId>org.openjdk.jmhgroupId>
<artifactId>jmh-generator-annprocessartifactId>
<version>${jmh.version}version>
<scope>providedscope>
dependency>
dependencies>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<jmh.version>1.25jmh.version>
<javac.target>1.8javac.target>
<uberjar.name>benchmarksuberjar.name>
properties>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-shade-pluginartifactId>
<version>3.2.1version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>shadegoal>
goals>
<configuration>
<finalName>${uberjar.name}finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.MainmainClass>
transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
transformers>
<filters>
<filter>
<artifact>*:*artifact>
<excludes>
<exclude>META-INF/*.SFexclude>
<exclude>META-INF/*.DSAexclude>
<exclude>META-INF/*.RSAexclude>
excludes>
filter>
filters>
configuration>
execution>
executions>
plugin>
知道pom文件以及相关依赖,我们就可以有别的选择。
1.那就是将benchmark整合进项目,作为一个module。虽然在很多教程中都指出这样并不是一个很好的主意,但是存在即合理。2.直接将benchmark的代码耦合进主项目。
第二种想法,直接将上述pom文件拷入就行,这样打包的话就需要将整个项目进行打包。
第一种具体步骤:
第一步,非常重要,在父项目中添加packaging标签
<packaging>pompackaging>
第二步,使用mvn 生成 项目
第三步,将子项目用idea,new->module from existing sources 导入
这样就可以单独在右侧maven中,使用install,clean等命令单独对子项目进行打包,测试。
具体参数配置,在下一篇博客。
在学习jvm中,课程中讲到了JMH使用,但是没办法运行。于是参考了jenkov,以及zzh.特此做一下记录,希望能帮助和我一样的新手,先把项目跑起来。