fix(接口测试): 修复集合报告操作日志偶发为空的情况

--bug=1013615 --user=赵勇 【接口测试】批量执行case和场景,集合报告,控制台是空白的 https://www.tapd.cn/55049933/s/1167419
This commit is contained in:
fit2-zhao 2022-05-30 12:09:04 +08:00 committed by f2c-ci-robot[bot]
parent 896f2ba932
commit 9c3b529bfd
7 changed files with 24 additions and 16 deletions

View File

@ -6,6 +6,7 @@ import io.metersphere.api.service.ApiExecutionQueueService;
import io.metersphere.api.service.TestResultService;
import io.metersphere.cache.JMeterEngineCache;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.ResultDTO;
import io.metersphere.jmeter.JMeterBase;
import io.metersphere.jmeter.MsExecListener;
@ -46,7 +47,7 @@ public class APISingleResultListener implements MsExecListener {
JMeterBase.resultFormatting(queues, dto);
dto.setConsole(FixedCapacityUtils.getJmeterLogger(dto.getReportId()));
dto.setConsole(FixedCapacityUtils.getJmeterLogger(dto.getReportId(), !StringUtils.equals(dto.getReportType(), RunModeConstants.SET_REPORT.toString())));
// 入库存储
CommonBeanFactory.getBean(TestResultService.class).saveResults(dto);
@ -70,9 +71,6 @@ public class APISingleResultListener implements MsExecListener {
} catch (Exception e) {
LoggerUtil.error(e);
} finally {
if (FixedCapacityUtils.jmeterLogTask.containsKey(dto.getReportId())) {
FixedCapacityUtils.jmeterLogTask.remove(dto.getReportId());
}
if (JMeterEngineCache.runningEngine.containsKey(dto.getReportId())) {
JMeterEngineCache.runningEngine.remove(dto.getReportId());
}

View File

@ -33,11 +33,11 @@ public class FixedCapacityUtils {
}
public static String getJmeterLogger(String testId) {
public static String getJmeterLogger(String reportId, boolean isClear) {
try {
Long startTime = FixedCapacityUtils.jmeterLogTask.get(testId);
Long startTime = FixedCapacityUtils.jmeterLogTask.get(reportId);
if (startTime == null) {
startTime = FixedCapacityUtils.jmeterLogTask.get("[" + testId + "]");
startTime = FixedCapacityUtils.jmeterLogTask.get("[" + reportId + "]");
}
if (startTime == null) {
startTime = System.currentTimeMillis();
@ -47,9 +47,14 @@ public class FixedCapacityUtils {
String logMessage = FixedCapacityUtils.fixedCapacityCache.entrySet().stream()
.filter(map -> map.getKey() > finalStartTime && map.getKey() <= endTime)
.map(map -> map.getValue()).collect(Collectors.joining());
return logMessage;
} catch (Exception e) {
return "";
} finally {
if (isClear && FixedCapacityUtils.jmeterLogTask.containsKey(reportId)) {
FixedCapacityUtils.jmeterLogTask.remove(reportId);
}
}
}
}

View File

@ -155,7 +155,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
dto.setReportId("send." + this.getName());
dto.setToReport(this.getName());
String console = FixedCapacityUtils.getJmeterLogger(this.getName());
String console = FixedCapacityUtils.getJmeterLogger(this.getName(),false);
if (StringUtils.isNotEmpty(requestResult.getName()) && requestResult.getName().startsWith("Transaction=")) {
requestResult.getSubRequestResults().forEach(transactionResult -> {
transactionResult.getResponseResult().setConsole(console);

View File

@ -212,7 +212,7 @@ public class ApiExecutionQueueService {
if (StringUtils.equalsIgnoreCase(dto.getRunMode(), ApiRunMode.DEFINITION.name())) {
reportId = dto.getTestPlanReportId();
}
apiScenarioReportService.margeReport(reportId, dto.getRunMode());
apiScenarioReportService.margeReport(reportId, dto.getRunMode(), dto.getConsole());
}
return false;
}
@ -299,7 +299,7 @@ public class ApiExecutionQueueService {
if (StringUtils.equalsIgnoreCase(dto.getRunMode(), ApiRunMode.DEFINITION.name())) {
reportId = dto.getTestPlanReportId();
}
apiScenarioReportService.margeReport(reportId, dto.getRunMode());
apiScenarioReportService.margeReport(reportId, dto.getRunMode(), dto.getConsole());
}
}
return;
@ -335,7 +335,7 @@ public class ApiExecutionQueueService {
if (StringUtils.equalsIgnoreCase(dto.getRunMode(), ApiRunMode.DEFINITION.name())) {
reportId = dto.getTestPlanReportId();
}
apiScenarioReportService.margeReport(reportId, dto.getRunMode());
apiScenarioReportService.margeReport(reportId, dto.getRunMode(), dto.getConsole());
}
queueMapper.deleteByPrimaryKey(dto.getQueueId());
LoggerUtil.info("Queue execution ends" + dto.getQueueId());

View File

@ -345,7 +345,7 @@ public class ApiScenarioReportService {
return report;
}
public void margeReport(String reportId, String runMode) {
public void margeReport(String reportId, String runMode, String console) {
// 更新场景状态
if (StringUtils.equalsIgnoreCase(runMode, ApiRunMode.DEFINITION.name())) {
ApiDefinitionExecResult result = definitionExecResultMapper.selectByPrimaryKey(reportId);
@ -367,8 +367,15 @@ public class ApiScenarioReportService {
apiScenarioReportMapper.updateByPrimaryKey(report);
}
}
console = StringUtils.isNotEmpty(console) ? console : FixedCapacityUtils.getJmeterLogger(reportId, true);
if (StringUtils.isNotEmpty(console)) {
apiScenarioReportStructureService.update(reportId, console);
}
// 更新控制台信息
apiScenarioReportStructureService.update(reportId, FixedCapacityUtils.getJmeterLogger(reportId));
if (FixedCapacityUtils.jmeterLogTask.containsKey(reportId)) {
FixedCapacityUtils.jmeterLogTask.remove(reportId);
}
}
public ApiScenarioReport updateScenario(List<ApiScenarioReportResult> requestResults, ResultDTO dto) {

View File

@ -50,8 +50,6 @@ public class ApiScenarioReportStructureService {
@Resource
private ApiDefinitionExecResultMapper definitionExecResultMapper;
@Resource
private ApiScenarioReportResultService apiScenarioReportResultService;
@Resource
private ExtApiScenarioReportResultMapper extApiScenarioReportResultMapper;
private static final List<String> requests = Arrays.asList("HTTPSamplerProxy", "DubboSampler", "JDBCSampler", "TCPSampler", "JSR223Processor", "AbstractSampler");

View File

@ -181,7 +181,7 @@ public class RemakeReportService {
LoggerUtil.info("进入异常结果处理报告【" + dto.getReportId() + "" + dto.getRunMode() + " 整体执行完成");
// 全局并发队列
PoolExecBlockingQueueUtil.offer(dto.getReportId());
String consoleMsg = FixedCapacityUtils.getJmeterLogger(dto.getReportId());
String consoleMsg = FixedCapacityUtils.getJmeterLogger(dto.getReportId(),true);
dto.setConsole(consoleMsg + "\n" + errorMsg);
// 整体执行结束更新资源状态
CommonBeanFactory.getBean(TestResultService.class).testEnded(dto);