From a5bc32ff57afe3e50c5f693fb4dd87cbc80352f3 Mon Sep 17 00:00:00 2001 From: q4speed Date: Fri, 22 May 2020 16:45:08 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=B9=B6=E8=A1=8C=E8=BF=90=E8=A1=8C=E7=9A=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/jmeter/APIBackendListenerClient.java | 140 +++++++++--------- .../api/jmeter/ScenarioResult.java | 2 - .../components/api/report/ApiReportView.vue | 9 +- .../components/api/test/ApiTestConfig.vue | 8 +- .../api/test/components/ApiRequestConfig.vue | 2 +- .../api/test/components/ApiScenarioConfig.vue | 2 +- .../business/components/api/test/model/JMX.js | 26 ++-- .../api/test/model/ScenarioModel.js | 65 +++----- 8 files changed, 111 insertions(+), 143 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java index ff556e6a0e..96c6be1038 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -20,92 +20,88 @@ import java.util.concurrent.ConcurrentHashMap; */ public class APIBackendListenerClient extends AbstractBackendListenerClient implements Serializable { - // 与前端JMXGenerator的SPLIT对应,用于获取 测试名称 和 测试ID - private final static String SPLIT = "@@:"; - // 测试ID作为key - private final Map> queue = new ConcurrentHashMap<>(); + private final static String THREAD_SPLIT = " "; + + private final static String ID_SPLIT = "-"; + + private final static String TEST_ID = "id"; + + private final List queue = new ArrayList<>(); + + private APITestService apiTestService; + + private APIReportService apiReportService; + + // 测试ID + private String id; + + @Override + public void setupTest(BackendListenerContext context) throws Exception { + this.id = context.getParameter(TEST_ID); + apiTestService = CommonBeanFactory.getBean(APITestService.class); + if (apiTestService == null) { + LogUtil.error("apiTestService is required"); + } + + apiReportService = CommonBeanFactory.getBean(APIReportService.class); + if (apiReportService == null) { + LogUtil.error("apiReportService is required"); + } + } @Override public void handleSampleResults(List sampleResults, BackendListenerContext context) { - sampleResults.forEach(result -> { - // 将不同的测试脚本按测试ID分开 - String label = result.getSampleLabel(); - if (!label.contains(SPLIT)) { - LogUtil.error("request name format is invalid, name: " + label); - return; - } - String name = label.split(SPLIT)[0]; - String testId = label.split(SPLIT)[1]; - if (!queue.containsKey(testId)) { - List testResults = new ArrayList<>(); - queue.put(testId, testResults); - } - result.setSampleLabel(name); - queue.get(testId).add(result); - }); + queue.addAll(sampleResults); } @Override public void teardownTest(BackendListenerContext context) throws Exception { - APITestService apiTestService = CommonBeanFactory.getBean(APITestService.class); - if (apiTestService == null) { - LogUtil.error("apiTestService is required"); - return; - } + TestResult testResult = new TestResult(); + testResult.setTestId(id); + testResult.setTotal(queue.size()); - APIReportService apiReportService = CommonBeanFactory.getBean(APIReportService.class); - if (apiReportService == null) { - LogUtil.error("apiReportService is required"); - return; - } + // 一个脚本里可能包含多个场景(ThreadGroup),所以要区分开,key: 场景Id + final Map scenarios = new LinkedHashMap<>(); - queue.forEach((id, sampleResults) -> { - TestResult testResult = new TestResult(); - testResult.setTestId(id); - testResult.setTotal(sampleResults.size()); + queue.forEach(result -> { + // 线程名称: <场景名> <场景Index>-<请求Index>, 例如:Scenario 2-1 + String scenarioName = StringUtils.substringBeforeLast(result.getThreadName(), THREAD_SPLIT); + String index = StringUtils.substringAfterLast(result.getThreadName(), THREAD_SPLIT); + String scenarioId = StringUtils.substringBefore(index, ID_SPLIT); + ScenarioResult scenarioResult; + if (!scenarios.containsKey(scenarioId)) { + scenarioResult = new ScenarioResult(); + scenarioResult.setId(scenarioId); + scenarioResult.setName(scenarioName); + scenarios.put(scenarioId, scenarioResult); + } else { + scenarioResult = scenarios.get(scenarioId); + } - // key: 场景Id - final Map scenarios = new LinkedHashMap<>(); + if (result.isSuccessful()) { + scenarioResult.addSuccess(); + testResult.addSuccess(); + } else { + scenarioResult.addError(result.getErrorCount()); + testResult.addError(result.getErrorCount()); + } - sampleResults.forEach(result -> { - String thread = StringUtils.substringBeforeLast(result.getThreadName(), " "); - String order = StringUtils.substringAfterLast(result.getThreadName(), " "); - String scenarioName = StringUtils.substringBefore(thread, SPLIT); - String scenarioId = StringUtils.substringAfter(thread, SPLIT); - ScenarioResult scenarioResult; - if (!scenarios.containsKey(scenarioId)) { - scenarioResult = new ScenarioResult(); - scenarioResult.setId(scenarioId); - scenarioResult.setName(scenarioName); - scenarioResult.setOrder(StringUtils.substringBefore(order, "-")); - scenarios.put(scenarioId, scenarioResult); - } else { - scenarioResult = scenarios.get(scenarioId); - } + RequestResult requestResult = getRequestResult(result); + scenarioResult.getRequestResults().add(requestResult); + scenarioResult.addResponseTime(result.getTime()); - if (result.isSuccessful()) { - scenarioResult.addSuccess(); - testResult.addSuccess(); - } else { - scenarioResult.addError(result.getErrorCount()); - testResult.addError(result.getErrorCount()); - } + testResult.addPassAssertions(requestResult.getPassAssertions()); + testResult.addTotalAssertions(requestResult.getTotalAssertions()); - RequestResult requestResult = getRequestResult(result); - scenarioResult.getRequestResults().add(requestResult); - scenarioResult.addResponseTime(result.getTime()); - - testResult.addPassAssertions(requestResult.getPassAssertions()); - testResult.addTotalAssertions(requestResult.getTotalAssertions()); - - scenarioResult.addPassAssertions(requestResult.getPassAssertions()); - scenarioResult.addTotalAssertions(requestResult.getTotalAssertions()); - }); - testResult.getScenarios().addAll(scenarios.values()); - testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getOrder)); - apiTestService.changeStatus(id, APITestStatus.Completed); - apiReportService.complete(testResult); + scenarioResult.addPassAssertions(requestResult.getPassAssertions()); + scenarioResult.addTotalAssertions(requestResult.getTotalAssertions()); }); + + testResult.getScenarios().addAll(scenarios.values()); + testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); + apiTestService.changeStatus(id, APITestStatus.Completed); + apiReportService.complete(testResult); + queue.clear(); super.teardownTest(context); } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java b/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java index d9f2b7d4ca..331b9b8ab1 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java @@ -12,8 +12,6 @@ public class ScenarioResult { private String name; - private String order; - private long responseTime; private int error = 0; diff --git a/frontend/src/business/components/api/report/ApiReportView.vue b/frontend/src/business/components/api/report/ApiReportView.vue index 08d6aa1bf8..d2f8361e5e 100644 --- a/frontend/src/business/components/api/report/ApiReportView.vue +++ b/frontend/src/business/components/api/report/ApiReportView.vue @@ -1,5 +1,5 @@