From 6a9983903dec514235dd890d5b3a9c14f5949ee3 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 23 Feb 2022 16:10:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E7=94=A8=E4=BE=8B=E5=92=8C=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E6=89=B9=E9=87=8F=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/exec/api/ApiCaseExecuteService.java | 56 ++++++++++--------- .../api/ApiCaseParallelExecuteService.java | 45 +++++++-------- .../scenario/ApiScenarioExecuteService.java | 19 +++++-- .../scenario/ApiScenarioParallelService.java | 47 +++++++--------- 4 files changed, 84 insertions(+), 83 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java index b34f1c4929..4f6bb685d0 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java @@ -116,16 +116,19 @@ public class ApiCaseExecuteService { DBTestQueue deQueue = apiExecutionQueueService.add(executeQueue, poolId, ApiRunMode.API_PLAN.name(), request.getPlanReportId(), reportType, runMode, request.getConfig()); // 开始选择执行模式 - if (request.getConfig() != null && request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { - LoggerUtil.debug("开始串行执行"); - if (deQueue != null && deQueue.getQueue() != null) { - apiScenarioSerialService.serial(deQueue, deQueue.getQueue()); - } - } else { - LoggerUtil.debug("开始并发执行"); - if (deQueue != null && deQueue.getQueue() != null) { - apiCaseParallelExecuteService.parallel(executeQueue, request.getConfig(), deQueue, runMode); - } + if (deQueue != null && deQueue.getQueue() != null) { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + Thread.currentThread().setName("PLAN-CASE:" + request.getPlanReportId()); + if (request.getConfig() != null && request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { + apiScenarioSerialService.serial(deQueue, deQueue.getQueue()); + } else { + apiCaseParallelExecuteService.parallel(executeQueue, request.getConfig(), deQueue, runMode); + } + } + }); + thread.start(); } return responseDTOS; } @@ -153,8 +156,8 @@ public class ApiCaseExecuteService { ApiTestCaseExample example = new ApiTestCaseExample(); example.createCriteria().andIdIn(request.getIds()); - List list = apiTestCaseMapper.selectByExampleWithBLOBs(example); - LoggerUtil.debug("查询到执行数据:" + list.size()); + List caseList = apiTestCaseMapper.selectByExampleWithBLOBs(example); + LoggerUtil.debug("查询到执行数据:" + caseList.size()); // 集合报告设置 String serialReportId = null; @@ -164,7 +167,7 @@ public class ApiCaseExecuteService { APIScenarioReportResult report = apiScenarioReportService.init(request.getConfig().getReportId(), null, request.getConfig().getReportName(), ReportTriggerMode.MANUAL.name(), ExecuteType.Saved.name(), request.getProjectId(), null, request.getConfig()); - report.setVersionId(list.get(0).getVersionId()); + report.setVersionId(caseList.get(0).getVersionId()); report.setName(request.getConfig().getReportName()); report.setTestName(request.getConfig().getReportName()); report.setId(serialReportId); @@ -182,7 +185,7 @@ public class ApiCaseExecuteService { FixedOrderComparator fixedOrderComparator = new FixedOrderComparator(request.getIds()); fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.BEFORE); BeanComparator beanComparator = new BeanComparator("id", fixedOrderComparator); - Collections.sort(list, beanComparator); + Collections.sort(caseList, beanComparator); } } @@ -195,7 +198,7 @@ public class ApiCaseExecuteService { String status = request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString()) ? APITestStatus.Waiting.name() : APITestStatus.Running.name(); String finalSerialReportId = serialReportId; - list.forEach(caseWithBLOBs -> { + caseList.forEach(caseWithBLOBs -> { ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.initBase(caseWithBLOBs.getId(), APITestStatus.Running.name(), null, request.getConfig()); report.setStatus(status); report.setName(caseWithBLOBs.getName()); @@ -214,16 +217,19 @@ public class ApiCaseExecuteService { String poolId = request.getConfig().getResourcePoolId(); DBTestQueue deQueue = apiExecutionQueueService.add(executeQueue, poolId, ApiRunMode.DEFINITION.name(), finalSerialReportId, reportType, ApiRunMode.DEFINITION.name(), request.getConfig()); // 开始选择执行模式 - if (request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { - LoggerUtil.debug("开始串行执行"); - if (deQueue != null && deQueue.getQueue() != null) { - apiScenarioSerialService.serial(deQueue, deQueue.getQueue()); - } - } else { - LoggerUtil.debug("开始并发执行"); - if (deQueue != null && deQueue.getQueue() != null) { - apiCaseParallelExecuteService.parallel(executeQueue, request.getConfig(), deQueue, ApiRunMode.DEFINITION.name()); - } + if (deQueue != null && deQueue.getQueue() != null) { + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + Thread.currentThread().setName("API-CASE-RUN"); + if (request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { + apiScenarioSerialService.serial(deQueue, deQueue.getQueue()); + } else { + apiCaseParallelExecuteService.parallel(executeQueue, request.getConfig(), deQueue, ApiRunMode.DEFINITION.name()); + } + } + }); + thread.start(); } return responseDTOS; } diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java index 63d4d9d95a..8fdb9f64ec 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java @@ -22,32 +22,25 @@ public class ApiCaseParallelExecuteService { private JMeterService jMeterService; public void parallel(Map executeQueue, RunModeConfigDTO config, DBTestQueue executionQueue, String runMode) { - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - Thread.currentThread().setName("API-CASE-THREAD"); - for (String testId : executeQueue.keySet()) { - ApiDefinitionExecResult result = executeQueue.get(testId); - String reportId = result.getId(); - HashTree hashTree = null; - JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(testId, reportId, runMode, hashTree); - runRequest.setPool(GenerateHashTreeUtil.isResourcePool(config.getResourcePoolId())); - runRequest.setTestPlanReportId(executionQueue.getReportId()); - runRequest.setPoolId(config.getResourcePoolId()); - runRequest.setReportType(executionQueue.getReportType()); - runRequest.setRunType(RunModeConstants.PARALLEL.toString()); - runRequest.setQueueId(executionQueue.getId()); - if (executionQueue.getQueue() != null) { - runRequest.setPlatformUrl(executionQueue.getQueue().getId()); - } - if (!GenerateHashTreeUtil.isResourcePool(config.getResourcePoolId()).isPool()) { - hashTree = apiScenarioSerialService.generateHashTree(testId, config.getEnvMap(), runRequest); - runRequest.setHashTree(hashTree); - } - jMeterService.run(runRequest); - } + for (String testId : executeQueue.keySet()) { + ApiDefinitionExecResult result = executeQueue.get(testId); + String reportId = result.getId(); + HashTree hashTree = null; + JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(testId, reportId, runMode, hashTree); + runRequest.setPool(GenerateHashTreeUtil.isResourcePool(config.getResourcePoolId())); + runRequest.setTestPlanReportId(executionQueue.getReportId()); + runRequest.setPoolId(config.getResourcePoolId()); + runRequest.setReportType(executionQueue.getReportType()); + runRequest.setRunType(RunModeConstants.PARALLEL.toString()); + runRequest.setQueueId(executionQueue.getId()); + if (executionQueue.getQueue() != null) { + runRequest.setPlatformUrl(executionQueue.getQueue().getId()); } - }); - thread.start(); + if (!GenerateHashTreeUtil.isResourcePool(config.getResourcePoolId()).isPool()) { + hashTree = apiScenarioSerialService.generateHashTree(testId, config.getEnvMap(), runRequest); + runRequest.setHashTree(hashTree); + } + jMeterService.run(runRequest); + } } } diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java index 0e0865450a..9841e5c3e8 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java @@ -173,11 +173,20 @@ public class ApiScenarioExecuteService { apiScenarioReportService.batchSave(executeQueue, serialReportId, request.getRunMode(), responseDTOS); // 开始执行 - if (StringUtils.equals(request.getConfig().getMode(), RunModeConstants.SERIAL.toString())) { - serialService.serial(executionQueue, executionQueue.getQueue()); - } else { - parallelService.parallel(executeQueue, request, serialReportId, executionQueue); - } + String finalSerialReportId = serialReportId; + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + Thread.currentThread().setName("SCENARIO-PARALLEL-THREAD"); + if (StringUtils.equals(request.getConfig().getMode(), RunModeConstants.SERIAL.toString())) { + serialService.serial(executionQueue, executionQueue.getQueue()); + } else { + parallelService.parallel(executeQueue, request, finalSerialReportId, executionQueue); + } + } + }); + thread.start(); + return responseDTOS; } diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java index a1d4a8145c..0650541756 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java @@ -21,33 +21,26 @@ public class ApiScenarioParallelService { private JMeterService jMeterService; public void parallel(Map executeQueue, RunScenarioRequest request, String serialReportId, DBTestQueue executionQueue) { - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - Thread.currentThread().setName("SCENARIO-PARALLEL-THREAD"); - for (String reportId : executeQueue.keySet()) { - RunModeDataDTO dataDTO = executeQueue.get(reportId); - JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(dataDTO.getTestId(), StringUtils.isNotEmpty(serialReportId) ? serialReportId : reportId, request.getRunMode(), null); - runRequest.setReportType(StringUtils.isNotEmpty(serialReportId) ? RunModeConstants.SET_REPORT.toString() : RunModeConstants.INDEPENDENCE.toString()); - runRequest.setQueueId(executionQueue.getId()); - if (request.getConfig() != null) { - runRequest.setPool(GenerateHashTreeUtil.isResourcePool(request.getConfig().getResourcePoolId())); - runRequest.setPoolId(request.getConfig().getResourcePoolId()); - } - runRequest.setTestPlanReportId(request.getTestPlanReportId()); - runRequest.setPlatformUrl(executionQueue.getDetailMap().get(reportId)); - runRequest.setRunType(RunModeConstants.PARALLEL.toString()); - if (LoggerUtil.getLogger().isDebugEnabled()) { - LoggerUtil.debug("Scenario run-开始并发执行:" + JSON.toJSONString(request)); - } - // 本地执行生成hashTree - if (request.getConfig() != null && !runRequest.getPool().isPool()) { - runRequest.setHashTree(GenerateHashTreeUtil.generateHashTree(dataDTO.getScenario(), dataDTO.getPlanEnvMap(), runRequest)); - } - jMeterService.run(runRequest); - } + for (String reportId : executeQueue.keySet()) { + RunModeDataDTO dataDTO = executeQueue.get(reportId); + JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(dataDTO.getTestId(), StringUtils.isNotEmpty(serialReportId) ? serialReportId : reportId, request.getRunMode(), null); + runRequest.setReportType(StringUtils.isNotEmpty(serialReportId) ? RunModeConstants.SET_REPORT.toString() : RunModeConstants.INDEPENDENCE.toString()); + runRequest.setQueueId(executionQueue.getId()); + if (request.getConfig() != null) { + runRequest.setPool(GenerateHashTreeUtil.isResourcePool(request.getConfig().getResourcePoolId())); + runRequest.setPoolId(request.getConfig().getResourcePoolId()); } - }); - thread.start(); + runRequest.setTestPlanReportId(request.getTestPlanReportId()); + runRequest.setPlatformUrl(executionQueue.getDetailMap().get(reportId)); + runRequest.setRunType(RunModeConstants.PARALLEL.toString()); + if (LoggerUtil.getLogger().isDebugEnabled()) { + LoggerUtil.debug("Scenario run-开始并发执行:" + JSON.toJSONString(request)); + } + // 本地执行生成hashTree + if (request.getConfig() != null && !runRequest.getPool().isPool()) { + runRequest.setHashTree(GenerateHashTreeUtil.generateHashTree(dataDTO.getScenario(), dataDTO.getPlanEnvMap(), runRequest)); + } + jMeterService.run(runRequest); + } } }