fix: APIBackendListenerClient优化
This commit is contained in:
parent
2aab87f404
commit
c3359328ce
|
@ -1,12 +1,14 @@
|
||||||
package io.metersphere.api.jmeter;
|
package io.metersphere.api.jmeter;
|
||||||
|
|
||||||
|
import io.metersphere.commons.constants.ApiRunMode;
|
||||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.jmeter.samplers.SampleResult;
|
import org.apache.jmeter.samplers.SampleResult;
|
||||||
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient;
|
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient;
|
||||||
import org.apache.jmeter.visualizers.backend.BackendListenerContext;
|
import org.apache.jmeter.visualizers.backend.BackendListenerContext;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,27 +18,41 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
|
||||||
|
|
||||||
public final static String TEST_ID = "ms.test.id";
|
public final static String TEST_ID = "ms.test.id";
|
||||||
|
|
||||||
|
public String runMode = ApiRunMode.RUN.name();
|
||||||
|
|
||||||
|
private final List<SampleResult> queue = new ArrayList<>();
|
||||||
|
|
||||||
|
// 测试ID
|
||||||
|
private String testId;
|
||||||
|
|
||||||
|
private String debugReportId;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setupTest(BackendListenerContext context) throws Exception {
|
public void setupTest(BackendListenerContext context) throws Exception {
|
||||||
APIBackendListenerHandler apiBackendListenerHandler =
|
setParam(context);
|
||||||
CommonBeanFactory.getBean(APIBackendListenerHandler.class);
|
|
||||||
apiBackendListenerHandler.handleSetupTest(context);
|
|
||||||
super.setupTest(context);
|
super.setupTest(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleSampleResults(List<SampleResult> sampleResults, BackendListenerContext context) {
|
public void handleSampleResults(List<SampleResult> sampleResults, BackendListenerContext context) {
|
||||||
APIBackendListenerHandler apiBackendListenerHandler =
|
queue.addAll(sampleResults);
|
||||||
CommonBeanFactory.getBean(APIBackendListenerHandler.class);
|
|
||||||
apiBackendListenerHandler.handleSampleResults(sampleResults);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void teardownTest(BackendListenerContext context) throws Exception {
|
public void teardownTest(BackendListenerContext context) throws Exception {
|
||||||
APIBackendListenerHandler apiBackendListenerHandler =
|
APIBackendListenerHandler apiBackendListenerHandler =
|
||||||
CommonBeanFactory.getBean(APIBackendListenerHandler.class);
|
CommonBeanFactory.getBean(APIBackendListenerHandler.class);
|
||||||
apiBackendListenerHandler.handleTeardownTest();
|
apiBackendListenerHandler.handleTeardownTest(queue, this.runMode, this.testId, this.debugReportId);
|
||||||
super.teardownTest(context);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,16 +5,16 @@ import io.metersphere.api.dto.RunningParamKeys;
|
||||||
import io.metersphere.api.service.ApiEnvironmentRunningParamService;
|
import io.metersphere.api.service.ApiEnvironmentRunningParamService;
|
||||||
import io.metersphere.api.service.MsResultService;
|
import io.metersphere.api.service.MsResultService;
|
||||||
import io.metersphere.api.service.TestResultService;
|
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.commons.lang3.StringUtils;
|
||||||
import org.apache.jmeter.samplers.SampleResult;
|
import org.apache.jmeter.samplers.SampleResult;
|
||||||
import org.apache.jmeter.visualizers.backend.BackendListenerContext;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public class APIBackendListenerHandler {
|
public class APIBackendListenerHandler {
|
||||||
|
|
||||||
private final List<SampleResult> queue = new ArrayList<>();
|
@Resource
|
||||||
|
|
||||||
private TestResultService testResultService;
|
private TestResultService testResultService;
|
||||||
|
@Resource
|
||||||
private ApiEnvironmentRunningParamService apiEnvironmentRunningParamService;
|
private ApiEnvironmentRunningParamService apiEnvironmentRunningParamService;
|
||||||
|
@Resource
|
||||||
private MsResultService resultService;
|
private MsResultService resultService;
|
||||||
|
|
||||||
public String runMode = ApiRunMode.RUN.name();
|
public void handleTeardownTest(List<SampleResult> queue, String runMode, String testId, String debugReportId) throws Exception {
|
||||||
|
|
||||||
// 测试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<SampleResult> sampleResults) {
|
|
||||||
queue.addAll(sampleResults);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleTeardownTest() throws Exception {
|
|
||||||
TestResult testResult = new TestResult();
|
TestResult testResult = new TestResult();
|
||||||
testResult.setTestId(testId);
|
testResult.setTestId(testId);
|
||||||
MessageCache.runningEngine.remove(testId);
|
MessageCache.runningEngine.remove(testId);
|
||||||
|
@ -80,19 +51,10 @@ public class APIBackendListenerHandler {
|
||||||
testResult.getScenarios().addAll(scenarios.values());
|
testResult.getScenarios().addAll(scenarios.values());
|
||||||
testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId));
|
testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId));
|
||||||
testResult.setConsole(resultService.getJmeterLogger(testId, true));
|
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)) {
|
if (!MessageCache.reportCache.containsKey(testId) && resultService.getProcessCache().containsKey(testId)) {
|
||||||
resultService.getProcessCache().remove(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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue