refactor(接口测试): 优化执行过程减少对node节点发送过多心跳检查

This commit is contained in:
fit2-zhao 2022-10-27 17:29:00 +08:00 committed by fit2-zhao
parent dd31522be2
commit ccc9141b3d
5 changed files with 40 additions and 64 deletions

View File

@ -5,23 +5,19 @@ import io.metersphere.api.exec.engine.EngineFactory;
import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
import io.metersphere.api.jmeter.utils.ServerConfig;
import io.metersphere.api.jmeter.utils.SmoothWeighted;
import io.metersphere.base.domain.TestResource;
import io.metersphere.commons.config.KafkaConfig;
import io.metersphere.service.RemakeReportService;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.*;
import io.metersphere.config.JmeterProperties;
import io.metersphere.constants.BackendListenerConstants;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.JvmInfoDTO;
import io.metersphere.dto.NodeDTO;
import io.metersphere.engine.Engine;
import io.metersphere.jmeter.JMeterBase;
import io.metersphere.jmeter.LocalRunner;
import io.metersphere.commons.utils.FixedCapacityUtil;
import io.metersphere.commons.utils.GenerateHashTreeUtil;
import io.metersphere.commons.utils.HashTreeUtil;
import io.metersphere.service.RemakeReportService;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
@ -203,13 +199,13 @@ public class JMeterService {
public boolean getRunningQueue(String poolId, String reportId) {
try {
List<JvmInfoDTO> resources = GenerateHashTreeUtil.setPoolResource(poolId);
List<TestResource> resources = GenerateHashTreeUtil.setPoolResource(poolId);
if (CollectionUtils.isEmpty(resources)) {
return false;
}
boolean isRunning = false;
for (JvmInfoDTO testResource : resources) {
String configuration = testResource.getTestResource().getConfiguration();
for (TestResource testResource : resources) {
String configuration = testResource.getConfiguration();
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
String nodeIp = node.getIp();
Integer port = node.getPort();

View File

@ -41,34 +41,38 @@ public class ResourcePoolCalculation {
}
}
public List<JvmInfoDTO> getPools(String resourcePoolId) {
// 获取可以执行的资源池
public List<TestResource> getResourcePools(String resourcePoolId) {
TestResourcePoolExample example = new TestResourcePoolExample();
example.createCriteria().andStatusEqualTo("VALID").andTypeEqualTo("NODE").andIdEqualTo(resourcePoolId);
List<TestResourcePool> pools = testResourcePoolMapper.selectByExample(example);
// 按照NODE节点的可用内存空间大小排序
List<JvmInfoDTO> availableNodes = new ArrayList<>();
if (CollectionUtils.isNotEmpty(pools)) {
List<String> poolIds = pools.stream().map(pool -> pool.getId()).collect(Collectors.toList());
TestResourceExample resourceExample = new TestResourceExample();
resourceExample.createCriteria().andTestResourcePoolIdIn(poolIds);
List<TestResource> testResources = testResourceMapper.selectByExampleWithBLOBs(resourceExample);
for (TestResource testResource : testResources) {
String configuration = testResource.getConfiguration();
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
String nodeIp = node.getIp();
Integer port = node.getPort();
String uri = String.format(BASE_URL + "/jmeter/get-jvm-info", nodeIp, port);
JvmInfoDTO nodeJvm = this.getNodeJvmInfo(uri);
if (nodeJvm == null) {
continue;
}
TestResourceDTO dto = new TestResourceDTO();
BeanUtils.copyBean(dto, testResource);
nodeJvm.setTestResource(dto);
availableNodes.add(nodeJvm);
return testResources;
}
return new ArrayList<>();
}
public List<JvmInfoDTO> getPools(String resourcePoolId) {
// 按照NODE节点的可用内存空间大小排序
List<JvmInfoDTO> availableNodes = new ArrayList<>();
List<TestResource> testResources = getResourcePools(resourcePoolId);
for (TestResource testResource : testResources) {
String configuration = testResource.getConfiguration();
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
String nodeIp = node.getIp();
Integer port = node.getPort();
String uri = String.format(BASE_URL + "/jmeter/get-jvm-info", nodeIp, port);
JvmInfoDTO nodeJvm = this.getNodeJvmInfo(uri);
if (nodeJvm == null) {
continue;
}
TestResourceDTO dto = new TestResourceDTO();
BeanUtils.copyBean(dto, testResource);
nodeJvm.setTestResource(dto);
availableNodes.add(nodeJvm);
}
return availableNodes;
}

View File

@ -1,11 +1,10 @@
package io.metersphere.api.jmeter.utils;
import io.metersphere.base.domain.TestResource;
import io.metersphere.commons.utils.GenerateHashTreeUtil;
import io.metersphere.commons.utils.JSON;
import io.metersphere.dto.JvmInfoDTO;
import io.metersphere.dto.NodeDTO;
import io.metersphere.dto.TestResourceDTO;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.vo.BooleanPool;
import org.apache.commons.collections4.CollectionUtils;
@ -27,7 +26,7 @@ public class SmoothWeighted {
if (StringUtils.isEmpty(poolId)) {
return;
}
List<JvmInfoDTO> resources = new ArrayList<>();
List<TestResource> resources = new ArrayList<>();
BooleanPool pool = GenerateHashTreeUtil.isResourcePool(poolId);
if (pool.isPool()) {
resources = GenerateHashTreeUtil.setPoolResource(poolId);
@ -39,8 +38,8 @@ public class SmoothWeighted {
}
Map<String, ServerConfig> configs = new HashMap<>();
for (JvmInfoDTO jvmInfoDTO : resources) {
String configuration = jvmInfoDTO.getTestResource().getConfiguration();
for (TestResource testResource : resources) {
String configuration = testResource.getConfiguration();
if (StringUtils.isNotEmpty(configuration)) {
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
String uri = String.format(BASE_URL + "/jmeter/api/start", node.getIp(), node.getPort());
@ -115,10 +114,10 @@ public class SmoothWeighted {
public static ServerConfig getResource(String poolId) {
BooleanPool pool = GenerateHashTreeUtil.isResourcePool(poolId);
if (pool.isPool()) {
List<JvmInfoDTO> resources = GenerateHashTreeUtil.setPoolResource(poolId);
List<TestResource> resources = GenerateHashTreeUtil.setPoolResource(poolId);
if (CollectionUtils.isNotEmpty(resources)) {
int index = (int) (Math.random() * resources.size());
TestResourceDTO testResource = resources.get(index).getTestResource();
TestResource testResource = resources.get(index);
NodeDTO node = JSON.parseObject(testResource.getConfiguration(), NodeDTO.class);
String nodeIp = node.getIp();
Integer port = node.getPort();

View File

@ -10,6 +10,7 @@ import io.metersphere.api.dto.definition.request.*;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
import io.metersphere.api.jmeter.NewDriverManager;
import io.metersphere.api.jmeter.ResourcePoolCalculation;
import io.metersphere.base.domain.TestResource;
import io.metersphere.service.ApiExecutionQueueService;
import io.metersphere.service.RemakeReportService;
import io.metersphere.commons.constants.ElementConstants;
@ -95,10 +96,10 @@ public class GenerateHashTreeUtil {
return pool;
}
public static List<JvmInfoDTO> setPoolResource(String id) {
public static List<TestResource> setPoolResource(String id) {
if (GenerateHashTreeUtil.isResourcePool(id).isPool() && !GenerateHashTreeUtil.isResourcePool(id).isK8s()) {
ResourcePoolCalculation resourcePoolCalculation = CommonBeanFactory.getBean(ResourcePoolCalculation.class);
return resourcePoolCalculation.getPools(id);
return resourcePoolCalculation.getResourcePools(id);
}
return new LinkedList<>();
}

View File

@ -22,6 +22,7 @@ import io.metersphere.commons.enums.ApiReportStatus;
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.ResultDTO;
import io.metersphere.commons.utils.FixedCapacityUtil;
@ -119,7 +120,7 @@ public class RemakeReportService {
}
} else {
ApiScenarioReportWithBLOBs report = apiScenarioReportMapper.selectByPrimaryKey(request.getReportId());
if (report != null) {
if (report != null && !StringUtils.equals(request.getRunType(), RunModeConstants.SERIAL.toString())) {
report.setStatus(ApiReportStatus.ERROR.name());
apiScenarioReportMapper.updateByPrimaryKeySelective(report);
}
@ -145,31 +146,6 @@ public class RemakeReportService {
}
}
public void remakeScenario(String runMode, String scenarioId, ApiScenarioWithBLOBs scenarioWithBLOBs, ApiScenarioReportWithBLOBs report) {
// 生成失败报告
if (StringUtils.equalsAny(runMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name(), ApiRunMode.SCENARIO_PLAN.name())) {
TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(scenarioId);
if (testPlanApiScenario != null) {
report.setScenarioId(scenarioWithBLOBs.getId());
report.setEndTime(System.currentTimeMillis());
testPlanApiScenario.setLastResult(ApiReportStatus.ERROR.name());
testPlanApiScenario.setPassRate("0%");
testPlanApiScenario.setReportId(report.getId());
testPlanApiScenario.setUpdateTime(report.getCreateTime());
testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario);
}
} else {
scenarioWithBLOBs.setLastResult(ApiReportStatus.ERROR.name());
scenarioWithBLOBs.setPassRate("0%");
scenarioWithBLOBs.setReportId(report.getId());
scenarioWithBLOBs.setExecuteTimes(1);
apiScenarioMapper.updateByPrimaryKey(scenarioWithBLOBs);
}
report.setStatus(ApiReportStatus.ERROR.name());
apiScenarioReportMapper.updateByPrimaryKeySelective(report);
}
public void testEnded(JmeterRunRequestDTO request, String errorMsg) {
try {
ResultDTO dto = new ResultDTO();