我有一个 Maven 项目,其相关的 pom.xml
为
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>0.1</version>
</dependency>
我按照官方的例子here .
我的 App.java
类如下所示:
public class App
{
@GenerateMicroBenchmark
public void run()
{
System.out.println("Hello World");
}
public static void main( String[] args ) throws RunnerException {
Options opt = new OptionsBuilder()
.include(".*" + App.class.getSimpleName() + ".*")
.forks(1)
.build();
new Runner(opt).run();
}
}
当我通过 Eclipse 运行它时,仍然收到以下错误。
没有匹配的基准。正则表达式拼写错误?使用 -v 进行详细输出。
最佳答案
问题是,JMH 默认情况下会查找目录 /META-INF/MicroBenchmarks
来查找要运行的基准测试,但 Maven 不会将类编译到该目录。要使其在Eclipse中工作,请引用this answer .
但是,我建议您直接在命令行上运行基准测试(Eclipse 可能会增加一些开销)。
我注意到您正在使用 0.1 版,这是 JMH 的第一个版本。最好更新到最新版本。开始使用 JMH 的最佳方法是使用原型(prototype) jmh-java-benchmark-archetype
生成项目:
$ mvn archetype:generate \
-DinteractiveMode=false \
-DarchetypeGroupId=org.openjdk.jmh \
-DarchetypeArtifactId=jmh-java-benchmark-archetype \
-DgroupId=org.sample \
-DartifactId=jmh-test \
-Dversion=1.0
这将从头开始生成一个 Maven 项目,使用最新的 JMH 版本,预先配置,而无需担心打包。然后,您可以将这个新项目作为现有 Maven 项目导入到 Eclipse 中,并开始编写基准测试代码。
之后,您只需运行:
$ cd jmh-test/
$ mvn clean install
$ java -jar target/benchmarks.jar
为了让您快速入门,这里有一个示例基准(不执行任何操作...)。
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.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(3)
@State(Scope.Benchmark)
public class StreamTest {
@Benchmark
public void benchmark() {
// code here
}
}
关于java - Eclipse、Maven 和 JMH : No matching benchmarks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597268/