From a04c0fc07808602f5df7c2320a021a964e8bbb1c Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 25 Mar 2020 10:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/engine/AbstractEngine.java | 21 ------------------- .../io/metersphere/engine/EngineFactory.java | 18 +++++++++++++--- .../engine/docker/DockerTestEngine.java | 2 +- .../kubernetes/KubernetesTestEngine.java | 2 +- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/backend/src/main/java/io/metersphere/engine/AbstractEngine.java b/backend/src/main/java/io/metersphere/engine/AbstractEngine.java index e5544eb2c9..3596b135ab 100644 --- a/backend/src/main/java/io/metersphere/engine/AbstractEngine.java +++ b/backend/src/main/java/io/metersphere/engine/AbstractEngine.java @@ -3,17 +3,13 @@ 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.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; @@ -21,11 +17,8 @@ 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; - protected List csvFiles; protected LoadTestWithBLOBs loadTest; protected LoadTestService loadTestService; protected Integer threadNum; @@ -33,12 +26,10 @@ public abstract class AbstractEngine implements Engine { 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 @@ -48,18 +39,6 @@ public abstract class AbstractEngine implements Engine { } this.loadTest = loadTest; - 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/EngineFactory.java b/backend/src/main/java/io/metersphere/engine/EngineFactory.java index ffd58df045..6d522e696b 100644 --- a/backend/src/main/java/io/metersphere/engine/EngineFactory.java +++ b/backend/src/main/java/io/metersphere/engine/EngineFactory.java @@ -6,6 +6,7 @@ import io.metersphere.base.domain.FileContent; import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.TestResourcePool; +import io.metersphere.commons.constants.FileType; import io.metersphere.commons.constants.ResourcePoolTypeEnum; import io.metersphere.commons.exception.MSException; import io.metersphere.engine.docker.DockerTestEngine; @@ -24,6 +25,7 @@ import java.io.ByteArrayInputStream; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; @Service public class EngineFactory { @@ -52,8 +54,18 @@ public class EngineFactory { return null; } - public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List csvFiles, long threadNum) throws Exception { - final FileContent fileContent = fileService.getFileContent(fileMetadata.getId()); + public static EngineContext createContext(LoadTestWithBLOBs loadTest, long threadNum) throws Exception { + 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()); + } + FileMetadata 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()); + }); + + List csvFiles = fileMetadataList.stream().filter(f -> StringUtils.equalsIgnoreCase(f.getType(), FileType.CSV.name())).collect(Collectors.toList()); + final FileContent fileContent = fileService.getFileContent(jmxFile.getId()); if (fileContent == null) { MSException.throwException(Translator.get("run_load_test_file_content_not_found") + loadTest.getId()); } @@ -61,7 +73,7 @@ public class EngineFactory { engineContext.setTestId(loadTest.getId()); engineContext.setTestName(loadTest.getName()); engineContext.setNamespace(loadTest.getProjectId()); - engineContext.setFileType(fileMetadata.getType()); + engineContext.setFileType(jmxFile.getType()); engineContext.setThreadNum(threadNum); engineContext.setResourcePoolId(loadTest.getTestResourcePoolId()); 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 91110cce78..016009543a 100644 --- a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java @@ -57,7 +57,7 @@ public class DockerTestEngine extends AbstractEngine { // todo 运行测试 EngineContext context = null; try { - context = EngineFactory.createContext(loadTest, jmxFile, csvFiles, realThreadNum); + context = EngineFactory.createContext(loadTest, 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 a7f3a20477..a23e009c14 100644 --- a/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/kubernetes/KubernetesTestEngine.java @@ -41,7 +41,7 @@ public class KubernetesTestEngine extends AbstractEngine { MSException.throwException("Insufficient resources"); } try { - EngineContext context = EngineFactory.createContext(loadTest, jmxFile, csvFiles, threadNum); + EngineContext context = EngineFactory.createContext(loadTest, threadNum); runTest(context, clientCredential, 1); } catch (Exception e) { LogUtil.error(e);