refactor(接口测试): 并发任务分配节点优化

This commit is contained in:
fit2-zhao 2022-04-18 11:05:29 +08:00 committed by 刘瑞斌
parent d56b7e1819
commit 9c5d8d69dd
7 changed files with 37 additions and 30 deletions

View File

@ -6,9 +6,12 @@ import io.metersphere.api.exec.utils.GenerateHashTreeUtil;
import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.utils.SmoothWeighted; import io.metersphere.api.jmeter.utils.SmoothWeighted;
import io.metersphere.base.domain.ApiDefinitionExecResult; import io.metersphere.base.domain.ApiDefinitionExecResult;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.dto.RunModeConfigDTO;
import io.metersphere.service.SystemParameterService;
import io.metersphere.vo.BooleanPool; import io.metersphere.vo.BooleanPool;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
@ -33,6 +36,8 @@ public class ApiCaseParallelExecuteService {
if (pool.isPool()) { if (pool.isPool()) {
SmoothWeighted.setServerConfig(config.getResourcePoolId(), redisTemplate); SmoothWeighted.setServerConfig(config.getResourcePoolId(), redisTemplate);
} }
// 获取可以执行的资源池
BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
for (String testId : executeQueue.keySet()) { for (String testId : executeQueue.keySet()) {
ApiDefinitionExecResult result = executeQueue.get(testId); ApiDefinitionExecResult result = executeQueue.get(testId);
String reportId = result.getId(); String reportId = result.getId();
@ -44,7 +49,7 @@ public class ApiCaseParallelExecuteService {
runRequest.setRunType(RunModeConstants.PARALLEL.toString()); runRequest.setRunType(RunModeConstants.PARALLEL.toString());
runRequest.setQueueId(executionQueue.getId()); runRequest.setQueueId(executionQueue.getId());
if (MapUtils.isNotEmpty(executionQueue.getDetailMap())) { 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()) { if (!pool.isPool()) {
HashTree hashTree = apiScenarioSerialService.generateHashTree(testId, config.getEnvMap(), runRequest); HashTree hashTree = apiScenarioSerialService.generateHashTree(testId, config.getEnvMap(), runRequest);

View File

@ -7,8 +7,11 @@ import io.metersphere.api.exec.queue.DBTestQueue;
import io.metersphere.api.exec.utils.GenerateHashTreeUtil; import io.metersphere.api.exec.utils.GenerateHashTreeUtil;
import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.utils.SmoothWeighted; import io.metersphere.api.jmeter.utils.SmoothWeighted;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.service.SystemParameterService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import io.metersphere.vo.BooleanPool; import io.metersphere.vo.BooleanPool;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -31,6 +34,8 @@ public class ApiScenarioParallelService {
if (pool.isPool()) { if (pool.isPool()) {
SmoothWeighted.setServerConfig(request.getConfig().getResourcePoolId(), redisTemplate); SmoothWeighted.setServerConfig(request.getConfig().getResourcePoolId(), redisTemplate);
} }
// 获取可以执行的资源池
BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
for (String reportId : executeQueue.keySet()) { for (String reportId : executeQueue.keySet()) {
RunModeDataDTO dataDTO = executeQueue.get(reportId); RunModeDataDTO dataDTO = executeQueue.get(reportId);
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(dataDTO.getTestId(), StringUtils.isNotEmpty(serialReportId) ? serialReportId : reportId, request.getRunMode(), null); 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.setPoolId(request.getConfig().getResourcePoolId());
runRequest.setTestPlanReportId(request.getTestPlanReportId()); 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()); runRequest.setRunType(RunModeConstants.PARALLEL.toString());
if (LoggerUtil.getLogger().isDebugEnabled()) { if (LoggerUtil.getLogger().isDebugEnabled()) {
LoggerUtil.debug("Scenario run-开始并发执行:" + JSON.toJSONString(request)); LoggerUtil.debug("Scenario run-开始并发执行:" + JSON.toJSONString(request));

View File

@ -28,9 +28,11 @@ import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.HashTreeUtil; import io.metersphere.commons.utils.HashTreeUtil;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.ResultDTO; import io.metersphere.dto.ResultDTO;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.SystemParameterService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
@ -67,6 +69,8 @@ public class ApiScenarioSerialService {
public void serial(ApiExecutionQueue executionQueue, ApiExecutionQueueDetail queue) { public void serial(ApiExecutionQueue executionQueue, ApiExecutionQueueDetail queue) {
LoggerUtil.debug("Scenario run-执行脚本装载-进入串行准备"); LoggerUtil.debug("Scenario run-执行脚本装载-进入串行准备");
// 获取可以执行的资源池
BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
if (!StringUtils.equals(executionQueue.getReportType(), RunModeConstants.SET_REPORT.toString()) if (!StringUtils.equals(executionQueue.getReportType(), RunModeConstants.SET_REPORT.toString())
|| StringUtils.equalsIgnoreCase(executionQueue.getRunMode(), ApiRunMode.DEFINITION.name())) { || 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())) { 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); runRequest.setHashTree(hashTree);
if (queue != null) { if (queue != null) {
runRequest.setPlatformUrl(queue.getId()); runRequest.setPlatformUrl(GenerateHashTreeUtil.getPlatformUrl(baseInfo, runRequest, queue.getId()));
} }
if (runRequest.getPool().isPool()) { if (runRequest.getPool().isPool()) {
SmoothWeighted.setServerConfig(runRequest.getPoolId(), redisTemplate); SmoothWeighted.setServerConfig(runRequest.getPoolId(), redisTemplate);

View File

@ -21,10 +21,7 @@ import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.*;
import io.metersphere.dto.JvmInfoDTO;
import io.metersphere.dto.ResultDTO;
import io.metersphere.dto.RunModeConfigDTO;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.EnvironmentGroupProjectService; import io.metersphere.service.EnvironmentGroupProjectService;
import io.metersphere.vo.BooleanPool; import io.metersphere.vo.BooleanPool;
@ -161,4 +158,20 @@ public class GenerateHashTreeUtil {
public static boolean isSetReport(RunModeConfigDTO config) { public static boolean isSetReport(RunModeConfigDTO config) {
return config != null && StringUtils.equals(config.getReportType(), RunModeConstants.SET_REPORT.toString()) && StringUtils.isNotEmpty(config.getReportName()); 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;
}
} }

View File

@ -14,7 +14,6 @@ import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.config.JmeterProperties; import io.metersphere.config.JmeterProperties;
import io.metersphere.config.KafkaConfig; import io.metersphere.config.KafkaConfig;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.JvmInfoDTO; import io.metersphere.dto.JvmInfoDTO;
import io.metersphere.dto.NodeDTO; import io.metersphere.dto.NodeDTO;
@ -22,7 +21,6 @@ import io.metersphere.jmeter.JMeterBase;
import io.metersphere.jmeter.LocalRunner; import io.metersphere.jmeter.LocalRunner;
import io.metersphere.performance.engine.Engine; import io.metersphere.performance.engine.Engine;
import io.metersphere.performance.engine.EngineFactory; import io.metersphere.performance.engine.EngineFactory;
import io.metersphere.service.SystemParameterService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
@ -121,24 +119,6 @@ public class JMeterService {
} }
private void runNode(JmeterRunRequestDTO request) { 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()); request.setKafkaConfig(KafkaConfig.getKafka());
// 如果是K8S调用 // 如果是K8S调用
if (request.getPool().isK8s()) { if (request.getPool().isK8s()) {

View File

@ -23,7 +23,7 @@ public class SmoothWeighted {
public static final String EXEC_INDEX = "EXEC_INDEX_"; public static final String EXEC_INDEX = "EXEC_INDEX_";
public static void setServerConfig(String poolId, RedisTemplate client) { public static void setServerConfig(String poolId, RedisTemplate client) {
if (StringUtils.isNotEmpty(poolId)) { if (StringUtils.isEmpty(poolId)) {
return; return;
} }
List<JvmInfoDTO> resources = new ArrayList<>(); List<JvmInfoDTO> 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) { if (client.opsForValue().get(CONFIG + poolId) != null) {
client.opsForValue().set(CONFIG + poolId, serverList); client.opsForValue().set(CONFIG + poolId, serverList);

View File

@ -86,7 +86,7 @@ public class ApiJmeterFileService {
envMap = JSON.parseObject(detail.getEvnMap(), Map.class); envMap = JSON.parseObject(detail.getEvnMap(), Map.class);
} }
if (MapUtils.isEmpty(envMap)) { if (MapUtils.isEmpty(envMap)) {
LoggerUtil.info("测试资源:【" + remoteTestId + "未找到可执行的环境 >>>>>>> "); LoggerUtil.info("测试资源:【" + remoteTestId + ", 报告【" + reportId + "】未重新选择环境");
} }
HashTree hashTree = null; 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())) { 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())) {