From c3359328ce5774fba19e4c7ae5234495cd4aa013 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Wed, 18 Aug 2021 16:15:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20APIBackendListenerClient=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/jmeter/APIBackendListenerClient.java | 32 +++++++--- .../api/jmeter/APIBackendListenerHandler.java | 58 ++++--------------- 2 files changed, 34 insertions(+), 56 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 b3c8122d06..837953783f 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -1,12 +1,14 @@ package io.metersphere.api.jmeter; - +import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.utils.CommonBeanFactory; +import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient; import org.apache.jmeter.visualizers.backend.BackendListenerContext; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; /** @@ -16,27 +18,41 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl public final static String TEST_ID = "ms.test.id"; + public String runMode = ApiRunMode.RUN.name(); + + private final List queue = new ArrayList<>(); + + // 测试ID + private String testId; + + private String debugReportId; + @Override public void setupTest(BackendListenerContext context) throws Exception { - APIBackendListenerHandler apiBackendListenerHandler = - CommonBeanFactory.getBean(APIBackendListenerHandler.class); - apiBackendListenerHandler.handleSetupTest(context); + setParam(context); super.setupTest(context); } @Override public void handleSampleResults(List sampleResults, BackendListenerContext context) { - APIBackendListenerHandler apiBackendListenerHandler = - CommonBeanFactory.getBean(APIBackendListenerHandler.class); - apiBackendListenerHandler.handleSampleResults(sampleResults); + queue.addAll(sampleResults); } @Override public void teardownTest(BackendListenerContext context) throws Exception { APIBackendListenerHandler apiBackendListenerHandler = CommonBeanFactory.getBean(APIBackendListenerHandler.class); - apiBackendListenerHandler.handleTeardownTest(); + apiBackendListenerHandler.handleTeardownTest(queue, this.runMode, this.testId, this.debugReportId); super.teardownTest(context); } + private void setParam(BackendListenerContext context) { + this.testId = context.getParameter(APIBackendListenerClient.TEST_ID); + this.runMode = context.getParameter("runMode"); + this.debugReportId = context.getParameter("debugReportId"); + if (StringUtils.isBlank(this.runMode)) { + this.runMode = ApiRunMode.RUN.name(); + } + } + } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerHandler.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerHandler.java index 4e34729c9c..4b14129810 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerHandler.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerHandler.java @@ -5,16 +5,16 @@ import io.metersphere.api.dto.RunningParamKeys; import io.metersphere.api.service.ApiEnvironmentRunningParamService; import io.metersphere.api.service.MsResultService; import io.metersphere.api.service.TestResultService; -import io.metersphere.commons.constants.ApiRunMode; -import io.metersphere.commons.utils.CommonBeanFactory; -import io.metersphere.commons.utils.LogUtil; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.samplers.SampleResult; -import org.apache.jmeter.visualizers.backend.BackendListenerContext; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.*; +import javax.annotation.Resource; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; /** * 获取结果和数据库操作分离 @@ -24,43 +24,14 @@ import java.util.*; @Transactional(rollbackFor = Exception.class) public class APIBackendListenerHandler { - private final List queue = new ArrayList<>(); - + @Resource private TestResultService testResultService; - + @Resource private ApiEnvironmentRunningParamService apiEnvironmentRunningParamService; - + @Resource private MsResultService resultService; - public String runMode = ApiRunMode.RUN.name(); - - // 测试ID - private String testId; - - private String debugReportId; - - public void handleSetupTest(BackendListenerContext context) throws Exception { - setParam(context); - testResultService = CommonBeanFactory.getBean(TestResultService.class); - if (testResultService == null) { - LogUtil.error("testResultService is required"); - } - resultService = CommonBeanFactory.getBean(MsResultService.class); - if (resultService == null) { - LogUtil.error("MsResultService is required"); - } - - apiEnvironmentRunningParamService = CommonBeanFactory.getBean(ApiEnvironmentRunningParamService.class); - if (apiEnvironmentRunningParamService == null) { - LogUtil.error("apiEnvironmentRunningParamService is required"); - } - } - - public void handleSampleResults(List sampleResults) { - queue.addAll(sampleResults); - } - - public void handleTeardownTest() throws Exception { + public void handleTeardownTest(List queue, String runMode, String testId, String debugReportId) throws Exception { TestResult testResult = new TestResult(); testResult.setTestId(testId); MessageCache.runningEngine.remove(testId); @@ -80,19 +51,10 @@ public class APIBackendListenerHandler { testResult.getScenarios().addAll(scenarios.values()); testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); testResult.setConsole(resultService.getJmeterLogger(testId, true)); - testResultService.saveResult(testResult, this.runMode, this.debugReportId, this.testId); + testResultService.saveResult(testResult, runMode, debugReportId, testId); // 清除已经中断的过程数据 if (!MessageCache.reportCache.containsKey(testId) && resultService.getProcessCache().containsKey(testId)) { resultService.getProcessCache().remove(testId); } } - - private void setParam(BackendListenerContext context) { - this.testId = context.getParameter(APIBackendListenerClient.TEST_ID); - this.runMode = context.getParameter("runMode"); - this.debugReportId = context.getParameter("debugReportId"); - if (StringUtils.isBlank(this.runMode)) { - this.runMode = ApiRunMode.RUN.name(); - } - } }