From ccc9141b3de36ab4f3be76f43868b6ad3b7d3a5e Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 27 Oct 2022 17:29:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=89=A7=E8=A1=8C=E8=BF=87=E7=A8=8B?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E5=AF=B9node=E8=8A=82=E7=82=B9=E5=8F=91?= =?UTF-8?q?=E9=80=81=E8=BF=87=E5=A4=9A=E5=BF=83=E8=B7=B3=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/api/jmeter/JMeterService.java | 16 +++---- .../api/jmeter/ResourcePoolCalculation.java | 42 ++++++++++--------- .../api/jmeter/utils/SmoothWeighted.java | 13 +++--- .../commons/utils/GenerateHashTreeUtil.java | 5 ++- .../service/RemakeReportService.java | 28 +------------ 5 files changed, 40 insertions(+), 64 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java b/api-test/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java index ba12a7cf47..8dc2424c93 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java @@ -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 resources = GenerateHashTreeUtil.setPoolResource(poolId); + List 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(); diff --git a/api-test/backend/src/main/java/io/metersphere/api/jmeter/ResourcePoolCalculation.java b/api-test/backend/src/main/java/io/metersphere/api/jmeter/ResourcePoolCalculation.java index 129a1f04e9..be53cb58cc 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/jmeter/ResourcePoolCalculation.java +++ b/api-test/backend/src/main/java/io/metersphere/api/jmeter/ResourcePoolCalculation.java @@ -41,34 +41,38 @@ public class ResourcePoolCalculation { } } - public List getPools(String resourcePoolId) { - // 获取可以执行的资源池 + public List getResourcePools(String resourcePoolId) { TestResourcePoolExample example = new TestResourcePoolExample(); example.createCriteria().andStatusEqualTo("VALID").andTypeEqualTo("NODE").andIdEqualTo(resourcePoolId); List pools = testResourcePoolMapper.selectByExample(example); - - // 按照NODE节点的可用内存空间大小排序 - List availableNodes = new ArrayList<>(); if (CollectionUtils.isNotEmpty(pools)) { List poolIds = pools.stream().map(pool -> pool.getId()).collect(Collectors.toList()); TestResourceExample resourceExample = new TestResourceExample(); resourceExample.createCriteria().andTestResourcePoolIdIn(poolIds); List 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 getPools(String resourcePoolId) { + // 按照NODE节点的可用内存空间大小排序 + List availableNodes = new ArrayList<>(); + List 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; } diff --git a/api-test/backend/src/main/java/io/metersphere/api/jmeter/utils/SmoothWeighted.java b/api-test/backend/src/main/java/io/metersphere/api/jmeter/utils/SmoothWeighted.java index d5e2b58496..9315884696 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/jmeter/utils/SmoothWeighted.java +++ b/api-test/backend/src/main/java/io/metersphere/api/jmeter/utils/SmoothWeighted.java @@ -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 resources = new ArrayList<>(); + List resources = new ArrayList<>(); BooleanPool pool = GenerateHashTreeUtil.isResourcePool(poolId); if (pool.isPool()) { resources = GenerateHashTreeUtil.setPoolResource(poolId); @@ -39,8 +38,8 @@ public class SmoothWeighted { } Map 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 resources = GenerateHashTreeUtil.setPoolResource(poolId); + List 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(); diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java index a134202f16..bbd2cc29e1 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java @@ -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 setPoolResource(String id) { + public static List 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<>(); } diff --git a/api-test/backend/src/main/java/io/metersphere/service/RemakeReportService.java b/api-test/backend/src/main/java/io/metersphere/service/RemakeReportService.java index 03cc54bb27..5c43fe992f 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/RemakeReportService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/RemakeReportService.java @@ -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();