You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JMH基准测试执行mvn test时报/META-INF/BenchmarkList找不到错误

解决JMH基准测试Maven执行时找不到/META-INF/BenchmarkList的问题

问题场景

  • 基准测试代码放在src/test/java目录下,代码结构如下:
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,报错:ERROR: Unable to find the resource: /META-INF/BenchmarkList

原因分析

JMH需要在编译阶段生成/META-INF/BenchmarkList文件,用来记录所有标注了@Benchmark注解的方法。IDEA在运行时会自动处理JMH的注解生成该文件,但Maven默认编译流程不会触发这个生成步骤,导致命令行执行时缺失关键资源。

解决方案

1. 引入JMH Maven插件

在项目的pom.xml中添加JMH插件,让Maven在编译阶段自动生成BenchmarkList:

<build>
    <plugins>
        <!-- 基础编译插件,根据JDK版本调整source/target -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <!-- JMH插件,负责生成BenchmarkList和编译基准测试代码 -->
        <plugin>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-maven-plugin</artifactId>
            <version>1.37</version>
            <executions>
                <execution>
                    <id>generate-benchmark-list</id>
                    <goals>
                        <goal>generate-lookup</goal>
                    </goals>
                </execution>
                <execution>
                    <id>compile-benchmarks</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 确认JMH依赖正确

确保pom.xml中已引入JMH核心依赖和注解处理器:

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

3. 验证执行

执行mvn clean test,此时Maven会先运行JMH插件生成BenchmarkList,再编译测试代码,最后执行测试,即可正常运行基准测试。

额外注意点

  • 若仍有问题,可通过mvn clean compile -X查看编译日志,确认JMH插件的generate-lookup和compile目标是否被执行
  • 插件版本可根据JMH官方最新稳定版调整,确保依赖和插件版本一致

内容的提问来源于stack exchange,提问作者onekram

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 17:21:14