java 基准测试 格式_JMH java基准测试

Measure, don’t guess!JMH适用场景JMH只适合细粒度的方法测试原理编译时会生成一些测试代码,一般都会继承你的类maven依赖org.openjdk.jmhjmh-core${jmh.version}org.openjdk.jmhjmh-generator-annprocess${jmh.version}providedUTF-81.01.6benchmarks命令行方式:生成...
摘要由CSDN通过智能技术生成

Measure, don’t guess!

JMH适用场景

JMH只适合细粒度的方法测试

原理

编译时会生成一些测试代码,一般都会继承你的类

281de6a3849fa1c0f65572abda78b156.png

77d161522b8a419a05544a0140acd928.png

maven依赖

org.openjdk.jmh

jmh-core

${jmh.version}

org.openjdk.jmh

jmh-generator-annprocess

${jmh.version}

provided

UTF-8

1.0

1.6

benchmarks

命令行方式:

生成测试项目

mvn archetype:generate \-DinteractiveMode=false\-DarchetypeGroupId=org.openjdk.jmh \-DarchetypeArtifactId=jmh-java-benchmark-archetype \-DgroupId=org.sample \-DartifactId=test \-Dversion=1.0

打包

$ cd test/$ mvn cleaninstall

执行,

java -jar target/benchmarks.jar

java -jar target/benchmarks.jar -h //查看帮助信息

插件方式

插件会影响执行效果,但最多不会超过 2.2%

A brief research shows that benchmark results are affected, but not that much. The whole research is described in Research results.

Long story short, the maximum means difference observed was 2.2%.

main函数

packageorg.openjdk.jmh.samples;importorg.openjdk.jmh.annotations.Benchmark;importorg.openjdk.jmh.runner.Runner;importorg.openjdk.jmh.runner.RunnerException;importorg.openjdk.jmh.runner.options.Options;importorg.openjdk.jmh.runner.options.OptionsBuilder;public classJMHSample_01_HelloWorld {/** This is our first benchmark method.

*

* JMH works as follows: users annotate the methods with @Benchmark, and

* then JMH produces the generated code to run this particular benchmark as

* reliably as possible. In general one might think about @Benchmark methods

* as the benchmark "payload", the things we want to measure. The

* surrounding infrastructure is provided by the harness itself.

*

* Read the Javadoc for @Benchmark annotation for complete semantics and

* restrictions. At this point we only note that the methods names are

* non-essential, and it only matters that the methods are marked with

* @Benchmark. You can have multiple benchmark methods within the same

* class.

*

* Note: if the benchmark method never finishes, then JMH run never finishes

* as well. If you throw an exception from the method body the JMH run ends

* abruptly for this benchmark and JMH will run the next benchmark down the

* list.

*

* Although this benchmark meas

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值