✅P150_性能压测-优化-优化三级分类获取数据
2024/2/13大约 2 分钟
优化三级分类数据获取
业务逻辑的优化,性能损耗的原因:数据库的交互过多
优化方案:将数据库的多次查询变成一次查询
- 先查询所有分类数据
List<CategoryEntity> selectList = this.baseMapper.selectList(null);- 后续在所有分类数据中筛选二级、三级分类数据
实现代码:
cfmall-product/src/main/java/com/gyz/cfmall/product/service/impl/CategoryServiceImpl.java
@Override
public Map<String, List<Catelog2Vo>> getCatalogJson() {
    //将数据库的多次查询变为一次
    List<CategoryEntity> selectList = this.baseMapper.selectList(null);
    //1、查出所有分类
    //1、1)查出所有一级分类
    List<CategoryEntity> level1Categorys = getParent_cid(selectList, 0L);
    //封装数据
    Map<String, List<Catelog2Vo>> parentCid = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
        //1、每一个的一级分类,查到这个一级分类的二级分类
        List<CategoryEntity> categoryEntities = getParent_cid(selectList, v.getCatId());
        //2、封装上面的结果
        List<Catelog2Vo> catelog2Vos = null;
        if (categoryEntities != null) {
            catelog2Vos = categoryEntities.stream().map(l2 -> {
                Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName().toString());
                //1、找当前二级分类的三级分类封装成vo
                List<CategoryEntity> level3Catelog = getParent_cid(selectList, l2.getCatId());
                if (level3Catelog != null) {
                    List<Catelog2Vo.Category3Vo> category3Vos = level3Catelog.stream().map(l3 -> {
                        //2、封装成指定格式
                        Catelog2Vo.Category3Vo category3Vo = new Catelog2Vo.Category3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());
                        return category3Vo;
                    }).collect(Collectors.toList());
                    catelog2Vo.setCatalog3List(category3Vos);
                }
                return catelog2Vo;
            }).collect(Collectors.toList());
        }
        return catelog2Vos;
    }));
    return parentCid;
}private List<CategoryEntity> getParent_cid(List<CategoryEntity> selectList, Long parentCid) {
    List<CategoryEntity> categoryEntities = selectList.stream().filter(item -> 	item.getParentCid().equals(parentCid)).collect(Collectors.toList());
    return categoryEntities;
}Tips:
在IDEA中,如果想把某个代码片段抽取成方法,可以选中代码,鼠标右键,选择
Refactor->Extract Method...,然后定义新方法名称即可!
设置堆内存大小
-Xmx100m

JMeter 压测
线程数:200
循环次数:永远
Ramp-Up Period(in seconds)准备时长:1

HTTP请求
