• jmh测试实践(针对不同准备数据测试)


    通过不同准备数据进行测试

    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    @BenchmarkMode(Mode.AverageTime)
    @State(Scope.Benchmark)
    public class ForTests {
        @Param({ "1000", "2000", "3000", "4000" })
        public int size;
        public final static List<GoodsInfo> GOODS_INFOS_1000 = new ArrayList<>();
        public final static List<GoodsInfo> GOODS_INFOS_2000 = new ArrayList<>();
        public final static List<GoodsInfo> GOODS_INFOS_3000 = new ArrayList<>();
        public final static List<GoodsInfo> GOODS_INFOS_4000 = new ArrayList<>();
    
        static {
            for (int i = 0; i < 1000; i++) {
                GoodsInfo goodsInfo = new GoodsInfo();
                goodsInfo.setCatIds(Arrays.asList(1, 2, 3, 0));
                GOODS_INFOS_1000.add(goodsInfo);
            }
            for (int i = 0; i < 2000; i++) {
                GoodsInfo goodsInfo = new GoodsInfo();
                goodsInfo.setCatIds(Arrays.asList(1, 2, 3, 0));
                GOODS_INFOS_2000.add(goodsInfo);
            }
    
            for (int i = 0; i < 3000; i++) {
                GoodsInfo goodsInfo = new GoodsInfo();
                goodsInfo.setCatIds(Arrays.asList(1, 2, 3, 0));
                GOODS_INFOS_3000.add(goodsInfo);
            }
            for (int i = 0; i < 4000; i++) {
                GoodsInfo goodsInfo = new GoodsInfo();
                goodsInfo.setCatIds(Arrays.asList(1, 2, 3, 0));
                GOODS_INFOS_4000.add(goodsInfo);
            }
        }
    
        public static void main(String[] args) throws RunnerException {
            Options opt = new OptionsBuilder().include(ForTests.class.getSimpleName()).warmupIterations(1).measurementIterations(1).forks(1).build();
            new Runner(opt).run();
        }
    
        @Benchmark
        public void testOrigin() {
            test(size, this::origin);
        }
        @Benchmark
        public void testOptimize() {
            test(size, this::optimize);
        }
        private void test(int size, Function<GoodsInfo, AlgoGoods> function) {
            if (size == 1000) {
                GOODS_INFOS_1000.forEach(goodsInfo -> {
                    function.apply(goodsInfo);
                });
            }
    
            if (size == 2000) {
                GOODS_INFOS_2000.forEach(goodsInfo -> {
                    function.apply(goodsInfo);
                });
            }
            if (size == 3000) {
                GOODS_INFOS_3000.forEach(goodsInfo -> {
                    function.apply(goodsInfo);
                });
            }
            if (size == 4000) {
                GOODS_INFOS_4000.forEach(goodsInfo -> {
                    function.apply(goodsInfo);
                });
            }
        }
    
        public AlgoGoods origin(GoodsInfo goodsInfo) {
            AlgoGoods algoGoods = new AlgoGoods();
            int catId = 0;
            for (int i = goodsInfo.getCatIds().size() - 1; i >= 0; --i) {
                if (goodsInfo.getCatIds().get(i) > 0) {
                    catId = goodsInfo.getCatIds().get(i);
                    break;
                }
            }
            algoGoods.setCatId(catId);
            algoGoods.setCatId1(goodsInfo.getCatIds().get(0));
            algoGoods.setCatId2(goodsInfo.getCatIds().get(1));
            algoGoods.setCatId3(goodsInfo.getCatIds().get(2));
            algoGoods.setCatId4(goodsInfo.getCatIds().get(3));
            for (int i = 0; i < goodsInfo.getCatIds().size(); i++) {
                algoGoods.getCatIds().add(i, goodsInfo.getCatIds().get(i));
            }
            return algoGoods;
        }
    
        private AlgoGoods optimize(GoodsInfo goodsInfo) {
            AlgoGoods algoGoods = new AlgoGoods();
            int catId = 0;
            int catIdSize = goodsInfo.getCatIds().size();
            for (int i = 0; i < catIdSize; i++) {
                algoGoods.getCatIds().add(i, goodsInfo.getCatIds().get(i));
                if (goodsInfo.getCatIds().get(i) > 0) {
                    catId = goodsInfo.getCatIds().get(i);
                }
            }
            algoGoods.setCatId(catId);
            algoGoods.setCatId1(goodsInfo.getCatIds().get(0));
            algoGoods.setCatId2(goodsInfo.getCatIds().get(1));
            algoGoods.setCatId3(goodsInfo.getCatIds().get(2));
            algoGoods.setCatId4(goodsInfo.getCatIds().get(3));
            return algoGoods;
        }
    
        @Data
        private static class GoodsInfo {
            List<Integer> catIds;
        }
    
        @Data
        private static class AlgoGoods {
            private int   catId;
            private int   catId1;
            private int   catId2;
            private int   catId3;
            private int   catId4;
            List<Integer> catIds = new ArrayList<>();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
  • 相关阅读:
    JSON结构示例
    ctfshow—web—一切看起来都那么合情合理
    归并(merge)排序
    原子搜索算法改进的深度极限学习机DELM的分类
    你需要知道的webpack高频面试题
    程序设计题 2:双11抢宝计划
    ​在控制终端输入AT命令​
    CF776B Sherlock and his girlfriend 题解
    文档图片阴影去除
    正则表达式
  • 原文地址:https://blog.csdn.net/Primary_wind/article/details/127941953