配合使用csv

This commit is contained in:
Captain.B 2020-03-19 20:58:31 +08:00
parent 9c5d32d4c6
commit 6253ac17c6
2 changed files with 40 additions and 19 deletions

View File

@ -9,13 +9,22 @@ import io.metersphere.commons.constants.EngineType;
import io.metersphere.commons.exception.MSException;
import io.metersphere.engine.docker.DockerTestEngine;
import io.metersphere.engine.kubernetes.KubernetesTestEngine;
import io.metersphere.i18n.Translator;
import io.metersphere.parse.EngineSourceParser;
import io.metersphere.parse.EngineSourceParserFactory;
import io.metersphere.service.FileService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.util.List;
@Service
public class EngineFactory {
private static FileService fileService;
public static Engine createEngine(String engineType) {
final EngineType type = EngineType.valueOf(engineType);
@ -28,7 +37,11 @@ public class EngineFactory {
return null;
}
public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, FileContent fileContent) throws Exception {
public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) 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());
}
final EngineContext engineContext = new EngineContext();
engineContext.setTestId(loadTest.getId());
engineContext.setTestName(loadTest.getName());
@ -48,12 +61,25 @@ public class EngineFactory {
final EngineSourceParser engineSourceParser = EngineSourceParserFactory.createEngineSourceParser(engineContext.getFileType());
if (engineSourceParser == null) {
MSException.throwException("未知的文件类型!");
MSException.throwException("File type unknown");
}
String content = engineSourceParser.parse(engineContext, new ByteArrayInputStream(fileContent.getFile()));
engineContext.setContent(content);
if (CollectionUtils.isNotEmpty(csvFiles)) {
csvFiles.forEach(cf -> {
FileContent csvContent = fileService.getFileContent(cf.getId());
engineContext.addProperty(cf.getName(), new String(csvContent.getFile()));
});
}
return engineContext;
}
@Resource
private void setFileService(FileService fileService) {
EngineFactory.fileService = fileService;
}
}

View File

@ -126,15 +126,15 @@ public class LoadTestService {
public String edit(EditTestPlanRequest request, List<MultipartFile> files) {
// 新选择了一个文件删除原来的文件
if (files != null) {
List<FileMetadata> updatedFiles = request.getUpdatedFileList();
List<FileMetadata> originFiles = fileService.getFileMetadataByTestId(request.getId());
List<String> updatedFileIds = updatedFiles.stream().map(FileMetadata::getId).collect(Collectors.toList());
List<String> originFileIds = originFiles.stream().map(FileMetadata::getId).collect(Collectors.toList());
// 相减
List<String> deleteFileIds = ListUtils.subtract(originFileIds, updatedFileIds);
fileService.deleteFileByIds(deleteFileIds);
List<FileMetadata> updatedFiles = request.getUpdatedFileList();
List<FileMetadata> originFiles = fileService.getFileMetadataByTestId(request.getId());
List<String> updatedFileIds = updatedFiles.stream().map(FileMetadata::getId).collect(Collectors.toList());
List<String> originFileIds = originFiles.stream().map(FileMetadata::getId).collect(Collectors.toList());
// 相减
List<String> deleteFileIds = ListUtils.subtract(originFileIds, updatedFileIds);
fileService.deleteFileByIds(deleteFileIds);
if (files != null) {
files.forEach(file -> {
final FileMetadata fileMetadata = saveFile(file);
LoadTestFile loadTestFile = new LoadTestFile();
@ -171,29 +171,24 @@ public class LoadTestService {
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(), "JMX"))
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());
});
final FileContent fileContent = fileService.getFileContent(fileMetadata.getId());
if (fileContent == null) {
MSException.throwException(Translator.get("run_load_test_file_content_not_found") + request.getId());
}
List<FileMetadata> csvFiles = fileMetadataList.stream().filter(f -> StringUtils.equalsIgnoreCase(f.getType(), FileType.CSV.name())).collect(Collectors.toList());
LogUtil.info("Load test started " + loadTest.getName());
// engine type (DOCKER|KUBERNETES)
// todo set type
final Engine engine = EngineFactory.createEngine(fileMetadata.getEngine());
if (engine == null) {
MSException.throwException(String.format("Test cannot be runtest ID%sfile type%s",
request.getId(),
fileMetadata.getType()));
MSException.throwException(String.format("Test cannot be runtest ID%sfile type%s", request.getId(), fileMetadata.getType()));
}
boolean init = true;
try {
init = engine.init(EngineFactory.createContext(loadTest, fileMetadata, fileContent));
init = engine.init(EngineFactory.createContext(loadTest, fileMetadata, csvFiles));
} catch (Exception e) {
MSException.throwException(e);
}