fix(性能测试): 修复性能测试部署多个节点时出现time sync 的问题
This commit is contained in:
parent
a02673b154
commit
253194935e
|
@ -1,11 +1,12 @@
|
||||||
package io.metersphere.controller;
|
package io.metersphere.controller;
|
||||||
|
|
||||||
|
|
||||||
import io.metersphere.commons.utils.WeakConcurrentHashMap;
|
|
||||||
import io.metersphere.controller.handler.annotation.NoResultHolder;
|
import io.metersphere.controller.handler.annotation.NoResultHolder;
|
||||||
import io.metersphere.service.JmeterFileService;
|
import io.metersphere.service.JmeterFileService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
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.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("jmeter")
|
@RequestMapping("jmeter")
|
||||||
public class JmeterFileController {
|
public class JmeterFileController {
|
||||||
@Resource
|
@Resource
|
||||||
private JmeterFileService jmeterFileService;
|
private JmeterFileService jmeterFileService;
|
||||||
private final WeakConcurrentHashMap<String, List<Double>> readyMap = new WeakConcurrentHashMap<>(30 * 60 * 1000);// 默认保留30分钟
|
@Resource
|
||||||
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
@GetMapping("ping")
|
@GetMapping("ping")
|
||||||
public String checkStatus() {
|
public String checkStatus() {
|
||||||
|
@ -32,10 +34,15 @@ public class JmeterFileController {
|
||||||
public long ready(@RequestParam("reportId") String reportId, @RequestParam("ratio") String ratio,
|
public long ready(@RequestParam("reportId") String reportId, @RequestParam("ratio") String ratio,
|
||||||
@RequestParam("resourceIndex") int resourceIndex) {
|
@RequestParam("resourceIndex") int resourceIndex) {
|
||||||
try {
|
try {
|
||||||
List<Double> ratios = readyMap.getOrDefault(reportId, Arrays.stream(ratio.split(",")).map(Double::parseDouble).collect(Collectors.toList()));
|
// 保存当前节点状态到redis
|
||||||
ratios.set(resourceIndex, -1.0);
|
String reportIdKey = "jmeter_ready:" + reportId;
|
||||||
readyMap.put(reportId, ratios);
|
stringRedisTemplate.opsForHash().put(reportIdKey, resourceIndex, 1);
|
||||||
return ratios.stream().filter(r -> r > 0).count();
|
// 设置30分钟过期
|
||||||
|
stringRedisTemplate.expire(reportIdKey, 30 * 60, TimeUnit.SECONDS);
|
||||||
|
// 返回当前已经准备好的节点数量
|
||||||
|
List<Object> values = stringRedisTemplate.opsForHash().values(reportIdKey);
|
||||||
|
return Arrays.asList(ratio.split(",")).size() - CollectionUtils.size(values);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue