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<>();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值