From e736019e8f57c12f39cddff05bd3fc257d62fda2 Mon Sep 17 00:00:00 2001 From: q4speed Date: Mon, 25 May 2020 15:22:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=90=8E=E5=8F=B0=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0BackendListener=EF=BC=8C=E4=BF=9D=E8=AF=81=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E7=94=9F=E6=88=90=E7=9A=84jmx=E7=8B=AC=E7=AB=8B?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/jmeter/APIBackendListenerClient.java | 14 +++++++------- .../io/metersphere/api/jmeter/JMeterService.java | 15 ++++++++++++++- .../metersphere/api/service/APITestService.java | 2 +- .../components/api/test/model/ScenarioModel.js | 7 ------- 4 files changed, 22 insertions(+), 16 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 96c6be1038..2b88720d76 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -13,19 +13,18 @@ import org.apache.jmeter.visualizers.backend.BackendListenerContext; import java.io.Serializable; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; /** * JMeter BackendListener扩展, jmx脚本中使用 */ public class APIBackendListenerClient extends AbstractBackendListenerClient implements Serializable { + public final static String TEST_ID = "ms.test.id"; + 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; @@ -33,11 +32,11 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl private APIReportService apiReportService; // 测试ID - private String id; + private String testId; @Override public void setupTest(BackendListenerContext context) throws Exception { - this.id = context.getParameter(TEST_ID); + this.testId = context.getParameter(TEST_ID); apiTestService = CommonBeanFactory.getBean(APITestService.class); if (apiTestService == null) { LogUtil.error("apiTestService is required"); @@ -47,6 +46,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl if (apiReportService == null) { LogUtil.error("apiReportService is required"); } + super.setupTest(context); } @Override @@ -57,7 +57,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl @Override public void teardownTest(BackendListenerContext context) throws Exception { TestResult testResult = new TestResult(); - testResult.setTestId(id); + testResult.setTestId(testId); testResult.setTotal(queue.size()); // 一个脚本里可能包含多个场景(ThreadGroup),所以要区分开,key: 场景Id @@ -99,7 +99,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl testResult.getScenarios().addAll(scenarios.values()); testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); - apiTestService.changeStatus(id, APITestStatus.Completed); + apiTestService.changeStatus(testId, APITestStatus.Completed); apiReportService.complete(testResult); queue.clear(); diff --git a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java index bc5207fa0a..ceeeab9ed0 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java @@ -3,8 +3,10 @@ package io.metersphere.api.jmeter; import io.metersphere.commons.exception.MSException; import io.metersphere.config.JmeterProperties; import io.metersphere.i18n.Translator; +import org.apache.jmeter.config.Arguments; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.util.JMeterUtils; +import org.apache.jmeter.visualizers.backend.BackendListener; import org.apache.jorphan.collections.HashTree; import org.springframework.stereotype.Service; @@ -19,7 +21,7 @@ public class JMeterService { @Resource private JmeterProperties jmeterProperties; - public void run(InputStream is) { + public void run(String testId, InputStream is) { String JMETER_HOME = jmeterProperties.getHome(); String JMETER_PROPERTIES = JMETER_HOME + "/bin/jmeter.properties"; JMeterUtils.loadJMeterProperties(JMETER_PROPERTIES); @@ -27,6 +29,7 @@ public class JMeterService { try { Object scriptWrapper = SaveService.loadElement(is); HashTree testPlan = getHashTree(scriptWrapper); + addBackendListener(testId, testPlan); LocalRunner runner = new LocalRunner(testPlan); runner.run(); @@ -40,4 +43,14 @@ public class JMeterService { field.setAccessible(true); return (HashTree) field.get(scriptWrapper); } + + private void addBackendListener(String testId, HashTree testPlan) { + BackendListener backendListener = new BackendListener(); + backendListener.setName(testId); + Arguments arguments = new Arguments(); + arguments.addArgument(APIBackendListenerClient.TEST_ID, testId); + backendListener.setArguments(arguments); + backendListener.setClassname(APIBackendListenerClient.class.getCanonicalName()); + testPlan.add(testPlan.getArray()[0], backendListener); + } } diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index db88c9dd11..8b731ab3ee 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -90,7 +90,7 @@ public class APITestService { String reportId = apiReportService.create(get(request.getId())); changeStatus(request.getId(), APITestStatus.Running); - jMeterService.run(is); + jMeterService.run(request.getId(), is); return reportId; } diff --git a/frontend/src/business/components/api/test/model/ScenarioModel.js b/frontend/src/business/components/api/test/model/ScenarioModel.js index fdeb504d0f..297f6b81b3 100644 --- a/frontend/src/business/components/api/test/model/ScenarioModel.js +++ b/frontend/src/business/components/api/test/model/ScenarioModel.js @@ -386,7 +386,6 @@ class JMXGenerator { let testPlan = new TestPlan(test.name); this.addScenarios(testPlan, test.scenarioDefinition); - this.addBackendListener(testPlan, test.id); this.jmeterTestPlan = new JMeterTestPlan(); this.jmeterTestPlan.put(testPlan); @@ -424,12 +423,6 @@ class JMXGenerator { }) } - addBackendListener(testPlan, testId) { - let className = 'io.metersphere.api.jmeter.APIBackendListenerClient'; - let args = [new KeyValue("id", testId)]; - testPlan.put(new BackendListener(testId, className, args)); - } - addScenarioVariables(threadGroup, scenario) { let args = this.replaceKV(scenario.variables); if (args.length > 0) {