JMH无法找到资源:/ META-INF/BenchmarkList

riv*_*iva 25 java eclipse maven jmh

我无法在eclipse中运行简单的JMH基准测试.Maven依赖:

        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>1.12</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>1.12</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

Java代码:

public class BTest {
    @Benchmark
    public void test() {
        // todo
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                 .include(BTest.class.getSimpleName())
                  .build();

        new Runner(opt).run();
    }
}
Run Code Online (Sandbox Code Playgroud)

运行结果:

线程"main"中的异常java.lang.RuntimeException:错误:无法在org.openjdk的org.openjdk.jmh.runner.AbstractResourceReader.getReaders(AbstractResourceReader.java:96)中找到资源:/ META-INF/BenchmarkList. jmh.runner.BenchmarkList.find(BenchmarkList.java:104)org.openjdk.jmh.runner.Runner.internalRun(Runner.java:256)org.openjdk.jmh.runner.Runner.run(Runner.java: 206)at com.test.BTest.main(BTest.java:24)

也许问题是,我是从eclipse运行的.

谢谢你的帮助.

bla*_*ide 27

如果有人使用 Gradle,请将jmh-gradle-plugin添加到您的插件块中:

plugins {
    id 'java'
    id 'me.champeau.jmh' version '0.6.8'
}
Run Code Online (Sandbox Code Playgroud)

然后在您的依赖项块中添加以下所有内容(在此处检查 Maven 上的JMH 的最新版本):

dependencies {
    jmh 'org.openjdk.jmh:jmh-core:1.36'
    jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.36'

    // this is the line that solves the missing /META-INF/BenchmarkList error
    jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.36'
}
Run Code Online (Sandbox Code Playgroud)

然后只需使用以下命令通过 Gradle 运行基准测试:

plugins {
    id 'java'
    id 'me.champeau.jmh' version '0.6.8'
}
Run Code Online (Sandbox Code Playgroud)

运行您的 IDE

如果您还想在 IDE 中而不是通过 Gradle 运行基准测试,您可以执行以下任一操作:

选项 1 -main方法

只需使用 main 方法,无需额外配置,您的 IDE 将尊重您的注释配置:

dependencies {
    jmh 'org.openjdk.jmh:jmh-core:1.36'
    jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.36'

    // this is the line that solves the missing /META-INF/BenchmarkList error
    jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.36'
}
Run Code Online (Sandbox Code Playgroud)

选项 2 - 安装 JMH 插件

如果您使用 IntelliJ,请从参考资料 部分安装JMH Java Microharness Benchmark PluginPreferences > Plugins,然后您可以main完全省略您的方法,IntelliJ 会在您的类名称旁边提供一个运行按钮:

IntelliJ 中的 JMH 基准测试


riv*_*iva 11

终于找到了.缺少exec-maven-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 Code Online (Sandbox Code Playgroud)


小智 9

pom.xml 必须对 Java Micro-benchmark Harness (JMH) 框架具有以下依赖项和配置

<properties>
    <jmh.version>1.21</jmh.version>
</properties>
<dependencies>
<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-core</artifactId>
    <version>${jmh.version}</version>
</dependency>
<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-generator-annprocess</artifactId>
    <version>${jmh.version}</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>java-jmh</finalName>
<plugins>
    <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>${jmh.version}</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>
</plugins>
Run Code Online (Sandbox Code Playgroud)

之后转到命令行并运行命令 $mvn clean install


rah*_*ani 5

当您的编译器插件尚未处理 JMH 相关注释时,可能会发生这种情况。对我来说,吉尔maven-compiler-plugin更新的回答<annotationProcessorPaths>有效。