From 253194935ede95d248bb73dc865391add44f0799 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Wed, 28 Aug 2024 11:50:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E5=A4=9A=E4=B8=AA=E8=8A=82=E7=82=B9=E6=97=B6=E5=87=BA?= =?UTF-8?q?=E7=8E=B0time=20sync=20=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/JmeterFileController.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/performance-test/backend/src/main/java/io/metersphere/controller/JmeterFileController.java b/performance-test/backend/src/main/java/io/metersphere/controller/JmeterFileController.java index 4ebf5a9def..c0e3654a6f 100644 --- a/performance-test/backend/src/main/java/io/metersphere/controller/JmeterFileController.java +++ b/performance-test/backend/src/main/java/io/metersphere/controller/JmeterFileController.java @@ -1,11 +1,12 @@ package io.metersphere.controller; -import io.metersphere.commons.utils.WeakConcurrentHashMap; import io.metersphere.controller.handler.annotation.NoResultHolder; import io.metersphere.service.JmeterFileService; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -13,14 +14,15 @@ import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; +import java.util.concurrent.TimeUnit; @RestController @RequestMapping("jmeter") public class JmeterFileController { @Resource private JmeterFileService jmeterFileService; - private final WeakConcurrentHashMap> readyMap = new WeakConcurrentHashMap<>(30 * 60 * 1000);// 默认保留30分钟 + @Resource + private StringRedisTemplate stringRedisTemplate; @GetMapping("ping") public String checkStatus() { @@ -32,10 +34,15 @@ public class JmeterFileController { public long ready(@RequestParam("reportId") String reportId, @RequestParam("ratio") String ratio, @RequestParam("resourceIndex") int resourceIndex) { try { - List ratios = readyMap.getOrDefault(reportId, Arrays.stream(ratio.split(",")).map(Double::parseDouble).collect(Collectors.toList())); - ratios.set(resourceIndex, -1.0); - readyMap.put(reportId, ratios); - return ratios.stream().filter(r -> r > 0).count(); + // 保存当前节点状态到redis + String reportIdKey = "jmeter_ready:" + reportId; + stringRedisTemplate.opsForHash().put(reportIdKey, resourceIndex, 1); + // 设置30分钟过期 + stringRedisTemplate.expire(reportIdKey, 30 * 60, TimeUnit.SECONDS); + // 返回当前已经准备好的节点数量 + List values = stringRedisTemplate.opsForHash().values(reportIdKey); + return Arrays.asList(ratio.split(",")).size() - CollectionUtils.size(values); + } catch (Exception e) { return 0; }