Java Microbenchmark Harness (JMH)

Java Microbenchmark Harness (JMH)

http://albertnetymk.github.io/2017/12/27/jmh/(此文档是安装过程)
https://github.com/openjdk/jmh (最新的jmh,上面的jmh似乎跑不起来)
官方推荐的测试java的函数性能的工具
jmh的使用guide:https://blog.csdn.net/lxbjkben/article/details/79410740

dependency

<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>
    </dependencies>

例子:


public class MyBenchmark {



    @State(Scope.Thread)
    public static class MyState {
        Vector vector = new Vector();
        ArrayList<Integer> arrayList = new ArrayList<>();
        Integer[] arr = new Integer[10000];
        @Setup
        public void init() {
            for (int i = 0; i < 10000; i++) {
                vector.add(i);
                arrayList.add(i);
                arr[i] = i;
            }
        }
    }


    @Benchmark
    public void testArrayList(MyState state){
        int i = state.arrayList.get(0);
        i = state.arrayList.get(1000);
        i = state.arrayList.get(8888);
    }
    @Benchmark
    public void testArr(MyState state){
        int i = state.arr[0];
        i = state.arr[1000];
        i = state.arr[8888];

    }



    public static void main(String[] args) throws RunnerException {
        MyBenchmark mytest = new MyBenchmark();

        Options opt = new OptionsBuilder()
                .include(MyBenchmark.class.getSimpleName())
                .warmupIterations(1)
                .measurementIterations(1)
                .forks(1)
                .build();

        new Runner(opt).run();
    }
}



执行参数(生成机器执行汇编)

-Xcomp -XX:+UnlockDiagnosticVMOptions -XX:CompileCommand="print,*.mulTest" --add-modules jdk.incubator.vector

output:

# JMH version: 1.33
# VM version: JDK xxxx, OpenJDK 64-Bit Server VM, 17-xxxx+3-167
# VM invoker: /home/xxx/jdk/jdk-17/bin/java
# VM options: --add-modules=jdk.incubator.vector -javaagent:/home/xxxx/idea/idea-IC-/lib/idea_rt.jar=39765:/home/xxxx/idea/idea-IC-bin -Dfile.encoding=UTF-8
# Blackhole mode: full + dont-inline hint (default, use -Djmh.blackhole.autoDetect=true to auto-detect)
# Warmup: 1 iterations, 10 s each
# Measurement: 1 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: org.sample.MyBenchmark.testArr

# Run progress: 0.00% complete, ETA 00:00:40
# Fork: 1 of 1
WARNING: Using incubator modules: jdk.incubator.vector
# Warmup Iteration   1: 587528394.545 ops/s
Iteration   1: 588012492.451 ops/s


Result "org.sample.MyBenchmark.testArr":
  588012492.451 ops/s


# JMH version: 1.33
# VM version: JDK 17-xxxx, OpenJDK 64-Bit Server VM, 17-xxxx+3-167
# VM invoker: /home/xxxx/jdk/jdk-17/bin/java
# VM options: --add-modules=jdk.incubator.vector -javaagent:/home/xxxxx/idea/idea-IC-lib/idea_rt.jar=39765:/home/ziyi/idea/idea-IC-/bin -Dfile.encoding=UTF-8
# Blackhole mode: full + dont-inline hint (default, use -Djmh.blackhole.autoDetect=true to auto-detect)
# Warmup: 1 iterations, 10 s each
# Measurement: 1 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: org.sample.MyBenchmark.testArrayList

# Run progress: 50.00% complete, ETA 00:00:20
# Fork: 1 of 1
WARNING: Using incubator modules: jdk.incubator.vector
# Warmup Iteration   1: 338121260.976 ops/s
Iteration   1: 339601119.490 ops/s


Result "org.sample.MyBenchmark.testArrayList":
  339601119.490 ops/s


# Run complete. Total time: 00:00: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
MyBenchmark.testArr        thrpt       588012492.451          ops/s
MyBenchmark.testArrayList  thrpt       339601119.490          ops/s

我们发现直接使用数组比使用ArrayList的ops更高,说明当数组长度已知时直接使用数组效率更高。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值