6.性能与压力测试

课件

性能优化 - Nginx 动静分离

  • 首先,把商品服务中静态文件夹 index 放到 nginx 下 /mydata/nginx/html/static目录;
  • 给模板中所有静态资源的请求路径前都加上 /static;
  • 修改 Nginx 配置文件 /mydata/nginx/conf/conf.d/gulimall.conf
# /static/ 下所有的请求都转给 nginx
location /static/ {
	root /user/share/nginx/html;
}

性能优化 - 优化查询三级分类数据

@Override
public Map<String, List<Catalogs2Vo>> getCatalogJson() {
    System.out.println("查询了数据库");
    // 性能优化:将数据库的多次查询变为一次
    List<CategoryEntity> selectList = this.baseMapper.selectList(null);

    //1、查出所有分类
    //1、1)查出所有一级分类
    List<CategoryEntity> level1Categories = getParentCid(selectList, 0L);

    //封装数据
    Map<String, List<Catalogs2Vo>> parentCid = level1Categories.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
        //1、每一个的一级分类,查到这个一级分类的二级分类
        List<CategoryEntity> categoryEntities = getParentCid(selectList, v.getCatId());

        //2、封装上面的结果
        List<Catalogs2Vo> catalogs2Vos = null;
        if (categoryEntities != null) {
            catalogs2Vos = categoryEntities.stream().map(l2 -> {
                Catalogs2Vo catalogs2Vo = new Catalogs2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName().toString());

                //1、找当前二级分类的三级分类封装成vo
                List<CategoryEntity> level3Catelog = getParentCid(selectList, l2.getCatId());

                if (level3Catelog != null) {
                    List<Catalogs2Vo.Category3Vo> category3Vos = level3Catelog.stream().map(l3 -> {
                        //2、封装成指定格式
                        Catalogs2Vo.Category3Vo category3Vo = new Catalogs2Vo.Category3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());

                        return category3Vo;
                    }).collect(Collectors.toList());
                    catalogs2Vo.setCatalog3List(category3Vos);
                }

                return catalogs2Vo;
            }).collect(Collectors.toList());
        }
        return catalogs2Vos;
    }));
    return parentCid;
}

private List<CategoryEntity> getParentCid(List<CategoryEntity> selectList, Long parentCid) {
    return selectList.stream().filter(item -> item.getParentCid().equals(parentCid)).collect(Collectors.toList());
}

性能优化 - 调整内存

对于系统的性能优化方面,我们还可以调整 Java 程序运行的堆内存空间来减少 Full GC 和 Minor GC 从而减少运行时间。

Full gc 最会影响性能,根据代码问题,避免 full gc 频率。可以适当调大年轻代容量,让大对象可以在年轻代触发 young gc,调整大对象在年轻代的回收频次,尽可能保证大对象在年轻代回收,减小老年代缩短回收时间;

posted @ 2022-03-17 10:04  随遇而安==  阅读(37)  评论(0编辑  收藏  举报