我编写了一个基准测试并放入 src/test 文件夹中。代码:
public class BenchMarkTest {
@State(Scope.Benchmark)
public static class BenchmarkState {
...
}
@Benchmark
public void singleThread(BenchmarkState state) {
...
}
@Benchmark
public void executorService(BenchmarkState state) {
...
}
@Benchmark
public void forkJointPool(BenchmarkState state) {
...
}
@Test
public void launchBenchmark() throws Exception {
Options opt = new OptionsBuilder()
.include(this.getClass().getName() + ".*")
.mode(Mode.AverageTime)
.timeUnit(TimeUnit.MICROSECONDS)
.warmupTime(TimeValue.seconds(1))
.warmupIterations(1)
.measurementTime(TimeValue.seconds(1))
.measurementIterations(1)
.threads(3)
.forks(1)
.shouldFailOnError(true)
.build();
new Runner(opt).run();
}
}
所以当我通过Intellij IDEA运行配置调用launchBenchmark时它运行正常。 但是当我尝试在“mvn clean”之后运行“mvn test”时,它失败并出现错误: 错误:无法找到资源:/META-INF/BenchmarkList
解决方案是添加特定路径:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>${java.release}</release>
<parameters>true</parameters>
<proc>full</proc>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<!--add this path-->
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.35</version>
</path>
<!---->
</annotationProcessorPaths>
</configuration>
</plugin>