From ac369fea634d0c00000955aa295f4f316c553d17 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 24 Mar 2020 20:07:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E4=BA=8E=E5=B9=B6=E5=8F=91=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/engine/AbstractEngine.java | 81 +++++++++++++++++++ .../engine/docker/DockerTestEngine.java | 36 ++++++--- .../kubernetes/KubernetesTestEngine.java | 72 +---------------- 3 files changed, 112 insertions(+), 77 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/engine/AbstractEngine.java diff --git a/backend/src/main/java/io/metersphere/engine/AbstractEngine.java b/backend/src/main/java/io/metersphere/engine/AbstractEngine.java new file mode 100644 index 0000000000..1fb7e2da6f --- /dev/null +++ b/backend/src/main/java/io/metersphere/engine/AbstractEngine.java @@ -0,0 +1,81 @@ +package io.metersphere.engine; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import io.metersphere.base.domain.FileMetadata; +import io.metersphere.base.domain.LoadTestWithBLOBs; +import io.metersphere.base.domain.TestResource; +import io.metersphere.base.domain.TestResourcePool; +import io.metersphere.commons.constants.ResourcePoolTypeEnum; +import io.metersphere.commons.constants.TestStatus; +import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.CommonBeanFactory; +import io.metersphere.service.LoadTestService; +import io.metersphere.service.TestResourcePoolService; +import io.metersphere.service.TestResourceService; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.List; + +public abstract class AbstractEngine implements Engine { + protected FileMetadata jmxFile; + protected List csvFiles; + protected LoadTestWithBLOBs loadTest; + protected LoadTestService loadTestService; + protected Integer threadNum; + protected List resourceList; + + @Override + public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles) { + this.loadTest = loadTest; + this.jmxFile = fileMetadata; + this.csvFiles = csvFiles; + TestResourcePoolService testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class); + 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"); + } + } + String resourcePoolId = loadTest.getTestResourcePoolId(); + if (StringUtils.isBlank(resourcePoolId)) { + MSException.throwException("Resource Pool ID is empty"); + } + TestResourcePool resourcePool = testResourcePoolService.getResourcePool(resourcePoolId); + if (resourcePool == null) { + MSException.throwException("Resource Pool is empty"); + } + if (!ResourcePoolTypeEnum.K8S.name().equals(resourcePool.getType())) { + MSException.throwException("Invalid Resource Pool type."); + } + this.resourceList = testResourceService.getResourcesByPoolId(resourcePool.getId()); + if (CollectionUtils.isEmpty(this.resourceList)) { + MSException.throwException("Test Resource is empty"); + } + return true; + } + + protected Integer getSumThreadNum() { + 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 s; + }).reduce(Integer::sum).orElse(0); + } +} 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 87ad422164..b93e428b3a 100644 --- a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java @@ -1,32 +1,50 @@ 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.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.controller.request.TestRequest; -import io.metersphere.engine.Engine; +import io.metersphere.dto.NodeDTO; +import io.metersphere.engine.AbstractEngine; import io.metersphere.engine.EngineContext; +import io.metersphere.engine.EngineFactory; import org.apache.commons.lang3.StringUtils; import org.springframework.web.client.RestTemplate; + import java.util.HashMap; import java.util.List; -public class DockerTestEngine implements Engine { - private EngineContext context; +public class DockerTestEngine extends AbstractEngine { + + private RestTemplate restTemplate; - RestTemplate restTemplate; @Override - public boolean init(EngineContext context) { + public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles) { + super.init(loadTest, fileMetadata, csvFiles); this.restTemplate = CommonBeanFactory.getBean(RestTemplate.class); // todo 初始化操作 - this.context = context; return true; } @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); + // todo 运行测试 -// RestTemplate restTemplate = new RestTemplate(); + EngineContext context = null; + try { + context = EngineFactory.createContext(loadTest, jmxFile, csvFiles); + } catch (Exception e) { + e.printStackTrace(); + } + String testId = context.getTestId(); String content = context.getContent(); @@ -42,7 +60,7 @@ public class DockerTestEngine implements Engine { List containerList = restTemplate.getForObject(taskStatusUri, List.class); for (int i = 0; i < containerList.size(); i++) { HashMap h = (HashMap) containerList.get(i); - if (StringUtils.equals((String)h.get("State"), "running")) { + if (StringUtils.equals((String) h.get("State"), "running")) { MSException.throwException("the test is running!"); } } @@ -55,7 +73,7 @@ public class DockerTestEngine implements Engine { // TODO 停止运行测试 // RestTemplate restTemplate = new RestTemplate(); - String testId = context.getTestId(); + String testId = loadTest.getId(); String uri = "http://localhost:8082/jmeter/container/stop/" + testId; restTemplate.postForObject(uri, "", String.class); 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 ca2a94904b..9fd398bf07 100644 --- a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java @@ -1,102 +1,38 @@ package io.metersphere.engine.kubernetes; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.client.KubernetesClient; import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.LoadTestWithBLOBs; -import io.metersphere.base.domain.TestResource; -import io.metersphere.base.domain.TestResourcePool; -import io.metersphere.commons.constants.ResourcePoolTypeEnum; -import io.metersphere.commons.constants.TestStatus; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; -import io.metersphere.engine.Engine; +import io.metersphere.engine.AbstractEngine; import io.metersphere.engine.EngineContext; import io.metersphere.engine.EngineFactory; import io.metersphere.engine.kubernetes.crds.jmeter.Jmeter; import io.metersphere.engine.kubernetes.crds.jmeter.JmeterSpec; import io.metersphere.engine.kubernetes.provider.ClientCredential; import io.metersphere.engine.kubernetes.provider.KubernetesProvider; -import io.metersphere.service.LoadTestService; -import io.metersphere.service.TestResourcePoolService; -import io.metersphere.service.TestResourceService; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; import java.util.HashMap; import java.util.List; -public class KubernetesTestEngine implements Engine { - private FileMetadata jmxFile; - private List csvFiles; - private LoadTestWithBLOBs loadTest; - private LoadTestService loadTestService; - private Integer threadNum; - - private String resourcePoolId; - private List resourceList; +public class KubernetesTestEngine extends AbstractEngine { @Override public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles) { - this.loadTest = loadTest; - this.jmxFile = fileMetadata; - this.csvFiles = csvFiles; - TestResourcePoolService testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class); - 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("resourcePoolId", o.getString("key"))) { - resourcePoolId = o.getString("value"); - } - if (StringUtils.equals(o.getString("key"), "TargetLevel")) { - threadNum = o.getInteger("value"); - } - } - if (StringUtils.isBlank(resourcePoolId)) { - MSException.throwException("Resource Pool ID is empty"); - } - TestResourcePool resourcePool = testResourcePoolService.getResourcePool(resourcePoolId); - if (resourcePool == null) { - MSException.throwException("Resource Pool is empty"); - } - if (!ResourcePoolTypeEnum.K8S.name().equals(resourcePool.getType())) { - MSException.throwException("Invalid Resource Pool type."); - } - this.resourceList = testResourceService.getResourcesByPoolId(resourcePool.getId()); - if (CollectionUtils.isEmpty(this.resourceList)) { - MSException.throwException("Test Resource is empty"); - } + super.init(loadTest, fileMetadata, csvFiles); return true; } @Override public void start() { + Integer sumThreadNum = getSumThreadNum(); // resourceList size 1 - List loadTests = loadTestService.selectByTestResourcePoolId(resourcePoolId); - // 使用当前资源池正在运行的测试占用的并发数 - Integer sumThreadNum = 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 s; - }).reduce(Integer::sum).orElse(0); resourceList.forEach(r -> { String configuration = r.getConfiguration(); ClientCredential clientCredential = JSON.parseObject(configuration, ClientCredential.class);