改为后台增加BackendListener,保证前端生成的jmx独立性

This commit is contained in:
q4speed 2020-05-25 15:22:05 +08:00
parent af47f8d950
commit e736019e8f
4 changed files with 22 additions and 16 deletions

View File

@ -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<SampleResult> 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();

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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) {