This commit is contained in:
chenjianxing 2020-04-02 09:30:43 +08:00
commit c74d4fc3c8
6 changed files with 51 additions and 78 deletions

View File

@ -4,6 +4,7 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.commons.constants.RoleConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.PageUtils;
import io.metersphere.commons.utils.Pager;
import io.metersphere.controller.request.testplan.*;
@ -85,7 +86,10 @@ public class LoadTestController {
@PostMapping("/run")
public void run(@RequestBody RunTestPlanRequest request) {
loadTestService.run(request);
boolean started = loadTestService.run(request);
if (!started) {
MSException.throwException("Start engine error, please check log.");
}
}
@GetMapping("/file/metadata/{testId}")

View File

@ -89,7 +89,7 @@ public class DockerTestEngine extends AbstractEngine {
for (int i = 0; i < containerList.size(); i++) {
HashMap h = (HashMap) containerList.get(i);
if (StringUtils.equals((String) h.get("State"), "running")) {
MSException.throwException("the test is running!");
MSException.throwException("The test is running!");
}
}

View File

@ -1,8 +1,10 @@
package io.metersphere.report;
import com.alibaba.fastjson.JSONObject;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import io.metersphere.commons.exception.MSException;
import io.metersphere.report.base.*;
import io.metersphere.report.dto.ErrorsTop5DTO;
import io.metersphere.report.dto.RequestStatisticsDTO;
@ -289,12 +291,13 @@ public class JtlResolver {
ChartsData data = new ChartsData();
List<Metric> total = JtlResolver.resolver(jtlString);
////
List<String> users = new ArrayList<>();
List<String> hits = new ArrayList<>();
List<String> erorrs = new ArrayList<>();
List<String> timeList = new ArrayList<>();
//// todo SimpleDateFormat
Map<String, Object> resultMap = new HashMap<>();
// todo SimpleDateFormat
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DecimalFormat df = new DecimalFormat("0.0");
@ -340,11 +343,12 @@ public class JtlResolver {
}
data.setTime(timeList);
data.setUsers(users);
data.setHits(hits);
data.setErrors(erorrs);
resultMap.put("users", users);
resultMap.put("hits", hits);
resultMap.put("errors", erorrs);
JSONObject serices = new JSONObject(resultMap);
data.setxAxis(StringUtils.join(",", timeList));
data.setSerices(serices.toString());
return data;
}

View File

@ -1,20 +1,10 @@
package io.metersphere.report.base;
import java.util.List;
public class ChartsData {
private String xAxis;
private String yAxis;
private String yAxis1;
private String serices;
////
private List<String> time;
private List<String> users;
private List<String> hits;
private List<String> errors;
public String getxAxis() {
return xAxis;
}
@ -23,22 +13,6 @@ public class ChartsData {
this.xAxis=xAxis;
}
public String getyAxis() {
return yAxis;
}
public void setyAxis(String yAxis) {
this.yAxis = yAxis;
}
public String getyAxis1() {
return yAxis1;
}
public void setyAxis1(String yAxis1) {
this.yAxis1 = yAxis1;
}
public String getSerices() {
return serices;
}
@ -46,36 +20,4 @@ public class ChartsData {
public void setSerices(String serices) {
this.serices=serices;
}
public List<String> getTime() {
return time;
}
public void setTime(List<String> time) {
this.time=time;
}
public List<String> getUsers() {
return users;
}
public void setUsers(List<String> users) {
this.users=users;
}
public List<String> getHits() {
return hits;
}
public void setHits(List<String> hits) {
this.hits=hits;
}
public List<String> getErrors() {
return errors;
}
public void setErrors(List<String> errors) {
this.errors=errors;
}
}

View File

@ -14,6 +14,7 @@ import io.metersphere.engine.Engine;
import io.metersphere.engine.EngineFactory;
import io.metersphere.i18n.Translator;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
@ -163,12 +164,13 @@ public class LoadTestService {
return request.getId();
}
public void run(RunTestPlanRequest request) {
public boolean run(RunTestPlanRequest request) {
final LoadTestWithBLOBs loadTest = loadTestMapper.selectByPrimaryKey(request.getId());
if (loadTest == null) {
MSException.throwException(Translator.get("run_load_test_not_found") + request.getId());
}
if (TestStatus.Running.name().equals(loadTest.getStatus())) {
if (StringUtils.equalsAny(loadTest.getStatus(), TestStatus.Running.name(), TestStatus.Starting.name())) {
MSException.throwException(Translator.get("load_test_is_running"));
}
@ -178,14 +180,21 @@ public class LoadTestService {
if (engine == null) {
MSException.throwException(String.format("Test cannot be runtest ID%s", request.getId()));
}
return startEngine(loadTest, engine);
// todo通过调用stop方法能够停止正在运行的engine但是如果部署了多个backend实例页面发送的停止请求如何定位到具体的engine
}
private boolean startEngine(LoadTestWithBLOBs loadTest, Engine engine) {
LoadTestReportWithBLOBs testReport = new LoadTestReportWithBLOBs();
testReport.setId(engine.getReportId());
testReport.setCreateTime(engine.getStartTime());
testReport.setUpdateTime(engine.getStartTime());
testReport.setTestId(loadTest.getId());
testReport.setName(loadTest.getName());
// 启动测试
boolean started = true;
try {
engine.start();
// 标记running状态
@ -199,6 +208,9 @@ public class LoadTestService {
extLoadTestReportMapper.appendLine(testReport.getId(), "\n");
} catch (Exception e) {
LogUtil.error(e);
started = false;
loadTest.setStatus(TestStatus.Error.name());
loadTestMapper.updateByPrimaryKeySelective(loadTest);
//
@ -206,7 +218,7 @@ public class LoadTestService {
testReport.setDescription(e.getMessage());
loadTestReportMapper.insertSelective(testReport);
}
// todo通过调用stop方法能够停止正在运行的engine但是如果部署了多个backend实例页面发送的停止请求如何定位到具体的engine
return started;
}
public List<LoadTestDTO> recentTestPlans(QueryTestPlanRequest request) {

View File

@ -132,6 +132,16 @@
this.option = this.generateOption(data);
})
},
_objToStrMap(obj){
let strMap = new Map();
for (let k of Object.keys(obj)) {
strMap.set(k,obj[k]);
}
return strMap;
},
_jsonToMap(jsonStr){
return this._objToStrMap(JSON.parse(jsonStr));
},
generateOption(data) {
let option = {
legend: {
@ -174,11 +184,12 @@
}
]
}
this.$set(option.xAxis, "data", data.time);
this.$set(option.series[0], "data", data.users);
this.$set(option.series[1], "data", data.hits);
this.$set(option.series[2], "data", data.errors);
let map = this._jsonToMap(data.serices);
let xAxis = data.xAxis;
this.$set(option.xAxis, "data", xAxis.split(','));
this.$set(option.series[0], "data", map.get("users"));
this.$set(option.series[1], "data", map.get("hits"));
this.$set(option.series[2], "data", map.get("errors"));
return option;
}
},