From 16168b24b9e7fbf09e6e3902a536a05f446a71b5 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 1 Apr 2020 16:23:51 +0800 Subject: [PATCH 1/2] load test status --- .../controller/LoadTestController.java | 6 +++++- .../engine/docker/DockerTestEngine.java | 2 +- .../metersphere/service/LoadTestService.java | 20 +++++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/io/metersphere/controller/LoadTestController.java b/backend/src/main/java/io/metersphere/controller/LoadTestController.java index 5b1466e248..ef7cd910de 100644 --- a/backend/src/main/java/io/metersphere/controller/LoadTestController.java +++ b/backend/src/main/java/io/metersphere/controller/LoadTestController.java @@ -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}") diff --git a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java index 7a5789affb..df7e35b3ec 100644 --- a/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/engine/docker/DockerTestEngine.java @@ -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!"); } } diff --git a/backend/src/main/java/io/metersphere/service/LoadTestService.java b/backend/src/main/java/io/metersphere/service/LoadTestService.java index e9669e6ad7..0dcb7e04e3 100644 --- a/backend/src/main/java/io/metersphere/service/LoadTestService.java +++ b/backend/src/main/java/io/metersphere/service/LoadTestService.java @@ -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 run,test 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 recentTestPlans(QueryTestPlanRequest request) { From 3c6ed58fabbf84ea6c21a894e014f8366837cbf3 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Wed, 1 Apr 2020 17:34:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=A6=82=E8=A7=88LoadChart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/report/JtlResolver.java | 18 +++--- .../metersphere/report/base/ChartsData.java | 62 +------------------ .../report/components/TestOverview.vue | 21 +++++-- 3 files changed, 29 insertions(+), 72 deletions(-) diff --git a/backend/src/main/java/io/metersphere/report/JtlResolver.java b/backend/src/main/java/io/metersphere/report/JtlResolver.java index e15dd506b9..f543dca386 100644 --- a/backend/src/main/java/io/metersphere/report/JtlResolver.java +++ b/backend/src/main/java/io/metersphere/report/JtlResolver.java @@ -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 total = JtlResolver.resolver(jtlString); - //// List users = new ArrayList<>(); List hits = new ArrayList<>(); List erorrs = new ArrayList<>(); List timeList = new ArrayList<>(); - //// todo SimpleDateFormat + Map 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; } diff --git a/backend/src/main/java/io/metersphere/report/base/ChartsData.java b/backend/src/main/java/io/metersphere/report/base/ChartsData.java index 3f304da10c..3f4768adbc 100644 --- a/backend/src/main/java/io/metersphere/report/base/ChartsData.java +++ b/backend/src/main/java/io/metersphere/report/base/ChartsData.java @@ -1,42 +1,16 @@ 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 time; - private List users; - private List hits; - private List errors; - public String getxAxis() { return xAxis; } public void setxAxis(String xAxis) { - 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; + this.xAxis=xAxis; } public String getSerices() { @@ -44,38 +18,6 @@ public class ChartsData { } public void setSerices(String serices) { - this.serices = serices; - } - - public List getTime() { - return time; - } - - public void setTime(List time) { - this.time=time; - } - - public List getUsers() { - return users; - } - - public void setUsers(List users) { - this.users=users; - } - - public List getHits() { - return hits; - } - - public void setHits(List hits) { - this.hits=hits; - } - - public List getErrors() { - return errors; - } - - public void setErrors(List errors) { - this.errors=errors; + this.serices=serices; } } diff --git a/frontend/src/business/components/performance/report/components/TestOverview.vue b/frontend/src/business/components/performance/report/components/TestOverview.vue index 8bc18c1c7e..f9f76bafb4 100644 --- a/frontend/src/business/components/performance/report/components/TestOverview.vue +++ b/frontend/src/business/components/performance/report/components/TestOverview.vue @@ -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; } },