基准测试JMH的demo

整合到idea需要这些

    <dependencies>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>1.23</version>
        </dependency>

        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>1.23</version>
        </dependency>
    </dependencies>

  idea需要安装插件:JMH java Microbenchmark Harness

勾选上这个。

基准测试JMH的demo

 

 

package com.player3;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

 
public class NumsSum{

    static List<Integer> nums = new ArrayList<>();
    static {
        Random r = new Random();
        for (int i = 0; i < 10000; i++) nums.add(1000000 + r.nextInt(1000000));
    }

    public static void foreach() {
        for (Integer num : nums) {
        //    System.out.println(num);
        }
    }

}

  测试代码:直接运行

package com.player3;

import org.openjdk.jmh.annotations.*;

public class PSTest {
    @Benchmark
    @Warmup(iterations = 1,time = 2)
    @Threads(2)
    @Fork(2)
    @BenchmarkMode(Mode.Throughput)
    @Measurement(iterations = 2,time = 2)
    public void testForEach() {
        NumsSum.foreach();
    }
}

  会生成压测报告。根据报告进行代码调整。

上一篇:MASA Blazor多页签组件


下一篇:VUE的简陋实现