使用JMH做Java微基准测试(四)默认状态测试

本文是关于使用Java Microbenchmark Harness (JMH)进行微基准测试的系列文章第四篇,主要探讨了在默认状态下进行测试,并展示了如何在类级别应用线程独享模式,结果显示这种方法在吞吐量上的表现远超方法级别的设置。
摘要由CSDN通过智能技术生成

 

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

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

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

4、使用JMH做Java微基准测试(三)测试参数状态State

 

类上使用线程独享模式:


/** 
 * 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_04_DefaultState.java
* @author: tompai
* @email:liinux@qq.com
* @createTime: 2020年1月3日 下午9:19:14
* @history:
* @version: v1.0
*/
//该状态为每个线程独享。
@State(Scope.Thread)
public class JMHSample_04_DefaultState {

    double x = Math.PI;

    @Benchmark
    public void measure() {
        x++;
    }

    /*
     * ============================== HOW TO RUN THIS TEST: ====================================
     *
     * You can see the benchmark runs as usual.
     *
     * You can run this test:
     *
     * a) Via the command line:
     *    $ mvn clean install
     *    $ java -jar target/benchmarks.jar JMHSample_04 -f 1
     *    (we requested 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_04_DefaultState.class.getSimpleName())
                .output("./Benchmark.log")
                .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: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.tompai.test.JMHSample_04_DefaultState.measure

# Run progress: 0.00% complete, ETA 00:01:40
# Fork: 1 of 1
# Warmup Iteration   1: 346179913.321 ops/s
# Warmup Iteration   2: 350738022.766 ops/s
# Warmup Iteration   3: 336581741.400 ops/s
# Warmup Iteration   4: 327158358.578 ops/s
# Warmup Iteration   5: 360373160.588 ops/s
Iteration   1: 332821718.835 ops/s
Iteration   2: 321837561.505 ops/s
Iteration   3: 362746660.895 ops/s
Iteration   4: 364612501.545 ops/s
Iteration   5: 276559642.849 ops/s


Result "com.tompai.test.JMHSample_04_DefaultState.measure":
  331715617.126 ±(99.9%) 138651411.583 ops/s [Average]
  (min, avg, max) = (276559642.849, 331715617.126, 364612501.545), stdev = 36007331.282
  CI (99.9%): [193064205.543, 470367028.709] (assumes normal distribution)


# Run complete. Total time: 00:01:41

REMEMBER: 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_04_DefaultState.measure  thrpt    5  331715617.126 ± 138651411.583  ops/s

 

较方法上使用吞吐量要高很多!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值