This commit is contained in:
Captain.B 2020-03-25 10:17:06 +08:00
parent bab322a8cb
commit a04c0fc078
4 changed files with 17 additions and 26 deletions

View File

@ -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<FileMetadata> 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<FileMetadata> 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);

View File

@ -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<FileMetadata> csvFiles, long threadNum) throws Exception {
final FileContent fileContent = fileService.getFileContent(fileMetadata.getId());
public static EngineContext createContext(LoadTestWithBLOBs loadTest, long threadNum) throws Exception {
final List<FileMetadata> 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<FileMetadata> 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());

View File

@ -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();
}

View File

@ -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);