1、使用JMH做Java微基准测试:JMH简介

2、使用JMH做Java微基准测试(一)测试入门

3、使用JMH做Java微基准测试(二)Hello2020!

测试参数状态State

@Setup

方法级注解,这个注解的作用就是我们需要在测试之前进行一些准备工作,比如对一些数据的初始化之类的。

@State

当使用@Setup参数的时候,必须在类上加这个参数,不然会提示无法运行。

State 用于声明某个类是一个“状态”,然后接受一个 Scope 参数用来表示该状态的共享范围。 因为很多 benchmark 会需要一些表示状态的类,JMH 允许你把这些类以依赖注入的方式注入到 benchmark 函数里。Scope 主要分为三种。

  1. Thread: 该状态为每个线程独享。
  2. Group: 该状态为同一个组里面所有线程共享。
  3. Benchmark: 该状态在所有线程间共享。测试代码:
    
    /** * Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements.  See the NOTICE file distributed with* this work for additional information regarding copyright ownership.* The ASF licenses this file to You under the Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with* the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.**/
    package com.tompai.test;
    import org.openjdk.jmh.annotations.Benchmark;
    import org.openjdk.jmh.annotations.Scope;
    import org.openjdk.jmh.annotations.State;
    import org.openjdk.jmh.runner.Runner;
    import org.openjdk.jmh.runner.RunnerException;
    import org.openjdk.jmh.runner.options.Options;
    import org.openjdk.jmh.runner.options.OptionsBuilder;
    /**
    * @desc: demo
    * @name: JMHSample_03_States.java
    * @author: tompai
    * @email:liinux@qq.com
    * @createTime: 2020年1月3日 上午12:13:24
    * @history:
    * @version: v1.0
    */public class JMHSample_03_States {/** Most of the time, you need to maintain some state while the benchmark is* running. Since JMH is heavily used to build concurrent benchmarks, we* opted for an explicit notion of state-bearing objects.** Below are two state objects. Their class names are not essential, it* matters they are marked with @State. These objects will be instantiated* on demand, and reused during the entire benchmark trial.** The important property is that state is always instantiated by one of* those benchmark threads which will then have the access to that state.* That means you can initialize the fields as if you do that in worker* threads (ThreadLocals are yours, etc).*///默认状态。实例将分配给运行给定测试的每个线程(共享)。@State(Scope.Benchmark)public static class BenchmarkState {volatile double x = Math.PI;}//运行相同测试的所有线程将不共享实例。可以用来测试状态对象的多线程性能(或者仅标记该范围的基准)。@State(Scope.Thread)public static class ThreadState {volatile double x = Math.PI;}/** Benchmark methods can reference the states, and JMH will inject the* appropriate states while calling these methods. You can have no states at* all, or have only one state, or have multiple states referenced. This* makes building multi-threaded benchmark a breeze.** For this exercise, we have two methods.*/@Benchmarkpublic void measureUnshared(ThreadState state) {// All benchmark threads will call in this method.//// However, since ThreadState is the Scope.Thread, each thread// will have it's own copy of the state, and this benchmark// will measure unshared case.state.x++;}@Benchmarkpublic void measureShared(BenchmarkState state) {// All benchmark threads will call in this method.//// Since BenchmarkState is the Scope.Benchmark, all threads// will share the state instance, and we will end up measuring// shared case.state.x++;}/** ============================== HOW TO RUN THIS TEST: ====================================** You are expected to see the drastic difference in shared and unshared cases,* because you either contend for single memory location, or not. This effect* is more articulated on large machines.** You can run this test:** a) Via the command line:*    $ mvn clean install*    $ java -jar target/benchmarks.jar JMHSample_03 -t 4 -f 1*    (we requested 4 threads, single fork; there are also other options, see -h)** b) Via the Java API:*    (see the JMH homepage for possible caveats when running from IDE:*      http://openjdk.java.net/projects/code-tools/jmh/)*/public static void main(String[] args) throws RunnerException {Options opt = new OptionsBuilder().include(JMHSample_03_States.class.getSimpleName()).output("./Benchmark.log").threads(4).forks(1).build();new Runner(opt).run();}
    }

    测试结果:

    # JMH version: 1.22
    # VM version: JDK 1.8.0_221, Java HotSpot(TM) 64-Bit Server VM, 25.221-b11
    # VM invoker: E:\Java\jdk\jdk1.8.0_221\jre\bin\java.exe
    # VM options: -Dfile.encoding=UTF-8
    # Warmup: 5 iterations, 10 s each
    # Measurement: 5 iterations, 10 s each
    # Timeout: 10 min per iteration
    # Threads: 4 threads, will synchronize iterations
    # Benchmark mode: Throughput, ops/time
    # Benchmark: com.tompai.test.JMHSample_03_States.measureShared# Run progress: 0.00% complete, ETA 00:03:20
    # Fork: 1 of 1
    # Warmup Iteration   1: 67298896.997 ops/s
    # Warmup Iteration   2: 68308485.750 ops/s
    # Warmup Iteration   3: 69882743.315 ops/s
    # Warmup Iteration   4: 69891149.623 ops/s
    # Warmup Iteration   5: 67506135.646 ops/s
    Iteration   1: 67391729.777 ops/s
    Iteration   2: 66734187.576 ops/s
    Iteration   3: 67945688.033 ops/s
    Iteration   4: 70228301.275 ops/s
    Iteration   5: 70063976.328 ops/sResult "com.tompai.test.JMHSample_03_States.measureShared":68472776.598 ±(99.9%) 6113605.121 ops/s [Average](min, avg, max) = (66734187.576, 68472776.598, 70228301.275), stdev = 1587683.835CI (99.9%): [62359171.477, 74586381.719] (assumes normal distribution)# JMH version: 1.22
    # VM version: JDK 1.8.0_221, Java HotSpot(TM) 64-Bit Server VM, 25.221-b11
    # VM invoker: E:\Java\jdk\jdk1.8.0_221\jre\bin\java.exe
    # VM options: -Dfile.encoding=UTF-8
    # Warmup: 5 iterations, 10 s each
    # Measurement: 5 iterations, 10 s each
    # Timeout: 10 min per iteration
    # Threads: 4 threads, will synchronize iterations
    # Benchmark mode: Throughput, ops/time
    # Benchmark: com.tompai.test.JMHSample_03_States.measureUnshared# Run progress: 50.00% complete, ETA 00:01:41
    # Fork: 1 of 1
    # Warmup Iteration   1: 267075988.556 ops/s
    # Warmup Iteration   2: 272251667.492 ops/s
    # Warmup Iteration   3: 274444908.147 ops/s
    # Warmup Iteration   4: 277804069.510 ops/s
    # Warmup Iteration   5: 278807440.060 ops/s
    Iteration   1: 287512303.825 ops/s
    Iteration   2: 283023773.358 ops/s
    Iteration   3: 281720730.780 ops/s
    Iteration   4: 263154214.935 ops/s
    Iteration   5: 287522409.968 ops/sResult "com.tompai.test.JMHSample_03_States.measureUnshared":280586686.573 ±(99.9%) 38850733.584 ops/s [Average](min, avg, max) = (263154214.935, 280586686.573, 287522409.968), stdev = 10089412.136CI (99.9%): [241735952.989, 319437420.158] (assumes normal distribution)# Run complete. Total time: 00:03:22REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
    why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
    experiments, perform baseline and negative tests that provide experimental control, make sure
    the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
    Do not assume the numbers tell you what you want them to tell.Benchmark                             Mode  Cnt          Score          Error  Units
    JMHSample_03_States.measureShared    thrpt    5   68472776.598 ±  6113605.121  ops/s
    JMHSample_03_States.measureUnshared  thrpt    5  280586686.573 ± 38850733.584  ops/s
    

    结论:不共享【@State(Scope.Thread)】情况下吞吐量很高!!!

使用JMH做Java微基准测试(三)测试参数状态State相关推荐

  1. 使用JMH做Java微基准测试

    摘要: # 使用JMH做Java微基准测试 在使用Java编程过程中,我们对于一些代码调用的细节有多种编写方式,但是不确定它们性能时,往往采用重复多次计数的方式来解决.但是随着JVM不断的进化,随着代 ...

  2. JMH(Java Microbenchmark Harness, java微基准测试工具)

    JMH是什么 JMH是Java Microbenchmark Harness(java 微基准测试工具)的简称,是openJDK的一部分. JMH VS JMeter 看来简单比较下他们俩.JMete ...

  3. Java微基准测试框架JMH

    本文转自:https://www.xncoding.com/2018/01/07/java/jmh.html 作者:XiongNeng JMH,即Java Microbenchmark Harness ...

  4. JMH(Java Microbenchmark Harness) Java微基准测试

    官网:OpenJDK: jmh 什么是JMH?微基准测试,他是测的某一个方法的性能到底是好或者不好,换了方法的实现之后他的性能到底好还是不好 创建JMH测试 创建Maven项目,添加依赖,我们需要添加 ...

  5. 在java中使用JMH(Java Microbenchmark Harness)做性能测试

    文章目录 使用JMH做性能测试 BenchmarkMode Fork和Warmup State和Scope 在java中使用JMH(Java Microbenchmark Harness)做性能测试 ...

  6. benchmark java_在java中使用JMH(Java Microbenchmark Harness)做性能测试

    JMH的全称是Java Microbenchmark Harness,是一个open JDK中用来做性能测试的套件.该套件已经被包含在了JDK 12中. 本文将会讲解如何使用JMH来在java中做性能 ...

  7. java能否构成三角形_java中判断三个参数是否能构成三角形的方法

    java中判断三个参数是否能构成三角形的方法 发布时间:2020-06-25 14:02:58 来源:亿速云 阅读:171 作者:Leah 这篇文章将为大家详细讲解有关java中判断三个参数是否能构成 ...

  8. [五]java函数式编程归约reduce概念原理 stream reduce方法详解 reduce三个参数的reduce方法如何使用...

    reduce-归约 看下词典翻译: 好的命名是自解释的 reduce的方法取得就是其中归纳的含义 java8 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...

  9. 如何在Java中编写正确的微基准测试?

    您如何用Java编写(并运行)正确的微基准测试? 我正在寻找一些代码示例和注释,以说明要考虑的各种问题. 示例:基准测试应测量时间/迭代或迭代/时间,为什么? 相关: 秒表基准测试是否可以接受? #1 ...

  10. java基准测试_微基准测试进入Java 9

    java基准测试 我已经几个月没有在这里写文章了,这种例外还会继续. 我计划在明年三月左右恢复写作. 本文末尾的说明. 等待! 不完全是最后,因为您可以向下滚动. 它在文章结尾处. 继续阅读! 三年前 ...

最新文章

  1. pyplot.plot画图turtouil
  2. Android绘制流程
  3. 中国首个工业云平台发布 徐工阿里华为联合打造
  4. postgresql主从备份_基于PG12.2实现主从异步流复制及主从切换教程(下)
  5. ubuntu安装redis 详细步骤
  6. PHP数组——自定义排序
  7. 什么是TypeScript的字符串索引签名
  8. Spring框架中的单例Bean是线程安全的吗?
  9. leetCode-995:K 连续位的最小翻转次数
  10. mysql不可重复读是锁的表吗,Mysql事务,并发问题,锁机制-- 幻读、不可重复读(转)...
  11. SQLite 3.31.0 发布,世界上使用量最大的数据库引擎
  12. 后台产品基本功:RBAC权限后台角色与权限设计
  13. 前端把cookie写在父域里_单点登录的三种实现方式
  14. Python urllib2 设置超时时间并处理超时异常
  15. 计算机网络大写英文缩写汇总(持续更新中……)
  16. 工业面阵相机与源型PLC和漏型PLC外部触发接线
  17. 【多线程与高并发】这可能是最全的多线程面试题了
  18. macd底背离的python_java尝试编写macd,试验顶背离底背离
  19. 【Adobe国际认证中文官网】Adobe中国摄影计划,免费安装 正版激活
  20. Codeforces 897D. Ithea Plays With Chtholly (交互)

热门文章

  1. 客户端(浏览器端)数据存储技术概览
  2. [LeetCode] 47. Permutations II_Medium tag: DFS, backtracking
  3. Hibernate的表之间的关系
  4. 5. 学习集合与常用类
  5. mysqlbackup 还原特定的表
  6. GridView冻结列的实现
  7. 为什么Zappos花钱让新员工走人?
  8. C++ 实现反射机制(转载)
  9. Visual Studio 远程调试设置
  10. 34. Differentiate between inheritance of interface and inheritance ofimplementations