diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParseTmpParam.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParseTmpParam.java index d4fd15daf7..e9b353446e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParseTmpParam.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiScenarioParseTmpParam.java @@ -5,6 +5,7 @@ import io.metersphere.api.dto.request.http.MsHTTPElement; import lombok.Data; import java.util.*; +import java.util.concurrent.atomic.AtomicLong; /** * 执行场景解析参数时的临时参数 @@ -50,4 +51,8 @@ public class ApiScenarioParseTmpParam { * 环境相关信息 */ private ApiScenarioParseEnvInfo scenarioParseEnvInfo; + /** + * 场景中的待执行的请求总数 + */ + private AtomicLong requestCount = new AtomicLong(0); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiBatchRunBaseService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiBatchRunBaseService.java index add5ec3062..8b2a9cc53e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiBatchRunBaseService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiBatchRunBaseService.java @@ -26,6 +26,7 @@ public class ApiBatchRunBaseService { */ public ExecutionQueue initExecutionqueue(List resourceIds, ApiRunModeConfigDTO runModeConfig, String resourceType, Map caseReportMap, String userId) { ExecutionQueue queue = getExecutionQueue(runModeConfig, resourceType, userId); + queue.setRequestCount(runModeConfig.isIntegratedReport() ? resourceIds.size() : 1L); List queueDetails = getExecutionQueueDetails(resourceIds, caseReportMap); apiExecutionQueueService.insertQueue(queue, queueDetails); return queue; @@ -57,6 +58,7 @@ public class ApiBatchRunBaseService { ExecutionQueueDetail queueDetail = new ExecutionQueueDetail(); queueDetail.setResourceId(resourceId); queueDetail.setSort(sort.getAndIncrement()); + queueDetail.setRequestCount(1L); // caseReportMap 为 null ,说明是集合报告,生成一个虚拟的报告ID queueDetail.setReportId(caseReportMap == null ? UUID.randomUUID().toString() : caseReportMap.get(resourceId)); queueDetails.add(queueDetail); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java index bd560d820d..c6ae275db2 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseBatchRunService.java @@ -123,6 +123,7 @@ public class ApiTestCaseBatchRunService { // 先初始化集成报告,设置好报告ID,再初始化执行队列 ExecutionQueue queue = apiBatchRunBaseService.initExecutionqueue(ids, runModeConfig, ApiExecuteResourceType.API_CASE.name(), caseReportMap, userId); + // 执行第一个任务 ExecutionQueueDetail nextDetail = apiExecutionQueueService.getNextDetail(queue.getQueueId()); @@ -192,7 +193,9 @@ public class ApiTestCaseBatchRunService { // 如果是集成报告则生成唯一的虚拟ID,非集成报告使用单用例的报告ID reportId = runModeConfig.isIntegratedReport() ? UUID.randomUUID().toString() : caseReportMap.get(id); + Long requestCount = runModeConfig.isIntegratedReport() ? ids.size() : 1L; TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, apiTestCase, runModeConfig); + taskRequest.setRequestCount(requestCount); execute(taskRequest, apiTestCase, apiTestCaseBlob, definitionExecuteInfoMap.get(apiTestCase.getId())); } catch (Exception e) { LogUtils.error("执行用例失败 {}-{}", reportId, id); @@ -312,6 +315,7 @@ public class ApiTestCaseBatchRunService { TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, apiTestCase, runModeConfig); taskRequest.setQueueId(queue.getQueueId()); + taskRequest.setRequestCount(queue.getRequestCount()); execute(taskRequest, apiTestCase, apiTestCaseBlob, BeanUtils.copyBean(new ApiDefinitionExecuteInfo(), apiDefinition)); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java index 18682ab932..322f232f73 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioBatchRunService.java @@ -1,7 +1,6 @@ package io.metersphere.api.service.scenario; import io.metersphere.api.constants.ApiScenarioStepRefType; -import io.metersphere.api.constants.ApiScenarioStepType; import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiScenarioRecord; import io.metersphere.api.domain.ApiScenarioReport; @@ -209,7 +208,7 @@ public class ApiScenarioBatchRunService { private Long getRequestCount(List steps) { AtomicLong requestCount = new AtomicLong(); apiScenarioService.traversalStepTree(steps, step -> { - if (BooleanUtils.isTrue(step.getEnable()) && isRequestStep(step)) { + if (BooleanUtils.isTrue(step.getEnable()) && apiScenarioService.isRequestStep(step)) { requestCount.getAndIncrement(); } return true; @@ -217,10 +216,6 @@ public class ApiScenarioBatchRunService { return requestCount.get(); } - private boolean isRequestStep(ApiScenarioStepCommonDTO step) { - return StringUtils.equalsAny(step.getStepType(), ApiScenarioStepType.API.name(), ApiScenarioStepType.API_CASE.name(), ApiScenarioStepType.CUSTOM_REQUEST.name()); - } - /** * 集成报告,执行前先设置成 RUNNING * @param runModeConfig diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index f90ea029be..4cb4f4ccf7 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -1251,6 +1251,7 @@ public class ApiScenarioService extends MoveNodeService { apiExecuteService.getDebugRunModule(request.getFrontendDebug())); taskRequest.setSaveResult(false); taskRequest.setRealTime(true); + taskRequest.setRequestCount(tmpParam.getRequestCount().get()); ApiScenarioParamConfig parseConfig = getApiScenarioParamConfig(request, tmpParam.getScenarioParseEnvInfo()); parseConfig.setReportId(request.getReportId()); @@ -1305,6 +1306,7 @@ public class ApiScenarioService extends MoveNodeService { taskRequest.getRunModeConfig().setPoolId(poolId); taskRequest.setSaveResult(true); taskRequest.getRunModeConfig().setEnvironmentId(parseParam.getEnvironmentId()); + taskRequest.setRequestCount(tmpParam.getRequestCount().get()); if (StringUtils.isEmpty(taskRequest.getReportId())) { taskRequest.setRealTime(false); @@ -1327,6 +1329,10 @@ public class ApiScenarioService extends MoveNodeService { return apiExecuteService.execute(runRequest, taskRequest, parseConfig); } + public boolean isRequestStep(ApiScenarioStepCommonDTO step) { + return StringUtils.equalsAny(step.getStepType(), ApiScenarioStepType.API.name(), ApiScenarioStepType.API_CASE.name(), ApiScenarioStepType.CUSTOM_REQUEST.name()); + } + /** * 预生成用例的执行报告 * @@ -1614,6 +1620,11 @@ public class ApiScenarioService extends MoveNodeService { } setPartialRefStepEnable(step, stepDetailMap); + if (isRequestStep(step) && BooleanUtils.isTrue(step.getEnable())) { + // 记录待执行的请求总数 + parseParam.getRequestCount().getAndIncrement(); + } + // 将步骤详情解析生成对应的MsTestElement AbstractMsTestElement msTestElement = stepParser.parseTestElement(step, MapUtils.isNotEmpty(resourceDetailMap) ? resourceDetailMap.getOrDefault(step.getResourceId(), StringUtils.EMPTY) : StringUtils.EMPTY, stepDetailMap.get(step.getId()));