From e1a1175cc9ed464ad573c55de7c91f6bb3b32cc1 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 25 Mar 2020 10:01:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/engine/AbstractEngine.java | 36 ++++++++++++++++--- .../java/io/metersphere/engine/Engine.java | 5 +-- .../engine/docker/DockerTestEngine.java | 5 ++- .../kubernetes/KubernetesTestEngine.java | 6 ++-- .../metersphere/service/LoadTestService.java | 16 ++------- 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/backend/src/main/java/io/metersphere/engine/AbstractEngine.java b/backend/src/main/java/io/metersphere/engine/AbstractEngine.java index 60c45fd0b7..e5544eb2c9 100644 --- a/backend/src/main/java/io/metersphere/engine/AbstractEngine.java +++ b/backend/src/main/java/io/metersphere/engine/AbstractEngine.java @@ -7,10 +7,13 @@ 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.FileType; 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.i18n.Translator; +import io.metersphere.service.FileService; import io.metersphere.service.LoadTestService; import io.metersphere.service.TestResourcePoolService; import io.metersphere.service.TestResourceService; @@ -18,6 +21,7 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import java.util.List; +import java.util.stream.Collectors; public abstract class AbstractEngine implements Engine { protected FileMetadata jmxFile; @@ -27,13 +31,35 @@ public abstract class AbstractEngine implements Engine { protected Integer threadNum; protected List resourceList; + private TestResourcePoolService testResourcePoolService; + private TestResourceService testResourceService; + private FileService fileService; + + public AbstractEngine() { + testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class); + testResourceService = CommonBeanFactory.getBean(TestResourceService.class); + fileService = CommonBeanFactory.getBean(FileService.class); + } + @Override - public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles) { + public boolean init(LoadTestWithBLOBs loadTest) { + if (loadTest == null) { + MSException.throwException("LoadTest is null."); + } this.loadTest = loadTest; - this.jmxFile = fileMetadata; - this.csvFiles = csvFiles; - TestResourcePoolService testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class); - TestResourceService testResourceService = CommonBeanFactory.getBean(TestResourceService.class); + + final List fileMetadataList = fileService.getFileMetadataByTestId(loadTest.getId()); + if (org.springframework.util.CollectionUtils.isEmpty(fileMetadataList)) { + MSException.throwException(Translator.get("run_load_test_file_not_found") + loadTest.getId()); + } + jmxFile = fileMetadataList.stream().filter(f -> StringUtils.equalsIgnoreCase(f.getType(), FileType.JMX.name())) + .findFirst().orElseGet(() -> { + throw new RuntimeException(Translator.get("run_load_test_file_not_found") + loadTest.getId()); + }); + + csvFiles = fileMetadataList.stream().filter(f -> StringUtils.equalsIgnoreCase(f.getType(), FileType.CSV.name())).collect(Collectors.toList()); + + this.loadTestService = CommonBeanFactory.getBean(LoadTestService.class); threadNum = getThreadNum(loadTest); diff --git a/backend/src/main/java/io/metersphere/engine/Engine.java b/backend/src/main/java/io/metersphere/engine/Engine.java index 355222be35..d352472d42 100644 --- a/backend/src/main/java/io/metersphere/engine/Engine.java +++ b/backend/src/main/java/io/metersphere/engine/Engine.java @@ -1,12 +1,9 @@ package io.metersphere.engine; -import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.LoadTestWithBLOBs; -import java.util.List; - public interface Engine { - boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles); + boolean init(LoadTestWithBLOBs loadTest); void start(); 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 68d26e4c4f..7021e4257b 100644 --- a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java @@ -1,7 +1,6 @@ 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; @@ -24,8 +23,8 @@ public class DockerTestEngine extends AbstractEngine { @Override - public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles) { - super.init(loadTest, fileMetadata, csvFiles); + public boolean init(LoadTestWithBLOBs loadTest) { + super.init(loadTest); this.restTemplate = CommonBeanFactory.getBean(RestTemplate.class); // todo 初始化操作 return true; 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 86924f77e2..bc52887888 100644 --- a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java @@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON; 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.commons.exception.MSException; import io.metersphere.commons.utils.LogUtil; @@ -18,13 +17,12 @@ import io.metersphere.engine.kubernetes.provider.KubernetesProvider; import org.apache.commons.collections.MapUtils; import java.util.HashMap; -import java.util.List; public class KubernetesTestEngine extends AbstractEngine { @Override - public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles) { - super.init(loadTest, fileMetadata, csvFiles); + public boolean init(LoadTestWithBLOBs loadTest) { + super.init(loadTest); return true; } diff --git a/backend/src/main/java/io/metersphere/service/LoadTestService.java b/backend/src/main/java/io/metersphere/service/LoadTestService.java index fd6545e28b..16e8859f91 100644 --- a/backend/src/main/java/io/metersphere/service/LoadTestService.java +++ b/backend/src/main/java/io/metersphere/service/LoadTestService.java @@ -16,7 +16,6 @@ import io.metersphere.engine.Engine; import io.metersphere.engine.EngineFactory; import io.metersphere.i18n.Translator; import org.apache.commons.collections4.ListUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -166,27 +165,16 @@ public class LoadTestService { MSException.throwException(Translator.get("run_load_test_not_found") + request.getId()); } - final List fileMetadataList = fileService.getFileMetadataByTestId(request.getId()); - if (CollectionUtils.isEmpty(fileMetadataList)) { - MSException.throwException(Translator.get("run_load_test_file_not_found") + request.getId()); - } - FileMetadata fileMetadata = fileMetadataList.stream().filter(f -> StringUtils.equalsIgnoreCase(f.getType(), FileType.JMX.name())) - .findFirst().orElseGet(() -> { - throw new RuntimeException(Translator.get("run_load_test_file_not_found") + request.getId()); - }); - - List csvFiles = fileMetadataList.stream().filter(f -> StringUtils.equalsIgnoreCase(f.getType(), FileType.CSV.name())).collect(Collectors.toList()); - LogUtil.info("Load test started " + loadTest.getName()); // engine type (NODE|K8S) final Engine engine = EngineFactory.createEngine(loadTest); if (engine == null) { - MSException.throwException(String.format("Test cannot be run,test ID:%s,file type:%s", request.getId(), fileMetadata.getType())); + MSException.throwException(String.format("Test cannot be run,test ID:%s", request.getId())); } boolean init = true; try { - init = engine.init(loadTest, fileMetadata, csvFiles); + init = engine.init(loadTest); } catch (Exception e) { MSException.throwException(e); }