From 05a6ef5569d68f37a1009db1e1063e176a36a29e Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 9 Nov 2021 17:13:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=A7=E8=A1=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/definition/request/ElementUtil.java | 4 +- .../api/jmeter/APIBackendListenerHandler.java | 67 +++++++++++-------- .../io/metersphere/api/jmeter/FixedTask.java | 12 +++- .../metersphere/api/jmeter/ReportCounter.java | 2 +- .../api/service/ApiAutomationService.java | 4 +- .../api/service/ApiScenarioReportService.java | 51 +++++--------- .../api/service/MsResultService.java | 50 +++++++------- .../api/service/RemakeReportService.java | 2 +- 8 files changed, 96 insertions(+), 96 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index 3c50d81071..df163c9cb9 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -398,7 +398,9 @@ public class ElementUtil { List delIds = new ArrayList<>(); Map updateMap = new HashMap<>(); if (CollectionUtils.isEmpty(sourceHashTree)) { - sourceHashTree.addAll(targetHashTree); + if (CollectionUtils.isNotEmpty(targetHashTree)) { + sourceHashTree.addAll(targetHashTree); + } return; } if (CollectionUtils.isNotEmpty(targetHashTree)) { diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerHandler.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerHandler.java index 50af871e8f..70eb1ee21d 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerHandler.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerHandler.java @@ -4,6 +4,7 @@ package io.metersphere.api.jmeter; import io.metersphere.api.dto.RunningParamKeys; import io.metersphere.api.service.MsResultService; import io.metersphere.api.service.TestResultService; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.samplers.SampleResult; import org.springframework.stereotype.Service; @@ -26,36 +27,44 @@ public class APIBackendListenerHandler { private MsResultService resultService; public void handleTeardownTest(List queue, String runMode, String testId, String debugReportId, String amassReport) throws Exception { - TestResult testResult = new TestResult(); - testResult.setTestId(testId); - testResult.setSetReportId(amassReport); - MessageCache.runningEngine.remove(testId); - testResult.setTotal(0); - List enviromentList = new ArrayList<>(); - // 一个脚本里可能包含多个场景(ThreadGroup),所以要区分开,key: 场景Id - final Map scenarios = new LinkedHashMap<>(); - queue.forEach(result -> { - // 线程名称: <场景名> <场景Index>-<请求Index>, 例如:Scenario 2-1 - if (StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) { - String evnStr = result.getResponseDataAsString(); - enviromentList.add(evnStr); - } else { - resultService.formatTestResult(testResult, scenarios, result); + try { + TestResult testResult = new TestResult(); + testResult.setTestId(testId); + testResult.setSetReportId(amassReport); + MessageCache.runningEngine.remove(testId); + testResult.setTotal(0); + List environmentList = new ArrayList<>(); + // 一个脚本里可能包含多个场景(ThreadGroup),所以要区分开,key: 场景Id + final Map scenarios = new LinkedHashMap<>(); + queue.forEach(result -> { + // 线程名称: <场景名> <场景Index>-<请求Index>, 例如:Scenario 2-1 + if (StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) { + String evnStr = result.getResponseDataAsString(); + environmentList.add(evnStr); + } else { + resultService.formatTestResult(testResult, scenarios, result); + } + }); + queue.clear(); + testResult.getScenarios().addAll(scenarios.values()); + testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); + testResult.setConsole(resultService.getJmeterLogger(testId, true)); + //处理环境参数 + ExecutedHandleSingleton.parseEnvironment(environmentList); + testResultService.saveResult(testResult, runMode, debugReportId, testId); + // 清除已经中断的过程数据 + if (!MessageCache.reportCache.containsKey(testId) && resultService.getProcessCache().containsKey(testId)) { + resultService.getProcessCache().remove(testId); + } + if (StringUtils.isNotEmpty(testId)) { + MessageCache.executionQueue.remove(testId); + } + } catch (Exception e) { + if (StringUtils.isNotEmpty(amassReport) && MessageCache.cache.get(amassReport) != null + && StringUtils.isNotEmpty(testId) + && CollectionUtils.isNotEmpty(MessageCache.cache.get(amassReport).getReportIds())) { + MessageCache.cache.get(amassReport).getReportIds().remove(testId); } - }); - queue.clear(); - testResult.getScenarios().addAll(scenarios.values()); - testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); - testResult.setConsole(resultService.getJmeterLogger(testId, true)); - //处理环境参数 - ExecutedHandleSingleton.parseEnvironment(enviromentList); - testResultService.saveResult(testResult, runMode, debugReportId, testId); - // 清除已经中断的过程数据 - if (!MessageCache.reportCache.containsKey(testId) && resultService.getProcessCache().containsKey(testId)) { - resultService.getProcessCache().remove(testId); - } - if (StringUtils.isNotEmpty(testId)) { - MessageCache.executionQueue.remove(testId); } } } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/FixedTask.java b/backend/src/main/java/io/metersphere/api/jmeter/FixedTask.java index f673db05ce..4aa21da966 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/FixedTask.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/FixedTask.java @@ -1,5 +1,6 @@ package io.metersphere.api.jmeter; +import com.alibaba.fastjson.JSON; import io.metersphere.api.service.ApiScenarioReportService; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; @@ -8,7 +9,9 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; @Component public class FixedTask { @@ -30,9 +33,12 @@ public class FixedTask { if (MessageCache.cache != null && MessageCache.cache.size() > 0) { for (String key : MessageCache.cache.keySet()) { ReportCounter counter = MessageCache.cache.get(key); - LogUtil.info("集成报告:【" + key + "】总执行场景:【" + counter.getReportIds().size() + "】已经执行完成场景:【" + counter.getNumber() + "】"); + LogUtil.info("集成报告:【" + key + "】总执行场景:【" + counter.getReportIds().size() + "】已经执行完成场景:【" + counter.getCompletedIds().size() + "】"); + List filterList = counter.getReportIds().stream().filter(t -> !counter.getCompletedIds().contains(t)).collect(Collectors.toList()); + LogUtil.info("剩余要执行的报告" + JSON.toJSONString(filterList)); + // 合并 - if (counter.getNumber() >= counter.getReportIds().size()) { + if (counter.getCompletedIds().size() >= counter.getReportIds().size()) { scenarioReportService.margeReport(key, counter.getReportIds()); guardTask.remove(key); MessageCache.cache.remove(key); @@ -45,7 +51,7 @@ public class FixedTask { } else { guardTask.put(key, 0); } - if (CollectionUtils.isNotEmpty(counter.getPoolUrls()) && counter.getNumber() > 0 && guardTask.get(key) > 200) { + if (CollectionUtils.isNotEmpty(counter.getPoolUrls()) && counter.getCompletedIds().size() > 0 && guardTask.get(key) > 200) { // 资源池中已经没有执行的请求了 int runningCount = scenarioReportService.get(key, counter); if (runningCount == 0) { diff --git a/backend/src/main/java/io/metersphere/api/jmeter/ReportCounter.java b/backend/src/main/java/io/metersphere/api/jmeter/ReportCounter.java index eacbac3c13..ca4f14d724 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/ReportCounter.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/ReportCounter.java @@ -7,7 +7,7 @@ import java.util.List; @Data public class ReportCounter { - private int number; + private List completedIds; private List poolUrls; private List reportIds; } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 5af3e359e3..8a6156fe3b 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -1229,7 +1229,7 @@ public class ApiAutomationService { List reportIds = executeQueue.entrySet().stream() .map(reports -> reports.getKey()).collect(Collectors.toList()); ReportCounter counter = new ReportCounter(); - counter.setNumber(0); + counter.setCompletedIds(new LinkedList<>()); if (CollectionUtils.isNotEmpty(request.getConfig().getTestResources())) { counter.setPoolUrls(request.getConfig().getTestResources()); } @@ -1369,7 +1369,7 @@ public class ApiAutomationService { APIScenarioReportResult report = executeQueue.get(reportId).getReport(); batchMapper.insert(report); // 增加一个本地锁,防止并发找不到资源 - MessageCache.scenarioExecResourceLock.put(reportId,report); + MessageCache.scenarioExecResourceLock.put(reportId, report); } sqlSession.flushStatements(); sqlSession.commit(); 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 d9027c1884..f64add9a40 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -93,7 +93,6 @@ public class ApiScenarioReportService { } else if (StringUtils.equalsAny(runMode, ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { return updateSchedulePlanCase(result, runMode); } else { - updateScenarioStatus(result.getTestId()); return updateScenario(result); } } @@ -415,37 +414,14 @@ public class ApiScenarioReportService { * * @param reportId */ - public void updateScenarioStatus(String reportId) { + public void updatePrevResult(String scenarioId, String reportId) { if (StringUtils.isNotEmpty(reportId)) { - List list = new LinkedList<>(); - try { - list = JSON.parseObject(reportId, List.class); - } catch (Exception e) { - list.add(reportId); - } - if (CollectionUtils.isNotEmpty(list)) { - ApiScenarioReportExample scenarioReportExample = new ApiScenarioReportExample(); - scenarioReportExample.createCriteria().andIdIn(list); - List reportList = apiScenarioReportMapper.selectByExample(scenarioReportExample); - SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - ApiScenarioReportMapper scenarioReportMapper = sqlSession.getMapper(ApiScenarioReportMapper.class); - if (CollectionUtils.isNotEmpty(reportList)) { - reportList.forEach(report -> { - report.setUpdateTime(System.currentTimeMillis()); - report.setEndTime(System.currentTimeMillis()); - String status = "Error"; - report.setStatus(status); - scenarioReportMapper.updateByPrimaryKeySelective(report); - // 把上一条调试的数据内容清空 - ApiScenarioReport prevResult = extApiScenarioReportMapper.selectPreviousReportByScenarioId(report.getScenarioId(), reportId); - if (prevResult != null) { - ApiScenarioReportDetailExample example = new ApiScenarioReportDetailExample(); - example.createCriteria().andReportIdEqualTo(prevResult.getId()); - apiScenarioReportDetailMapper.deleteByExample(example); - } - }); - sqlSession.flushStatements(); - } + // 把上一条调试的数据内容清空 + ApiScenarioReport prevResult = extApiScenarioReportMapper.selectPreviousReportByScenarioId(scenarioId, reportId); + if (prevResult != null) { + ApiScenarioReportDetailExample example = new ApiScenarioReportDetailExample(); + example.createCriteria().andReportIdEqualTo(prevResult.getId()); + apiScenarioReportDetailMapper.deleteByExample(example); } } } @@ -540,12 +516,14 @@ public class ApiScenarioReportService { } catch (Exception e) { list.add(result.getTestId()); } - if (StringUtils.isNotEmpty(result.getSetReportId())) { - list.add(result.getSetReportId()); - } ApiScenarioReportExample scenarioReportExample = new ApiScenarioReportExample(); scenarioReportExample.createCriteria().andScenarioIdIn(list); List reportList = apiScenarioReportMapper.selectByExample(scenarioReportExample); + list.forEach(item ->{ + if(MessageCache.scenarioExecResourceLock.containsKey(item)) { + reportList.add(MessageCache.scenarioExecResourceLock.get(item)); + } + }); for (ApiScenarioReport report : reportList) { report.setStatus("Error"); apiScenarioReportMapper.updateByPrimaryKey(report); @@ -555,7 +533,7 @@ public class ApiScenarioReportService { Object obj = MessageCache.cache.get(result.getSetReportId()); if (obj != null) { ReportCounter counter = (ReportCounter) obj; - counter.setNumber(counter.getNumber() + 1); + counter.getCompletedIds().add(report.getId()); MessageCache.cache.put(result.getSetReportId(), counter); } } @@ -612,10 +590,11 @@ public class ApiScenarioReportService { Object obj = MessageCache.cache.get(result.getSetReportId()); if (obj != null) { ReportCounter counter = (ReportCounter) obj; - counter.setNumber(counter.getNumber() + 1); + counter.getCompletedIds().add(report.getId()); MessageCache.cache.put(result.getSetReportId(), counter); } } + updatePrevResult(report.getScenarioId(), report.getId()); } } // 针对未正常返回结果的报告计数 diff --git a/backend/src/main/java/io/metersphere/api/service/MsResultService.java b/backend/src/main/java/io/metersphere/api/service/MsResultService.java index e5ee054cc0..0d0441c278 100644 --- a/backend/src/main/java/io/metersphere/api/service/MsResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/MsResultService.java @@ -138,31 +138,35 @@ public class MsResultService { } public String getJmeterLogger(String testId, boolean removed) { - Long startTime = FixedTask.tasks.get(testId); - if (startTime == null) { - startTime = FixedTask.tasks.get("[" + testId + "]"); - } - if (startTime == null) { - startTime = System.currentTimeMillis(); - } - Long endTime = System.currentTimeMillis(); - Long finalStartTime = startTime; - String logMessage = JmeterLoggerAppender.logger.entrySet().stream() - .filter(map -> map.getKey() > finalStartTime && map.getKey() < endTime) - .map(map -> map.getValue()).collect(Collectors.joining()); - if (removed) { - if (processCache.get(testId) != null) { - try { - Thread.sleep(2000); - } catch (Exception e) { - } + try { + Long startTime = FixedTask.tasks.get(testId); + if (startTime == null) { + startTime = FixedTask.tasks.get("[" + testId + "]"); } - FixedTask.tasks.remove(testId); + if (startTime == null) { + startTime = System.currentTimeMillis(); + } + Long endTime = System.currentTimeMillis(); + Long finalStartTime = startTime; + String logMessage = JmeterLoggerAppender.logger.entrySet().stream() + .filter(map -> map.getKey() > finalStartTime && map.getKey() < endTime) + .map(map -> map.getValue()).collect(Collectors.joining()); + if (removed) { + if (processCache.get(testId) != null) { + try { + Thread.sleep(2000); + } catch (Exception e) { + } + } + FixedTask.tasks.remove(testId); + } + if (FixedTask.tasks.isEmpty()) { + JmeterLoggerAppender.logger.clear(); + } + return logMessage; + }catch (Exception e){ + return ""; } - if (FixedTask.tasks.isEmpty()) { - JmeterLoggerAppender.logger.clear(); - } - return logMessage; } public RequestResult getRequestResult(SampleResult result) { 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 4291c5e2b6..2aa18b87fb 100644 --- a/backend/src/main/java/io/metersphere/api/service/RemakeReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/RemakeReportService.java @@ -31,7 +31,7 @@ public class RemakeReportService { private TestPlanApiScenarioMapper testPlanApiScenarioMapper; public void remake(RunRequest runRequest, RunModeConfig config, String reportId) { - if (MessageCache.cache.get(config.getAmassReport()) != null + if (config != null && config.getAmassReport() != null && MessageCache.cache.get(config.getAmassReport()) != null && MessageCache.cache.get(config.getAmassReport()).getReportIds() != null) { MessageCache.cache.get(config.getAmassReport()).getReportIds().remove(reportId); }