From 9c5d8d69dd1a72b6eb17b0f94a93b6fe98067eb3 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 18 Apr 2022 11:05:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E5=B9=B6=E5=8F=91=E4=BB=BB=E5=8A=A1=E5=88=86=E9=85=8D?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/ApiCaseParallelExecuteService.java | 7 ++++++- .../scenario/ApiScenarioParallelService.java | 7 ++++++- .../scenario/ApiScenarioSerialService.java | 6 +++++- .../api/exec/utils/GenerateHashTreeUtil.java | 21 +++++++++++++++---- .../metersphere/api/jmeter/JMeterService.java | 20 ------------------ .../api/jmeter/utils/SmoothWeighted.java | 4 ++-- .../api/service/ApiJmeterFileService.java | 2 +- 7 files changed, 37 insertions(+), 30 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java index e247a7c239..06b2009b97 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java @@ -6,9 +6,12 @@ import io.metersphere.api.exec.utils.GenerateHashTreeUtil; import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.utils.SmoothWeighted; import io.metersphere.base.domain.ApiDefinitionExecResult; +import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.constants.RunModeConstants; +import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.RunModeConfigDTO; +import io.metersphere.service.SystemParameterService; import io.metersphere.vo.BooleanPool; import org.apache.commons.collections4.MapUtils; import org.apache.jorphan.collections.HashTree; @@ -33,6 +36,8 @@ public class ApiCaseParallelExecuteService { if (pool.isPool()) { SmoothWeighted.setServerConfig(config.getResourcePoolId(), redisTemplate); } + // 获取可以执行的资源池 + BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo(); for (String testId : executeQueue.keySet()) { ApiDefinitionExecResult result = executeQueue.get(testId); String reportId = result.getId(); @@ -44,7 +49,7 @@ public class ApiCaseParallelExecuteService { runRequest.setRunType(RunModeConstants.PARALLEL.toString()); runRequest.setQueueId(executionQueue.getId()); if (MapUtils.isNotEmpty(executionQueue.getDetailMap())) { - runRequest.setPlatformUrl(executionQueue.getDetailMap().get(result.getId())); + runRequest.setPlatformUrl(GenerateHashTreeUtil.getPlatformUrl(baseInfo, runRequest, executionQueue.getDetailMap().get(result.getId()))); } if (!pool.isPool()) { HashTree hashTree = apiScenarioSerialService.generateHashTree(testId, config.getEnvMap(), runRequest); diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java index d490ae96ef..2f97e23988 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java @@ -7,8 +7,11 @@ import io.metersphere.api.exec.queue.DBTestQueue; import io.metersphere.api.exec.utils.GenerateHashTreeUtil; import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.utils.SmoothWeighted; +import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.constants.RunModeConstants; +import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.dto.JmeterRunRequestDTO; +import io.metersphere.service.SystemParameterService; import io.metersphere.utils.LoggerUtil; import io.metersphere.vo.BooleanPool; import org.apache.commons.lang3.StringUtils; @@ -31,6 +34,8 @@ public class ApiScenarioParallelService { if (pool.isPool()) { SmoothWeighted.setServerConfig(request.getConfig().getResourcePoolId(), redisTemplate); } + // 获取可以执行的资源池 + BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo(); for (String reportId : executeQueue.keySet()) { RunModeDataDTO dataDTO = executeQueue.get(reportId); JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(dataDTO.getTestId(), StringUtils.isNotEmpty(serialReportId) ? serialReportId : reportId, request.getRunMode(), null); @@ -41,7 +46,7 @@ public class ApiScenarioParallelService { runRequest.setPoolId(request.getConfig().getResourcePoolId()); runRequest.setTestPlanReportId(request.getTestPlanReportId()); - runRequest.setPlatformUrl(executionQueue.getDetailMap().get(reportId)); + runRequest.setPlatformUrl(GenerateHashTreeUtil.getPlatformUrl(baseInfo, runRequest, executionQueue.getDetailMap().get(reportId))); runRequest.setRunType(RunModeConstants.PARALLEL.toString()); if (LoggerUtil.getLogger().isDebugEnabled()) { LoggerUtil.debug("Scenario run-开始并发执行:" + JSON.toJSONString(request)); diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java index ec1b818e49..d6df961df5 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java @@ -28,9 +28,11 @@ import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.HashTreeUtil; import io.metersphere.commons.utils.LogUtil; import io.metersphere.constants.RunModeConstants; +import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.ResultDTO; import io.metersphere.plugin.core.MsTestElement; +import io.metersphere.service.SystemParameterService; import io.metersphere.utils.LoggerUtil; import org.apache.commons.lang3.StringUtils; import org.apache.jorphan.collections.HashTree; @@ -67,6 +69,8 @@ public class ApiScenarioSerialService { public void serial(ApiExecutionQueue executionQueue, ApiExecutionQueueDetail queue) { LoggerUtil.debug("Scenario run-执行脚本装载-进入串行准备"); + // 获取可以执行的资源池 + BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo(); if (!StringUtils.equals(executionQueue.getReportType(), RunModeConstants.SET_REPORT.toString()) || StringUtils.equalsIgnoreCase(executionQueue.getRunMode(), ApiRunMode.DEFINITION.name())) { if (StringUtils.equalsAny(executionQueue.getRunMode(), ApiRunMode.SCENARIO.name(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { @@ -130,7 +134,7 @@ public class ApiScenarioSerialService { } runRequest.setHashTree(hashTree); if (queue != null) { - runRequest.setPlatformUrl(queue.getId()); + runRequest.setPlatformUrl(GenerateHashTreeUtil.getPlatformUrl(baseInfo, runRequest, queue.getId())); } if (runRequest.getPool().isPool()) { SmoothWeighted.setServerConfig(runRequest.getPoolId(), redisTemplate); diff --git a/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java b/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java index 82de2f7b62..d881bd0820 100644 --- a/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java +++ b/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java @@ -21,10 +21,7 @@ import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; import io.metersphere.constants.RunModeConstants; -import io.metersphere.dto.JmeterRunRequestDTO; -import io.metersphere.dto.JvmInfoDTO; -import io.metersphere.dto.ResultDTO; -import io.metersphere.dto.RunModeConfigDTO; +import io.metersphere.dto.*; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.service.EnvironmentGroupProjectService; import io.metersphere.vo.BooleanPool; @@ -161,4 +158,20 @@ public class GenerateHashTreeUtil { public static boolean isSetReport(RunModeConfigDTO config) { return config != null && StringUtils.equals(config.getReportType(), RunModeConstants.SET_REPORT.toString()) && StringUtils.isNotEmpty(config.getReportName()); } + + public static String getPlatformUrl(BaseSystemConfigDTO baseInfo, JmeterRunRequestDTO request, String queueDetailId) { + // 占位符 + String platformUrl = "http://localhost:8081"; + if (baseInfo != null) { + platformUrl = baseInfo.getUrl(); + } + + platformUrl += "/api/jmeter/download?testId=" + + request.getTestId() + + "&reportId=" + request.getReportId() + + "&runMode=" + request.getRunMode() + + "&reportType=" + request.getReportType() + + "&queueId=" + queueDetailId; + return platformUrl; + } } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java index 0fa2b5acbc..439a74850d 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java @@ -14,7 +14,6 @@ import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.config.JmeterProperties; import io.metersphere.config.KafkaConfig; import io.metersphere.constants.RunModeConstants; -import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JvmInfoDTO; import io.metersphere.dto.NodeDTO; @@ -22,7 +21,6 @@ import io.metersphere.jmeter.JMeterBase; import io.metersphere.jmeter.LocalRunner; import io.metersphere.performance.engine.Engine; import io.metersphere.performance.engine.EngineFactory; -import io.metersphere.service.SystemParameterService; import io.metersphere.utils.LoggerUtil; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -121,24 +119,6 @@ public class JMeterService { } private void runNode(JmeterRunRequestDTO request) { - // 获取可以执行的资源池 - BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo(); - // 占位符 - String platformUrl = "http://localhost:8081"; - if (baseInfo != null) { - platformUrl = baseInfo.getUrl(); - } - // 临时存放 - String queueDetailId = request.getPlatformUrl(); - - platformUrl += "/api/jmeter/download?testId=" - + request.getTestId() - + "&reportId=" + request.getReportId() - + "&runMode=" + request.getRunMode() - + "&reportType=" + request.getReportType() - + "&queueId=" + queueDetailId; - - request.setPlatformUrl(platformUrl); request.setKafkaConfig(KafkaConfig.getKafka()); // 如果是K8S调用 if (request.getPool().isK8s()) { diff --git a/backend/src/main/java/io/metersphere/api/jmeter/utils/SmoothWeighted.java b/backend/src/main/java/io/metersphere/api/jmeter/utils/SmoothWeighted.java index bcf0be75c8..b9f3c2d7d9 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/utils/SmoothWeighted.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/utils/SmoothWeighted.java @@ -23,7 +23,7 @@ public class SmoothWeighted { public static final String EXEC_INDEX = "EXEC_INDEX_"; public static void setServerConfig(String poolId, RedisTemplate client) { - if (StringUtils.isNotEmpty(poolId)) { + if (StringUtils.isEmpty(poolId)) { return; } List resources = new ArrayList<>(); @@ -103,7 +103,7 @@ public class SmoothWeighted { } // 选中前的当前权重 - LoggerUtil.info("第" + (execIndex) + "次选中前的当前权重:" + serverList.toString()); + LoggerUtil.info("第" + (execIndex) + "次选中前的当前权重:" + JSON.toJSONString(serverList)); if (client.opsForValue().get(CONFIG + poolId) != null) { client.opsForValue().set(CONFIG + poolId, serverList); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java b/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java index 8b1c32250c..0f08efba68 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java @@ -86,7 +86,7 @@ public class ApiJmeterFileService { envMap = JSON.parseObject(detail.getEvnMap(), Map.class); } if (MapUtils.isEmpty(envMap)) { - LoggerUtil.info("测试资源:【" + remoteTestId + "】未找到可执行的环境 >>>>>>> "); + LoggerUtil.info("测试资源:【" + remoteTestId + "】, 报告【" + reportId + "】未重新选择环境"); } HashTree hashTree = null; if (StringUtils.equalsAnyIgnoreCase(runMode, ApiRunMode.DEFINITION.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) {