fix(接口测试): 修复事物控制器提取内容不展示问题

--bug=1008747 --user=赵勇 [github#8403]调试生成报告,事务控制器下面的请求,调试后提取的数据不展示 https://www.tapd.cn/55049933/s/1088013
This commit is contained in:
fit2-zhao 2021-12-28 17:30:21 +08:00 committed by fit2-zhao
parent 10ca1653a3
commit 344fa7be0b
6 changed files with 42 additions and 29 deletions

View File

@ -148,7 +148,7 @@
<dependency>
<groupId>io.metersphere</groupId>
<artifactId>ms-jmeter-core</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
</dependency>
<!-- 排除jmeter中的 xstream 解决bug -->

View File

@ -66,7 +66,7 @@ public class MsExtract extends MsTestElement {
shell.setProperty(TestElement.TEST_CLASS, JSR223PostProcessor.class.getName());
shell.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
shell.setProperty("cacheKey", false);
shell.setProperty("script", "io.metersphere.utils.JMeterVars.addVars(prev.hashCode(),vars," + "\"" + extract.toString() + "\"" + ");");
shell.setProperty("script", "io.metersphere.utils.JMeterVars.addVars(prev.getResourceId(),vars," + "\"" + extract.toString() + "\"" + ");");
samplerHashTree.add(shell);
}
}

View File

@ -68,9 +68,9 @@ public class JMeterService {
}
private void addDebugListener(String testId, HashTree testPlan) {
MsResultCollector resultCollector = new MsResultCollector();
MsDebugListener resultCollector = new MsDebugListener();
resultCollector.setName(testId);
resultCollector.setProperty(TestElement.TEST_CLASS, MsResultCollector.class.getName());
resultCollector.setProperty(TestElement.TEST_CLASS, MsDebugListener.class.getName());
resultCollector.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ViewResultsFullVisualizer"));
resultCollector.setEnabled(true);
testPlan.add(testPlan.getArray()[0], resultCollector);
@ -132,7 +132,7 @@ public class JMeterService {
}
}
private synchronized void send(JmeterRunRequestDTO request) {
private void send(JmeterRunRequestDTO request) {
try {
List<JvmInfoDTO> resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId());
int index = (int) (Math.random() * resources.size());
@ -141,6 +141,7 @@ public class JMeterService {
String configuration = testResource.getConfiguration();
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
request.setCorePoolSize(node.getMaxConcurrency());
request.setEnable(node.isEnable());
String nodeIp = node.getIp();
Integer port = node.getPort();
String uri = String.format(BASE_URL + "/jmeter/api/start", nodeIp, port);

View File

@ -44,7 +44,7 @@ import java.util.Map;
/**
* 实时结果监听
*/
public class MsResultCollector extends AbstractListenerElement implements SampleListener, Clearable, Serializable,
public class MsDebugListener extends AbstractListenerElement implements SampleListener, Clearable, Serializable,
TestStateListener, Remoteable, NoThreadClone {
private static final String ERROR_LOGGING = "MsResultCollector.error_logging"; // $NON-NLS-1$
@ -57,7 +57,7 @@ public class MsResultCollector extends AbstractListenerElement implements Sample
@Override
public Object clone() {
MsResultCollector clone = (MsResultCollector) super.clone();
MsDebugListener clone = (MsDebugListener) super.clone();
return clone;
}
@ -145,7 +145,39 @@ public class MsResultCollector extends AbstractListenerElement implements Sample
@Override
public void sampleOccurred(SampleEvent event) {
SampleResult result = event.getResult();
JMeterVariables variables = JMeterVars.get(result.hashCode());
this.setVars(result);
if (isSampleWanted(result.isSuccessful()) && !StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) {
RequestResult requestResult = JMeterBase.getRequestResult(result);
if (requestResult != null) {
MsgDto dto = new MsgDto();
dto.setExecEnd(false);
dto.setReportId("send." + this.getName());
dto.setToReport(this.getName());
String console = CommonBeanFactory.getBean(MsResultService.class).getJmeterLogger(this.getName());
if (StringUtils.isNotEmpty(requestResult.getName()) && requestResult.getName().startsWith("Transaction=")) {
requestResult.getSubRequestResults().forEach(transactionResult -> {
transactionResult.getResponseResult().setConsole(console);
dto.setContent("result_" + JSON.toJSONString(transactionResult));
WebSocketUtils.sendMessageSingle(dto);
});
} else {
requestResult.getResponseResult().setConsole(console);
dto.setContent("result_" + JSON.toJSONString(requestResult));
WebSocketUtils.sendMessageSingle(dto);
}
LoggerUtil.debug("send. " + this.getName());
}
}
}
private void setVars(SampleResult result) {
if (StringUtils.isNotEmpty(result.getSampleLabel()) && result.getSampleLabel().startsWith("Transaction=")) {
for (int i = 0; i < result.getSubResults().length; i++) {
SampleResult subResult = result.getSubResults()[i];
this.setVars(subResult);
}
}
JMeterVariables variables = JMeterVars.get(result.getResourceId());
if (variables != null && CollectionUtils.isNotEmpty(variables.entrySet())) {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Object> entry : variables.entrySet()) {
@ -155,23 +187,6 @@ public class MsResultCollector extends AbstractListenerElement implements Sample
result.setExtVars(builder.toString());
}
}
if (isSampleWanted(result.isSuccessful()) && !StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) {
RequestResult requestResult = JMeterBase.getRequestResult(result);
if (requestResult != null) {
if (StringUtils.isNotEmpty(requestResult.getName()) && requestResult.getName().startsWith("Transaction=") && CollectionUtils.isEmpty(requestResult.getSubRequestResults())) {
LoggerUtil.debug("进入合并事物,暂不处理");
} else {
requestResult.getResponseResult().setConsole(CommonBeanFactory.getBean(MsResultService.class).getJmeterLogger(this.getName()));
MsgDto dto = new MsgDto();
dto.setExecEnd(false);
dto.setContent("result_" + JSON.toJSONString(requestResult));
dto.setReportId("send." + this.getName());
dto.setToReport(this.getName());
LoggerUtil.debug("send. " + this.getName());
WebSocketUtils.sendMessageSingle(dto);
}
}
}
}
@Override

View File

@ -27,10 +27,6 @@ public class ApiScenarioReportResultService {
// 事物控制器出来的结果特殊处理
if (StringUtils.isNotEmpty(item.getName()) && item.getName().startsWith("Transaction=") && CollectionUtils.isEmpty(item.getSubRequestResults())) {
LoggerUtil.debug("合并事物请求暂不入库");
} else if (StringUtils.isNotEmpty(item.getName()) && item.getName().startsWith("Transaction=") && CollectionUtils.isNotEmpty(item.getSubRequestResults())) {
item.getSubRequestResults().forEach(subItem -> {
apiScenarioReportResultMapper.insert(this.newApiScenarioReportResult(reportId, subItem));
});
} else {
apiScenarioReportResultMapper.insert(this.newApiScenarioReportResult(reportId, item));
}

View File

@ -740,6 +740,7 @@ export default {
if (data.method === 'Request' && data.subRequestResults && data.subRequestResults.length > 0) {
data.subRequestResults.forEach(subItem => {
if (item.data && item.data.resourceId + "_" + item.data.parentIndex === subItem.resourceId) {
subItem.requestResult.console = data.responseResult.console;
item.data.requestResult.push(subItem);
//
this.resultEvaluation(subItem.resourceId, subItem.success);