JMH记要
1.What's JMH?——OpenJDK提供的微基准测试工具
官网: http://openjdk.java.net/projects/code-tools/jmh/
maven: http://central.maven.org/maven2/org/openjdk/jmh/
2.使用过程中遇到的问题及解决
简单maven工程:
<properties> <jmh.version>1.9.3</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> <scope>provided</scope> </dependency> <dependency> <groupId>org.openjdk.jmh</groupId> <artifactId>jmh-samples</artifactId> <version>${jmh.version}</version> </dependency> </dependencies>
然后运行jmh-samples jar包中的例子:org.openjdk.jmh.samples.JMHSample_01_HelloWorld
出现异常:
- Exception in thread "main" java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList
解决:安装m2e-apt插件(感谢:https://www.iteye.com/blog/szhnet-2323117)
2) 接着运行源码目录下的代码,出现异常:
Exception in thread "main" No benchmarks to run; check the include/exclude regexps.
at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:256)
at org.openjdk.jmh.runner.Runner.run(Runner.java:203)
解决:安装m2e-apt后,需配置:(再次感谢:https://www.iteye.com/blog/szhnet-2323117 ,文章中已经说了,怪自己太不仔细)
勾选后再次运行测试程序,发现会在 target 目录下生成 generated-sources 和 generated-test-sources 目录,其中generated-sources 下会生成一些java类,比如CollectionsBenchmarkTest_jmh.java、CollectionsBenchmarkTest_jmh_B1.java、CollectionsBenchmarkTest_jmh_B2.java、CollectionsBenchmarkTest_jmh_B3.java等,这些类中用到了 javax.annotation.Generated 注解,于是添加依赖:
<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency>
再次运行测试案例,成功!
上一篇: 一个关于Dart错误提示的思考