Java 基准测试

maven依赖

<dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>1.36</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>1.36</version>
        </dependency>

简单使用

package com.chauncy.benchmark;

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;

@State(Scope.Thread)
public class BenchmarkTest1 {

    double pi = Math.PI;
    @Benchmark
    public void measure(){
        pi++;
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(BenchmarkTest1.class.getSimpleName())
                .forks(1)
                .build();
        new Runner(opt).run();
    }
}

执行多个函数

package com.chauncy.benchmark;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
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;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@BenchmarkMode(Mode.All)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS, batchSize = 1)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS, batchSize = 1)
@Fork(5)
public class BenchmarkTest2 {

    private static final int COUNT = 1 ^ 21;
    private byte[] sorted;
    private byte[] unsorted;

    @Setup
    public void setup() {
        sorted = new byte[COUNT];
        unsorted = new byte[COUNT];
        SecureRandom random = new SecureRandom();
        random.nextBytes(sorted);
        random.nextBytes(unsorted);
        Arrays.sort(sorted);
    }

    @Benchmark
    @OperationsPerInvocation(COUNT)
    public void sorted(Blackhole bh1, Blackhole bh2) {
        for (byte b : sorted) {
            if (b > 0) {
                bh1.consume(b);
            } else {
                bh2.consume(b);
            }
        }
    }

    @Benchmark
    @OperationsPerInvocation(COUNT)
    public void unsorted(Blackhole bh1, Blackhole bh2) {
        for (byte b : unsorted) {
            if (b > 0) {
                bh1.consume(b);
            } else {
                bh2.consume(b);
            }
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(".*" + BenchmarkTest2.class.getSimpleName() + ".*")
                .forks(1)
                .build();
        new Runner(opt).run();
    }
}


@BenchmarkMode(Mode.AverageTime) 运行模式

@BenchmarkMode(Mode.Throughput) 吞吐量模式 获取单位时间内的操作数量
@BenchmarkMode(Mode.AverageTime) 平均时间模式 获取每次操作的平均时间
@BenchmarkMode(Mode.SampleTime) 函数运行时间采样模式
@BenchmarkMode(Mode.SingleShotTime)  单次操作时间
@BenchmarkMode(Mode.All) 采用所有的模式

@OutputTimeUnit(TimeUnit.NANOSECONDS) 报告结果时间单位

@Warmup(iterations = 5,time = 1,timeUnit = TimeUnit.SECONDS) 预热参数

iterations 预热的迭代次数
time预热时间
timeUnit 预热时间单位
batchSize 每个基准方法调用次数

@Measurement(iterations = 5,time = 1,timeUnit = TimeUnit.SECONDS,batchSize = 1) 与预热参数类似

@Fork(5) 整体测试次数

@State(Scope.Benchmark) 配置对象作用域

Scope.Benchmark 基准状态范围
Scope.Group 组状态范围
Scope.Thread 线程状态范围

@OperationsPerInvocation(COUNT) 允许与基准进行多个操作的通信

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值