項目引入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