Commit ccb66a4f by zhouhaibao

perf(supervisor): 优化指标查询性能

- 调整线程池配置,核心线程数从5增加到10,最大线程数从10增加到20
- 修改并行查询批次大小,从每次3个指标调整为每次5个指标
- 仅对需要查询的指标进行存储,避免不必要的内存占用
- 提升驻店销售业绩查询的整体性能表现
1 parent abf33d91
......@@ -21,7 +21,7 @@ public class IndicatorConfig {
* key: 指标编码, value: 字段名称
*/
public static final Map<String, IndicatorInfo> GENERAL_STAFF_INDICATORS = new HashMap<>();
/**
* 问界鸿蒙--延保权益指标配置(员工维度)
* key: 指标编码, value: 字段名称
......@@ -156,7 +156,9 @@ public class IndicatorConfig {
*/
private static void addIndicator(Map<String, IndicatorInfo> map, String code, String fieldName, String displayName,
Level level, boolean needQuery) {
map.put(code, new IndicatorInfo(code, fieldName, displayName, level, needQuery));
if (needQuery) {
map.put(code, new IndicatorInfo(code, fieldName, displayName, level, needQuery));
}
}
/**
......
......@@ -35,8 +35,8 @@ public class IndicatorQueryService {
/**
* 线程池配置
*/
private static final int CORE_POOL_SIZE = 5;
private static final int MAX_POOL_SIZE = 10;
private static final int CORE_POOL_SIZE = 10;
private static final int MAX_POOL_SIZE = 20;
private static final long KEEP_ALIVE_TIME = 60L;
private static final int QUEUE_CAPACITY = 100;
......
......@@ -2224,7 +2224,7 @@ public class SupervisorDashboardService {
// 5. 使用多线程并行查询指标数据(每批3个指标)
stepStartTime = System.currentTimeMillis();
List<DataBusinessDTO> allResults = indicatorQueryService.queryIndicatorsParallel(params, req.getStatDimension(), 3, IndicatorConfig.DIMENSION_STAFF);
List<DataBusinessDTO> allResults = indicatorQueryService.queryIndicatorsParallel(params, req.getStatDimension(), 5, IndicatorConfig.DIMENSION_STAFF);
long indicatorQueryTime = System.currentTimeMillis() - stepStartTime;
log.info("[驻店销售业绩查询] [{}] 步骤5:多线程并行查询指标完成,耗时 {}ms,返回记录数:{}",
requestId, indicatorQueryTime, allResults.size());
......@@ -3807,7 +3807,7 @@ public class SupervisorDashboardService {
// 5. 使用多线程并行查询指标数据(每批3个指标)
stepStartTime = System.currentTimeMillis();
List<DataBusinessDTO> allResults = indicatorQueryService.queryIndicatorsParallel(params, req.getStatDimension(), 3, IndicatorConfig.DIMENSION_STAFF);
List<DataBusinessDTO> allResults = indicatorQueryService.queryIndicatorsParallel(params, req.getStatDimension(), 5, IndicatorConfig.DIMENSION_STAFF);
long indicatorQueryTime = System.currentTimeMillis() - stepStartTime;
log.info("[驻店销售业绩查询] [{}] 步骤5:多线程并行查询指标完成,耗时 {}ms,返回记录数:{}",
requestId, indicatorQueryTime, allResults.size());
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(Top 3)

    1. 逻辑正确性与健壮性风险:配置类中过滤非查询指标可能导致空指针或业务异常

    • 问题描述:在 IndicatorConfig.java 的 addIndicator 方法中,增加了 if (needQuery) 的判断,导致 needQuery 为 false 的指标将不再被放入全局配置的 Map 中。
    • 影响分析:提交信息中提到这是为了“避免不必要的内存占用”,但作为全局静态配置,几十上百个指标对象的内存占用微乎其微(通常在 KB 级别)。然而,这种修改极其危险。如果系统中有其他地方(如前端展示元数据、计算型指标公式、默认值填充等)依赖这个 Map 来获取所有指标的配置信息(IndicatorInfo),此时去 get 一个 needQuery=false 的指标将返回 null,极易引发 NullPointerException 或导致前端显示缺失。
    • 优化建议:除非你有 100% 的把握确定整个系统中没有任何一处代码会访问 needQuery=false 的指标元数据,否则强烈建议撤销此修改。为了微不足道的内存节省而承担核心业务报错的风险是不符合工程实践的。 java // 建议恢复原样: private static void addIndicator(Map<String, IndicatorInfo> map, String code, String fieldName, String displayName, Level level, boolean needQuery) { map.put(code, new IndicatorInfo(code, fieldName, displayName, level, needQuery)); }

    2. 稳定性与性能风险:线程池与并发度翻倍可能导致数据库连接池耗尽

    • 问题描述:IndicatorQueryService.java 中将核心线程数从 5 调至 10,最大线程数从 10 调至 20;同时 SupervisorDashboardService.java 中将并发查询批次从 3 调至 5。
    • 影响分析:增加应用层的并发度确实可以减少单个请求的响应时间,但这会将压力直接转移到下游(通常是数据库)。如果数据库连接池(如 HikariCP)的最大连接数没有同步评估,或者数据库本身负载已经较高,并发度的翻倍可能会导致获取数据库连接超时(Connection Timeout),甚至引发数据库整体性能雪崩。此外,线程池参数硬编码在代码中,不利于生产环境的动态调优。
    • 优化建议:
      1. 确认下游数据库连接池的配置是否能支撑增加后的并发量。
      2. 最佳实践是将线程池的核心参数(CORE_POOL_SIZE, MAX_POOL_SIZE, QUEUE_CAPACITY)提取到配置中心(如 Nacos/Apollo),实现动态可调。

    3. 代码可维护性:注释与实际代码逻辑脱节

    • 问题描述:在 SupervisorDashboardService.java 中,代码已经将批次大小从 3 修改为 5,但上方的注释仍然保留着旧的逻辑说明。
    • 影响分析:这属于典型的“代码更新了但注释没更新”,会给后续接手维护的开发人员带来困扰和误导,降低代码的可读性。
    • 优化建议:同步修改注释,保持代码与注释的一致性。 ```java // 优化前: // 5. 使用多线程并行查询指标数据(每批3个指标) List allResults = indicatorQueryService.queryIndicatorsParallel(params, req.getStatDimension(), 5, IndicatorConfig.DIMENSION_STAFF);

    // 优化后: // 5. 使用多线程并行查询指标数据(每批5个指标) List allResults = indicatorQueryService.queryIndicatorsParallel(params, req.getStatDimension(), 5, IndicatorConfig.DIMENSION_STAFF);

    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(25/40分)**:扣分主要因为 `IndicatorConfig.java` 中的过滤逻辑属于典型的过度优化(微小内存优化换取高崩溃风险),极易破坏原有系统的元数据完整性,引发下游空指针异常。
    - **安全性与潜在风险(30/30分)**:本次变更不涉及 SQL、权限等安全敏感操作,未引入安全漏洞。
    - **最佳实践与可维护性(15/20分)**:扣分因为线程池参数硬编码不便于动态调优,且存在代码修改未同步更新注释(批次数量)的细节问题。
    - **性能与资源利用(3/5分)**:虽然意图是提升查询性能,但并发度的提升缺乏对全局资源(DB连接池)的考量,且“避免内存占用”的优化理由在 Java 静态配置场景下不成立。
    - **提交信息质量(5/5分)**:Commit message 格式规范,清晰地列出了具体的修改点和优化目的,便于追溯。
    
    总分:78分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!