From 9c3b529bfd50a33f3672903a8a447b0e36f10093 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 30 May 2022 12:09:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9B=86=E5=90=88=E6=8A=A5=E5=91=8A=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=97=A5=E5=BF=97=E5=81=B6=E5=8F=91=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1013615 --user=赵勇 【接口测试】批量执行case和场景,集合报告,控制台是空白的 https://www.tapd.cn/55049933/s/1167419 --- .../api/jmeter/APISingleResultListener.java | 6 ++---- .../io/metersphere/api/jmeter/FixedCapacityUtils.java | 11 ++++++++--- .../io/metersphere/api/jmeter/MsDebugListener.java | 2 +- .../api/service/ApiExecutionQueueService.java | 6 +++--- .../api/service/ApiScenarioReportService.java | 11 +++++++++-- .../service/ApiScenarioReportStructureService.java | 2 -- .../metersphere/api/service/RemakeReportService.java | 2 +- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java b/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java index 7a99e5e7b1..37dbb18d77 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java @@ -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()); } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/FixedCapacityUtils.java b/backend/src/main/java/io/metersphere/api/jmeter/FixedCapacityUtils.java index 088b0bf01b..a0a1cac868 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/FixedCapacityUtils.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/FixedCapacityUtils.java @@ -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); + } } } } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java b/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java index 3ef62c027d..88f5355a52 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java @@ -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); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java b/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java index c32272b715..64f9c82282 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java @@ -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()); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java index ccbebbdd2e..9f93828976 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -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 requestResults, ResultDTO dto) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportStructureService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportStructureService.java index a773e79442..e11b56a6f3 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportStructureService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportStructureService.java @@ -50,8 +50,6 @@ public class ApiScenarioReportStructureService { @Resource private ApiDefinitionExecResultMapper definitionExecResultMapper; @Resource - private ApiScenarioReportResultService apiScenarioReportResultService; - @Resource private ExtApiScenarioReportResultMapper extApiScenarioReportResultMapper; private static final List requests = Arrays.asList("HTTPSamplerProxy", "DubboSampler", "JDBCSampler", "TCPSampler", "JSR223Processor", "AbstractSampler"); diff --git a/backend/src/main/java/io/metersphere/api/service/RemakeReportService.java b/backend/src/main/java/io/metersphere/api/service/RemakeReportService.java index 2e5331092d..a43c991cfe 100644 --- a/backend/src/main/java/io/metersphere/api/service/RemakeReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/RemakeReportService.java @@ -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);