report save

This commit is contained in:
Captain.B 2020-04-01 12:57:08 +08:00
parent 812328b907
commit 326c1aff4b
9 changed files with 38 additions and 10 deletions

View File

@ -1,5 +1,5 @@
package io.metersphere.commons.constants;
public enum TestStatus {
Running, Completed
Starting, Running, Completed
}

View File

@ -22,6 +22,7 @@ public abstract class AbstractEngine implements Engine {
public static final String REGISTRY = "registry.fit2cloud.com/metersphere/";
public static final String JMETER_IMAGE = "jmeter-master:0.0.2";
private Long startTime;
protected LoadTestWithBLOBs loadTest;
protected LoadTestService loadTestService;
protected Integer threadNum;
@ -33,6 +34,7 @@ public abstract class AbstractEngine implements Engine {
public AbstractEngine() {
testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class);
testResourceService = CommonBeanFactory.getBean(TestResourceService.class);
this.startTime = System.currentTimeMillis();
}
protected void init(LoadTestWithBLOBs loadTest) {
@ -83,4 +85,8 @@ public abstract class AbstractEngine implements Engine {
}
return s;
}
public Long getStartTime() {
return startTime;
}
}

View File

@ -1,6 +1,7 @@
package io.metersphere.engine;
public interface Engine {
Long getStartTime();
void start();

View File

@ -11,6 +11,7 @@ public class EngineContext {
private String content;
private String resourcePoolId;
private Long threadNum;
private Long startTime;
private Map<String, Object> properties = new HashMap<>();
private Map<String, String> testData = new HashMap<>();
@ -89,4 +90,12 @@ public class EngineContext {
public void setThreadNum(Long threadNum) {
this.threadNum = threadNum;
}
public Long getStartTime() {
return startTime;
}
public void setStartTime(Long startTime) {
this.startTime = startTime;
}
}

View File

@ -54,7 +54,7 @@ public class EngineFactory {
return null;
}
public static EngineContext createContext(LoadTestWithBLOBs loadTest, long threadNum) throws Exception {
public static EngineContext createContext(LoadTestWithBLOBs loadTest, long threadNum, long startTime) 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());
@ -76,6 +76,7 @@ public class EngineFactory {
engineContext.setFileType(jmxFile.getType());
engineContext.setThreadNum(threadNum);
engineContext.setResourcePoolId(loadTest.getTestResourcePoolId());
engineContext.setStartTime(startTime);
if (StringUtils.isNotEmpty(loadTest.getLoadConfiguration())) {
final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration());

View File

@ -62,7 +62,7 @@ public class DockerTestEngine extends AbstractEngine {
// todo 运行测试
EngineContext context = null;
try {
context = EngineFactory.createContext(loadTest, realThreadNum);
context = EngineFactory.createContext(loadTest, realThreadNum, this.getStartTime());
} catch (Exception e) {
MSException.throwException(e);
}

View File

@ -46,7 +46,7 @@ public class KubernetesTestEngine extends AbstractEngine {
MSException.throwException("Insufficient resources");
}
try {
EngineContext context = EngineFactory.createContext(loadTest, threadNum);
EngineContext context = EngineFactory.createContext(loadTest, threadNum, this.getStartTime());
runTest(context, clientCredential);
} catch (Exception e) {
MSException.throwException(e);

View File

@ -570,7 +570,7 @@ public class JmeterDocumentParser implements DocumentParser {
// 添加关联关系 test.id test.name test.startTime
collectionProp.appendChild(createKafkaProp(document, "test.id", context.getTestId()));
collectionProp.appendChild(createKafkaProp(document, "test.name", context.getTestName()));
collectionProp.appendChild(createKafkaProp(document, "test.startTime", "" + System.currentTimeMillis()));
collectionProp.appendChild(createKafkaProp(document, "test.startTime", context.getStartTime().toString()));
elementProp.appendChild(collectionProp);
// set elementProp

View File

@ -1,10 +1,7 @@
package io.metersphere.service;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.FileContentMapper;
import io.metersphere.base.mapper.FileMetadataMapper;
import io.metersphere.base.mapper.LoadTestFileMapper;
import io.metersphere.base.mapper.LoadTestMapper;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtLoadTestMapper;
import io.metersphere.commons.constants.FileType;
import io.metersphere.commons.constants.TestStatus;
@ -31,6 +28,8 @@ import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
public class LoadTestService {
private static final String HEADERS = "timestamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect";
@Resource
private LoadTestMapper loadTestMapper;
@Resource
@ -44,7 +43,7 @@ public class LoadTestService {
@Resource
private FileService fileService;
@Resource
private TestResourcePoolService testResourcePoolService;
private LoadTestReportMapper loadTestReportMapper;
public List<LoadTestDTO> list(QueryTestPlanRequest request) {
return extLoadTestMapper.list(request);
@ -182,6 +181,18 @@ public class LoadTestService {
// 标记running状态
loadTest.setStatus(TestStatus.Running.name());
loadTestMapper.updateByPrimaryKeySelective(loadTest);
LoadTestReport testReport = new LoadTestReport();
testReport.setId(UUID.randomUUID().toString());
testReport.setCreateTime(engine.getStartTime());
testReport.setUpdateTime(engine.getStartTime());
testReport.setTestId(loadTest.getId());
testReport.setName(loadTest.getName());
testReport.setContent(HEADERS);
testReport.setStatus(TestStatus.Starting.name());
loadTestReportMapper.insertSelective(testReport);
// todo通过调用stop方法能够停止正在运行的engine但是如果部署了多个backend实例页面发送的停止请求如何定位到具体的engine
}