From c4cfae5baeca6cc80c52e29ff016df25989e978a Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 24 Mar 2020 22:18:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E6=AF=94=E7=8E=87=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=AE=B9=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/engine/AbstractEngine.java | 39 +++++++++---------- .../io/metersphere/engine/EngineContext.java | 9 +++++ .../io/metersphere/engine/EngineFactory.java | 20 ++-------- .../engine/docker/DockerTestEngine.java | 30 +++++++++++--- .../kubernetes/KubernetesTestEngine.java | 4 +- .../xml/reader/jmx/JmeterDocumentParser.java | 5 +++ 6 files changed, 63 insertions(+), 44 deletions(-) diff --git a/backend/src/main/java/io/metersphere/engine/AbstractEngine.java b/backend/src/main/java/io/metersphere/engine/AbstractEngine.java index 1fb7e2da6f..60c45fd0b7 100644 --- a/backend/src/main/java/io/metersphere/engine/AbstractEngine.java +++ b/backend/src/main/java/io/metersphere/engine/AbstractEngine.java @@ -36,14 +36,7 @@ public abstract class AbstractEngine implements Engine { TestResourceService testResourceService = CommonBeanFactory.getBean(TestResourceService.class); this.loadTestService = CommonBeanFactory.getBean(LoadTestService.class); - String loadConfiguration = loadTest.getLoadConfiguration(); - JSONArray jsonArray = JSON.parseArray(loadConfiguration); - for (int i = 0; i < jsonArray.size(); i++) { - JSONObject o = jsonArray.getJSONObject(i); - if (StringUtils.equals(o.getString("key"), "TargetLevel")) { - threadNum = o.getInteger("value"); - } - } + threadNum = getThreadNum(loadTest); String resourcePoolId = loadTest.getTestResourcePoolId(); if (StringUtils.isBlank(resourcePoolId)) { MSException.throwException("Resource Pool ID is empty"); @@ -62,20 +55,26 @@ public abstract class AbstractEngine implements Engine { return true; } - protected Integer getSumThreadNum() { + protected Integer getRunningThreadNum() { List loadTests = loadTestService.selectByTestResourcePoolId(loadTest.getTestResourcePoolId()); // 使用当前资源池正在运行的测试占用的并发数 - return loadTests.stream().filter(t -> TestStatus.Running.name().equals(t.getStatus())).map(t -> { - Integer s = 0; - String loadConfiguration = t.getLoadConfiguration(); - JSONArray jsonArray = JSON.parseArray(loadConfiguration); - for (int i = 0; i < jsonArray.size(); i++) { - JSONObject o = jsonArray.getJSONObject(i); - if (StringUtils.equals(o.getString("key"), "TargetLevel")) { - s = o.getInteger("value"); - } + return loadTests.stream() + .filter(t -> TestStatus.Running.name().equals(t.getStatus())) + .map(this::getThreadNum) + .reduce(Integer::sum) + .orElse(0); + } + + private Integer getThreadNum(LoadTestWithBLOBs t) { + Integer s = 0; + String loadConfiguration = t.getLoadConfiguration(); + JSONArray jsonArray = JSON.parseArray(loadConfiguration); + for (int i = 0; i < jsonArray.size(); i++) { + JSONObject o = jsonArray.getJSONObject(i); + if (StringUtils.equals(o.getString("key"), "TargetLevel")) { + s = o.getInteger("value"); } - return s; - }).reduce(Integer::sum).orElse(0); + } + return s; } } diff --git a/backend/src/main/java/io/metersphere/engine/EngineContext.java b/backend/src/main/java/io/metersphere/engine/EngineContext.java index 2a5f508220..23001bec39 100644 --- a/backend/src/main/java/io/metersphere/engine/EngineContext.java +++ b/backend/src/main/java/io/metersphere/engine/EngineContext.java @@ -10,6 +10,7 @@ public class EngineContext { private String fileType; private String content; private String resourcePoolId; + private Long threadNum; private Map properties = new HashMap<>(); private Map testData = new HashMap<>(); @@ -76,4 +77,12 @@ public class EngineContext { public void setResourcePoolId(String resourcePoolId) { this.resourcePoolId = resourcePoolId; } + + public Long getThreadNum() { + return threadNum; + } + + public void setThreadNum(Long threadNum) { + this.threadNum = threadNum; + } } diff --git a/backend/src/main/java/io/metersphere/engine/EngineFactory.java b/backend/src/main/java/io/metersphere/engine/EngineFactory.java index dc0095d2bb..ffd58df045 100644 --- a/backend/src/main/java/io/metersphere/engine/EngineFactory.java +++ b/backend/src/main/java/io/metersphere/engine/EngineFactory.java @@ -27,22 +27,11 @@ import java.util.Map; @Service public class EngineFactory { - private static final String RESOURCE_POOL_ID = "resourcePoolId"; private static FileService fileService; private static TestResourcePoolService testResourcePoolService; public static Engine createEngine(LoadTestWithBLOBs loadTest) { - String resourcePoolId = null; - if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) { - final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration()); - for (int i = 0; i < jsonArray.size(); i++) { - final JSONObject jsonObject = jsonArray.getJSONObject(i); - if (StringUtils.equals(jsonObject.getString("key"), RESOURCE_POOL_ID)) { - resourcePoolId = jsonObject.getString("value"); - break; - } - } - } + String resourcePoolId = loadTest.getTestResourcePoolId(); if (StringUtils.isBlank(resourcePoolId)) { MSException.throwException("Resource Pool ID is empty."); } @@ -63,7 +52,7 @@ public class EngineFactory { return null; } - public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles) throws Exception { + public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles, long threadNum) throws Exception { final FileContent fileContent = fileService.getFileContent(fileMetadata.getId()); if (fileContent == null) { MSException.throwException(Translator.get("run_load_test_file_content_not_found") + loadTest.getId()); @@ -73,6 +62,8 @@ public class EngineFactory { engineContext.setTestName(loadTest.getName()); engineContext.setNamespace(loadTest.getProjectId()); engineContext.setFileType(fileMetadata.getType()); + engineContext.setThreadNum(threadNum); + engineContext.setResourcePoolId(loadTest.getTestResourcePoolId()); if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) { final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration()); @@ -80,9 +71,6 @@ public class EngineFactory { for (int i = 0; i < jsonArray.size(); i++) { final JSONObject jsonObject = jsonArray.getJSONObject(i); engineContext.addProperty(jsonObject.getString("key"), jsonObject.get("value")); - if (StringUtils.equals(jsonObject.getString("key"), RESOURCE_POOL_ID)) { - engineContext.setResourcePoolId(jsonObject.getString("value")); - } } } diff --git a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java index b93e428b3a..68d26e4c4f 100644 --- a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java @@ -3,6 +3,7 @@ package io.metersphere.engine.docker; import com.alibaba.fastjson.JSON; import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.LoadTestWithBLOBs; +import io.metersphere.commons.constants.ResourceStatusEnum; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.controller.request.TestRequest; @@ -15,6 +16,7 @@ import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.List; +import java.util.stream.Collectors; public class DockerTestEngine extends AbstractEngine { @@ -31,16 +33,32 @@ public class DockerTestEngine extends AbstractEngine { @Override public void start() { - Integer runningSumThreadNum = getSumThreadNum(); - Integer integer = resourceList.stream().map(r -> { - NodeDTO nodeDTO = JSON.parseObject(r.getConfiguration(), NodeDTO.class); - return nodeDTO.getMaxConcurrency(); - }).reduce(Integer::sum).orElse(0); + Integer runningSumThreadNum = getRunningThreadNum(); + Integer totalThreadNum = resourceList.stream() + .filter(r -> ResourceStatusEnum.VALID.name().equals(r.getStatus())) + .map(r -> JSON.parseObject(r.getConfiguration(), NodeDTO.class).getMaxConcurrency()) + .reduce(Integer::sum) + .orElse(0); + if (threadNum > totalThreadNum - runningSumThreadNum) { + MSException.throwException("资源不足"); + } + List resourceRatio = resourceList.stream() + .filter(r -> ResourceStatusEnum.VALID.name().equals(r.getStatus())) + .map(r -> JSON.parseObject(r.getConfiguration(), NodeDTO.class).getMaxConcurrency()) + .collect(Collectors.toList()); + resourceRatio.forEach(ratio -> { + double realThreadNum = ((double) ratio / totalThreadNum) * threadNum; + runTest(Math.round(realThreadNum)); + }); + + } + + private void runTest(long realThreadNum) { // todo 运行测试 EngineContext context = null; try { - context = EngineFactory.createContext(loadTest, jmxFile, csvFiles); + context = EngineFactory.createContext(loadTest, jmxFile, csvFiles, realThreadNum); } catch (Exception e) { e.printStackTrace(); } diff --git a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java index 9fd398bf07..86924f77e2 100644 --- a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java @@ -31,7 +31,7 @@ public class KubernetesTestEngine extends AbstractEngine { @Override public void start() { - Integer sumThreadNum = getSumThreadNum(); + Integer sumThreadNum = getRunningThreadNum(); // resourceList size 1 resourceList.forEach(r -> { String configuration = r.getConfiguration(); @@ -43,7 +43,7 @@ public class KubernetesTestEngine extends AbstractEngine { MSException.throwException("资源不足"); } try { - EngineContext context = EngineFactory.createContext(loadTest, jmxFile, csvFiles); + EngineContext context = EngineFactory.createContext(loadTest, jmxFile, csvFiles, threadNum); runTest(context, clientCredential, 1); } catch (Exception e) { LogUtil.error(e); diff --git a/backend/src/main/java/io/metersphere/parse/xml/reader/jmx/JmeterDocumentParser.java b/backend/src/main/java/io/metersphere/parse/xml/reader/jmx/JmeterDocumentParser.java index 0207674797..2f457f543d 100644 --- a/backend/src/main/java/io/metersphere/parse/xml/reader/jmx/JmeterDocumentParser.java +++ b/backend/src/main/java/io/metersphere/parse/xml/reader/jmx/JmeterDocumentParser.java @@ -386,6 +386,11 @@ public class JmeterDocumentParser implements DocumentParser { if (nodeNameEquals(ele, STRING_PROP)) { parseStringProp(ele); } + + // 设置具体的线程数 + if (nodeNameEquals(ele, STRING_PROP) && "TargetLevel".equals(ele.getAttribute("name"))) { + ele.getFirstChild().setNodeValue(context.getThreadNum().toString()); + } } } }