perf(nanjing): 优化条码处理性能

- 调整最优线程数下限从 4 个增加到 8 个
- 提高并行窗口大小上限从 4 个批次增加到 24 个批次
- 将各个阶段的超时时间从 300 秒增加到 600 秒,
master
zch 1 day ago
parent bf0c287fe7
commit bf2c55da5d

@ -131,7 +131,7 @@ public class SelectProRpListController extends BaseController
// 计算最优线程数
int CPU_CORES = Runtime.getRuntime().availableProcessors();
int OPTIMAL_THREADS = Math.min(
Math.max(CPU_CORES / 4, 4), // CPU核心数的1/4但不少于4个线程
Math.max(CPU_CORES / 4, 8), // CPU核心数的1/4但不少于8个线程
12 // 最大不超过12个线程
);
// 线程池配置
@ -151,7 +151,7 @@ public class SelectProRpListController extends BaseController
int totalBatches = (barcodes.size() + BATCH_SIZE - 1) / BATCH_SIZE; // 向上取整计算总批次数
// 按顺序处理批次,但使用较小的并行窗口
int PARALLEL_WINDOW = Math.min(4, OPTIMAL_THREADS); // 并行窗口大小,最多4个批次同时处理
int PARALLEL_WINDOW = Math.min(24, OPTIMAL_THREADS); // 并行窗口大小,最多12个批次同时处理
for (int windowStart = 0; windowStart < totalBatches; windowStart += PARALLEL_WINDOW) {
List<CompletableFuture<List<ProRpListPrint>>> windowFutures = new ArrayList<>();
int windowEnd = Math.min(windowStart + PARALLEL_WINDOW, totalBatches);
@ -181,12 +181,12 @@ public class SelectProRpListController extends BaseController
try {
CompletableFuture<Void> windowComplete = CompletableFuture
.allOf(windowFutures.toArray(new CompletableFuture[0]));
windowComplete.get(300, TimeUnit.SECONDS);
windowComplete.get(600, TimeUnit.SECONDS);
// 按提交顺序收集当前窗口的结果
for (CompletableFuture<List<ProRpListPrint>> future : windowFutures) {
try {
List<ProRpListPrint> batchResult = future.get(300, TimeUnit.SECONDS);
List<ProRpListPrint> batchResult = future.get(600, TimeUnit.SECONDS);
printInfo.addAll(batchResult);
logger.info("当前处理进度: {}/{}",
Math.min((windowStart + windowFutures.indexOf(future) + 1) * BATCH_SIZE, barcodes.size()),

Loading…
Cancel
Save