From a7c1fc434df53727d1cef896bcb963c6728c9df4 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 30 Aug 2021 19:28:02 +0800 Subject: [PATCH] =?UTF-8?q?fix=20(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E6=89=B9=E9=87=8F=E6=89=A7=E8=A1=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiJmeterFileController.java | 16 ++++--- .../metersphere/api/jmeter/JMeterService.java | 1 + .../api/service/ApiJmeterFileService.java | 23 ++++++++++ .../track/service/TestPlanApiCaseService.java | 2 - .../service/task/ParallelApiExecTask.java | 46 ------------------- .../src/main/resources/application.properties | 3 +- 6 files changed, 36 insertions(+), 55 deletions(-) delete mode 100644 backend/src/main/java/io/metersphere/track/service/task/ParallelApiExecTask.java diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiJmeterFileController.java b/backend/src/main/java/io/metersphere/api/controller/ApiJmeterFileController.java index ecbac9e107..f31130ea5b 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiJmeterFileController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiJmeterFileController.java @@ -20,7 +20,7 @@ public class ApiJmeterFileController { private ApiJmeterFileService apiJmeterFileService; @PostMapping("download/files") - public ResponseEntity downloadJmeterFiles(@RequestBody List bodyFileList) { + public ResponseEntity downloadBodyFiles(@RequestBody List bodyFileList) { byte[] bytes = new byte[10]; if (CollectionUtils.isNotEmpty(bodyFileList)) { bytes = apiJmeterFileService.downloadJmeterFiles(bodyFileList); @@ -31,11 +31,6 @@ public class ApiJmeterFileController { .body(bytes); } - @GetMapping("download") - public byte[] downloadJmx(@RequestParam("testId") String testId, @RequestParam("reportId") String reportId, @RequestParam("runMode") String runMode, @RequestParam("testPlanScenarioId") String testPlanScenarioId) { - return apiJmeterFileService.downloadJmx(runMode, testId, reportId, testPlanScenarioId); - } - @GetMapping("download/jar") public ResponseEntity downloadJmeterFiles() { byte[] bytes = apiJmeterFileService.downloadJmeterJar(); @@ -44,4 +39,13 @@ public class ApiJmeterFileController { .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + UUID.randomUUID().toString() + ".zip\"") .body(bytes); } + + @GetMapping("download") + public ResponseEntity downloadJmeterFiles(@RequestParam("testId") String testId, @RequestParam("reportId") String reportId, @RequestParam("runMode") String runMode, @RequestParam("testPlanScenarioId") String testPlanScenarioId) { + byte[] bytes = apiJmeterFileService.downloadJmeterFiles(runMode,testId, reportId, testPlanScenarioId); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + testId + ".zip\"") + .body(bytes); + } } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java index d705400d2d..c59d2bae84 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java @@ -177,6 +177,7 @@ public class JMeterService { } else { runRequest.setJmx(new MsTestPlan().getJmx(hashTree)); kafkaTemplate.send(MsKafkaListener.EXEC_TOPIC, JSON.toJSONString(runRequest)); + kafkaTemplate.flush(); } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java b/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java index 6d0bf8662f..b819a8ea57 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java @@ -52,6 +52,29 @@ public class ApiJmeterFileService { return listBytesToZip(files); } + public byte[] downloadJmeterFiles(String runMode, String testId, String reportId, String testPlanScenarioId) { + Map planEnvMap = new HashMap<>(); + if (StringUtils.isNotEmpty(testPlanScenarioId)) { + // 获取场景用例单独的执行环境 + TestPlanApiScenario planApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(testPlanScenarioId); + String environment = planApiScenario.getEnvironment(); + if (StringUtils.isNotBlank(environment)) { + planEnvMap = JSON.parseObject(environment, Map.class); + } + } + HashTree hashTree = null; + if (ApiRunMode.DEFINITION.name().equals(runMode) || ApiRunMode.API_PLAN.name().equals(runMode)) { + hashTree = testPlanApiCaseService.generateHashTree(testId); + } else { + ApiScenarioWithBLOBs item = apiScenarioMapper.selectByPrimaryKey(testId); + if (item == null) { + MSException.throwException("未找到执行场景。"); + } + hashTree = apiAutomationService.generateHashTree(item, reportId, planEnvMap); + } + return zipFilesToByteArray(testId, hashTree); + } + public byte[] downloadJmx(String runMode, String testId, String reportId, String testPlanScenarioId) { Map planEnvMap = new HashMap<>(); if (StringUtils.isNotEmpty(testPlanScenarioId)) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java index 66a64954bc..14fcf0771c 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java @@ -38,7 +38,6 @@ import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.service.SystemParameterService; import io.metersphere.track.dto.*; import io.metersphere.track.request.testcase.TestPlanApiCaseBatchRequest; -import io.metersphere.track.service.task.ParallelApiExecTask; import io.metersphere.track.service.task.SerialApiExecTask; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; @@ -450,7 +449,6 @@ public class TestPlanApiCaseService { } ApiDefinitionExecResult report = addResult(request, key, APITestStatus.Running.name(), batchMapper); modeDataDTO.setApiCaseId(report.getId()); - executorService.submit(new ParallelApiExecTask(jMeterService, mapper, modeDataDTO, request.getConfig(), ApiRunMode.API_PLAN.name())); if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) { jMeterService.runTest(modeDataDTO.getTestId(), modeDataDTO.getApiCaseId(), ApiRunMode.API_PLAN.name(), null, request.getConfig(), hashTree); } else { diff --git a/backend/src/main/java/io/metersphere/track/service/task/ParallelApiExecTask.java b/backend/src/main/java/io/metersphere/track/service/task/ParallelApiExecTask.java deleted file mode 100644 index 9f123dd8b0..0000000000 --- a/backend/src/main/java/io/metersphere/track/service/task/ParallelApiExecTask.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * - */ -package io.metersphere.track.service.task; - -import io.metersphere.api.dto.RunModeDataDTO; -import io.metersphere.api.dto.automation.RunModeConfig; -import io.metersphere.api.jmeter.JMeterService; -import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; -import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.LogUtil; -import org.apache.commons.lang3.StringUtils; - -import java.util.concurrent.Callable; - -public class ParallelApiExecTask implements Callable { - private RunModeConfig config; - private JMeterService jMeterService; - private RunModeDataDTO runModeDataDTO; - private String runMode; - private ApiDefinitionExecResultMapper mapper; - - public ParallelApiExecTask(JMeterService jMeterService, ApiDefinitionExecResultMapper mapper, RunModeDataDTO runModeDataDTO, RunModeConfig config, String runMode) { - this.jMeterService = jMeterService; - this.config = config; - this.runModeDataDTO = runModeDataDTO; - this.runMode = runMode; - this.mapper = mapper; - } - - @Override - public T call() { - try { - if (config != null && StringUtils.isNotBlank(config.getResourcePoolId())) { - jMeterService.runTest(runModeDataDTO.getTestId(), runModeDataDTO.getApiCaseId(), runMode, null, config, runModeDataDTO.getHashTree()); - } else { - jMeterService.runLocal(runModeDataDTO.getApiCaseId(), runModeDataDTO.getHashTree(), runModeDataDTO.getReport() != null ? runModeDataDTO.getReport().getTriggerMode() : null, runMode); - } - return null; - } catch (Exception ex) { - LogUtil.error(ex); - MSException.throwException(ex.getMessage()); - return null; - } - } -} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 728cc8c897..1bbec3fdcf 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -52,8 +52,9 @@ spring.flyway.encoding=UTF-8 spring.flyway.validate-on-migrate=false spring.kafka.listener.missing-topics-fatal=false spring.kafka.producer.properties.max.request.size=32428800 -spring.messages.basename=i18n/messages +spring.kafka.producer.batch-size=16384 +spring.messages.basename=i18n/messages # kafka kafka.fields= kafka.timestamp=yyyy-MM-dd'T'HH:mm:ss.SSSZZ