启动测试

This commit is contained in:
Captain.B 2020-03-25 10:01:24 +08:00
parent c4cfae5bae
commit e1a1175cc9
5 changed files with 38 additions and 30 deletions

View File

@ -7,10 +7,13 @@ import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.base.domain.TestResource; import io.metersphere.base.domain.TestResource;
import io.metersphere.base.domain.TestResourcePool; import io.metersphere.base.domain.TestResourcePool;
import io.metersphere.commons.constants.FileType;
import io.metersphere.commons.constants.ResourcePoolTypeEnum; import io.metersphere.commons.constants.ResourcePoolTypeEnum;
import io.metersphere.commons.constants.TestStatus; import io.metersphere.commons.constants.TestStatus;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory; 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.LoadTestService;
import io.metersphere.service.TestResourcePoolService; import io.metersphere.service.TestResourcePoolService;
import io.metersphere.service.TestResourceService; import io.metersphere.service.TestResourceService;
@ -18,6 +21,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public abstract class AbstractEngine implements Engine { public abstract class AbstractEngine implements Engine {
protected FileMetadata jmxFile; protected FileMetadata jmxFile;
@ -27,13 +31,35 @@ public abstract class AbstractEngine implements Engine {
protected Integer threadNum; protected Integer threadNum;
protected List<TestResource> resourceList; protected List<TestResource> 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 @Override
public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) { public boolean init(LoadTestWithBLOBs loadTest) {
if (loadTest == null) {
MSException.throwException("LoadTest is null.");
}
this.loadTest = loadTest; this.loadTest = loadTest;
this.jmxFile = fileMetadata;
this.csvFiles = csvFiles; final List<FileMetadata> fileMetadataList = fileService.getFileMetadataByTestId(loadTest.getId());
TestResourcePoolService testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class); if (org.springframework.util.CollectionUtils.isEmpty(fileMetadataList)) {
TestResourceService testResourceService = CommonBeanFactory.getBean(TestResourceService.class); 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); this.loadTestService = CommonBeanFactory.getBean(LoadTestService.class);
threadNum = getThreadNum(loadTest); threadNum = getThreadNum(loadTest);

View File

@ -1,12 +1,9 @@
package io.metersphere.engine; package io.metersphere.engine;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.LoadTestWithBLOBs;
import java.util.List;
public interface Engine { public interface Engine {
boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles); boolean init(LoadTestWithBLOBs loadTest);
void start(); void start();

View File

@ -1,7 +1,6 @@
package io.metersphere.engine.docker; package io.metersphere.engine.docker;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.commons.constants.ResourceStatusEnum; import io.metersphere.commons.constants.ResourceStatusEnum;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
@ -24,8 +23,8 @@ public class DockerTestEngine extends AbstractEngine {
@Override @Override
public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) { public boolean init(LoadTestWithBLOBs loadTest) {
super.init(loadTest, fileMetadata, csvFiles); super.init(loadTest);
this.restTemplate = CommonBeanFactory.getBean(RestTemplate.class); this.restTemplate = CommonBeanFactory.getBean(RestTemplate.class);
// todo 初始化操作 // todo 初始化操作
return true; return true;

View File

@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON;
import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
@ -18,13 +17,12 @@ import io.metersphere.engine.kubernetes.provider.KubernetesProvider;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
public class KubernetesTestEngine extends AbstractEngine { public class KubernetesTestEngine extends AbstractEngine {
@Override @Override
public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) { public boolean init(LoadTestWithBLOBs loadTest) {
super.init(loadTest, fileMetadata, csvFiles); super.init(loadTest);
return true; return true;
} }

View File

@ -16,7 +16,6 @@ import io.metersphere.engine.Engine;
import io.metersphere.engine.EngineFactory; import io.metersphere.engine.EngineFactory;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import org.apache.commons.collections4.ListUtils; import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -166,27 +165,16 @@ public class LoadTestService {
MSException.throwException(Translator.get("run_load_test_not_found") + request.getId()); MSException.throwException(Translator.get("run_load_test_not_found") + request.getId());
} }
final List<FileMetadata> 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<FileMetadata> csvFiles = fileMetadataList.stream().filter(f -> StringUtils.equalsIgnoreCase(f.getType(), FileType.CSV.name())).collect(Collectors.toList());
LogUtil.info("Load test started " + loadTest.getName()); LogUtil.info("Load test started " + loadTest.getName());
// engine type (NODE|K8S) // engine type (NODE|K8S)
final Engine engine = EngineFactory.createEngine(loadTest); final Engine engine = EngineFactory.createEngine(loadTest);
if (engine == null) { 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%s", request.getId()));
} }
boolean init = true; boolean init = true;
try { try {
init = engine.init(loadTest, fileMetadata, csvFiles); init = engine.init(loadTest);
} catch (Exception e) { } catch (Exception e) {
MSException.throwException(e); MSException.throwException(e);
} }