java微基准测试JMH引入报错RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList
项目引入JMH进行性能测试,完整demo
业务类:
package com.simon.benchmark;
/**
* @Author:huzhiyang
* @Date:2023/3/7 17:03
* @Desc:
*/
public class BizService {
public void tryFor() {
try {
for (int i = 0; i < 5000; i++) {
System.out.println("tryFor" + i);
if (i == 4000) {
throw new RuntimeException("4000异常了");
}
}
} catch (Exception e) {
System.out.println("e:" + e.getMessage());
}
}
public void forTry() {
for (int i = 0; i < 5000; i++) {
try {
System.out.println("forTry" + i);
if (i == 4000) {
throw new RuntimeException("4000异常了");
}
} catch (Exception e) {
System.out.println("e:" + e.getMessage());
}
}
}
}
测试类:
package com.simon.benchmark;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
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.annotations.Warmup;
import org.openjdk.jmh.results.RunResult;
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;
/**
* @Author:huzhiyang
* @Date:2023/3/7 17:02
* @Desc:
*/
@BenchmarkMode(Mode.Throughput) // 吞吐量
@OutputTimeUnit(TimeUnit.MILLISECONDS) // 结果所使用的时间单位
@State(Scope.Thread) // 每个测试线程分配一个实例
@Fork(2) // Fork进行的数目
@Warmup(iterations = 1) // 先预热1轮
@Measurement(iterations = 1) // 进行2轮测试
public class JmhMainApplication {
private BizService bizService;
@Setup(Level.Trial) // 初始化方法,在全部Benchmark运行之前进行
public void init() {
bizService = new BizService();
}
@Benchmark
public void tryFor() {
bizService.tryFor();
}
@Benchmark
public void forTry() {
bizService.forTry();
}
@TearDown(Level.Trial) // 结束方法,在全部Benchmark运行之后进行
public void end() {
System.out.println("Benchmark end");
}
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder().include(JmhMainApplication.class.getSimpleName()).build();
Collection<RunResult> results = new Runner(options).run();
System.out.println(results);
}
}
pom文件
<!--Benchmark 性能测试-->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.19</version>
<scope>provided</scope>
</dependency>
运行之后控制台报错:
Exception in thread "main" java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList
at org.openjdk.jmh.runner.AbstractResourceReader.getReaders(AbstractResourceReader.java:98)
at org.openjdk.jmh.runner.BenchmarkList.find(BenchmarkList.java:122)
at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:256)
at org.openjdk.jmh.runner.Runner.run(Runner.java:206)
at com.simon.benchmark.JmhMainApplication.main(JmhMainApplication.java:63)
根据https://stackoverflow.com/questions/38056899/jmh-unable-to-find-the-resource-meta-inf-benchmarklist
给出的解决方法修改pom文件,记得mvn clean and install之后才会生效!
<!--Benchmark 性能测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.19</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>run-benchmarks</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<classpathScope>test</classpathScope>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>org.openjdk.jmh.Main</argument>
<argument>.*</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
最后执行结果
# Run complete. Total time: 00:00:10
Benchmark Mode Cnt Score Error Units
JmhMainApplication.forTry thrpt 2 0.010 ops/ms
JmhMainApplication.tryFor thrpt 2 0.013 ops/ms