我无法在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>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();
}
}运行结果:
线程"main“中的异常:错误:无法找到资源:/META/BenchmarkList在org.openjdk.jmh.runner.AbstractResourceReader.getReaders(AbstractResourceReader.java:96) at org.openjdk.jmh.runner.BenchmarkList.find(BenchmarkList.java:104) at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:256) at org.openjdk.jmh.runner.Runner.run(Runner.java:206) at com.test.BTest.main(BTest.java:24)
也许问题是,我是从eclipse运行的。
发布于 2016-06-28 08:49:50
终于发现了。缺少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>发布于 2022-02-27 15:44:10
如果有人使用Gradle,请将插件添加到您的插件块中:
plugins {
id 'java'
id 'me.champeau.jmh' version '0.6.6'
}然后在依赖项块中添加以下所有内容(查看最新版本的JMH 在这里的Maven):
dependencies {
jmh 'org.openjdk.jmh:jmh-core:1.35'
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.35'
// this is the line that solves the missing /META-INF/BenchmarkList error
jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.35'
}然后,只需使用以下方法在Gradle中运行您的基准测试:
./gradlew jmh在IDE中运行
如果您还希望在IDE中而不是通过Gradle运行这些基准测试,则可以执行以下任一操作:
选项1- main方法
只需使用没有额外配置的主方法,IDE就会尊重您的注释配置:
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
@Fork(value = 1)
@Warmup(iterations = 5, timeUnit = TimeUnit.MILLISECONDS, time = 5000)
@Measurement(iterations = 5, timeUnit = TimeUnit.MILLISECONDS, time = 5000)
public class MyBenchmark {
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(MyBenchmark.class.getSimpleName())
.build();
new Runner(options).run();
}
// benchmarks omitted
}选项2-安装JMH插件
如果您使用的是IntelliJ,请从Preferences > Plugins部分安装Java微带基准插件,然后可以完全省略main方法,IntelliJ将在类名旁边提供一个run按钮:

发布于 2020-07-19 18:03:33
pom.xml必须具有以下对(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>之后,转到命令行并运行$mvn干净安装命令
https://stackoverflow.com/questions/38056899
复制相似问题