From 78f15b801c82e9c667b03b8d16eeae58557f6566 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 16 Apr 2021 18:05:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8=E5=8C=96?= =?UTF-8?q?=E5=88=86=E5=A4=9A=E6=8E=A5=E7=82=B9=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/api/dto/RunRequest.java | 2 + .../metersphere/api/jmeter/JMeterService.java | 46 +++++++++++-------- .../api/jmeter/MsKafkaListener.java | 7 +-- .../api/jmeter/ResourcePoolCalculation.java | 5 +- .../io/metersphere/api/jmeter/TestResult.java | 2 + .../api/service/ApiAutomationService.java | 7 +-- .../api/service/ApiDefinitionService.java | 3 +- 7 files changed, 43 insertions(+), 29 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/RunRequest.java b/backend/src/main/java/io/metersphere/api/dto/RunRequest.java index 8fa20f21db..760ca12fdf 100644 --- a/backend/src/main/java/io/metersphere/api/dto/RunRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/RunRequest.java @@ -8,6 +8,8 @@ import lombok.Setter; @Setter public class RunRequest { private String testId; + private String userId; + private boolean isDebug; private String runMode; private String jmx; private RunModeConfig config; 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 f53edac72e..861692eaf0 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java @@ -11,6 +11,7 @@ import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; +import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.UrlTestUtils; import io.metersphere.config.JmeterProperties; import io.metersphere.dto.BaseSystemConfigDTO; @@ -49,6 +50,7 @@ import java.lang.reflect.Field; import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.Objects; @Service public class JMeterService { @@ -226,13 +228,16 @@ public class JMeterService { file = new File(path + "/"); } FileSystemResource resource = new FileSystemResource(file); - ByteArrayResource byteArrayResource = new ByteArrayResource(this.fileToByte(file)) { - @Override - public String getFilename() throws IllegalStateException { - return resource.getFilename(); - } - }; - jarFiles.add(byteArrayResource); + byte[] fileByte = this.fileToByte(file); + if (fileByte != null) { + ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { + @Override + public String getFilename() throws IllegalStateException { + return resource.getFilename(); + } + }; + jarFiles.add(byteArrayResource); + } } catch (Exception e) { LogUtil.error(e.getMessage(), e); @@ -251,20 +256,23 @@ public class JMeterService { File file = new File(bodyFile.getName()); if (file != null && !file.exists()) { FileSystemResource resource = new FileSystemResource(file); - ByteArrayResource byteArrayResource = new ByteArrayResource(this.fileToByte(file)) { - @Override - public String getFilename() throws IllegalStateException { - return resource.getFilename(); - } - }; - multipartFiles.add(byteArrayResource); + byte[] fileByte = this.fileToByte(file); + if (fileByte != null) { + ByteArrayResource byteArrayResource = new ByteArrayResource(fileByte) { + @Override + public String getFilename() throws IllegalStateException { + return resource.getFilename(); + } + }; + multipartFiles.add(byteArrayResource); + } } } } return multipartFiles; } - public void runTest(String testId, HashTree hashTree, String runMode, RunModeConfig config) { + public void runTest(String testId, HashTree hashTree, String runMode, boolean isDebug, RunModeConfig config) { // 获取JMX使用到的附件 List multipartFiles = getMultipartFiles(hashTree); // 获取JAR @@ -296,8 +304,10 @@ public class JMeterService { try { RunRequest runRequest = new RunRequest(); runRequest.setTestId(testId); + runRequest.setDebug(isDebug); runRequest.setRunMode(runMode); runRequest.setConfig(config); + runRequest.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); runRequest.setJmx(new MsTestPlan().getJmx(hashTree)); MultiValueMap postParameters = new LinkedMultiValueMap<>(); postParameters.put("files", multipartFiles); @@ -310,11 +320,9 @@ public class JMeterService { HttpEntity> request = new HttpEntity<>(postParameters, headers); String result = restTemplate.postForObject(uri, request, String.class); - if (result == null) { - MSException.throwException(Translator.get("start_engine_fail")); + if (result == null || !StringUtils.equals("SUCCESS",result)) { + MSException.throwException("执行失败:"+ result); } - } catch (MSException e) { - throw e; } catch (Exception e) { e.printStackTrace(); MSException.throwException("Please check node-controller status."); diff --git a/backend/src/main/java/io/metersphere/api/jmeter/MsKafkaListener.java b/backend/src/main/java/io/metersphere/api/jmeter/MsKafkaListener.java index 38b6109db1..76cdbba766 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/MsKafkaListener.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/MsKafkaListener.java @@ -146,10 +146,11 @@ public class MsKafkaListener { } } - if (report != null && StringUtils.equals(ReportTriggerMode.API.name(), report.getTriggerMode()) || StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), report.getTriggerMode())) { - sendTask(report, reportUrl, testResult); + if (report != null) { + if (StringUtils.equals(ReportTriggerMode.API.name(), report.getTriggerMode()) || StringUtils.equals(ReportTriggerMode.SCHEDULE.name(), report.getTriggerMode())) { + sendTask(report, reportUrl, testResult); + } } - } private static void sendTask(ApiTestReport report, String reportUrl, TestResult testResult) { diff --git a/backend/src/main/java/io/metersphere/api/jmeter/ResourcePoolCalculation.java b/backend/src/main/java/io/metersphere/api/jmeter/ResourcePoolCalculation.java index 65f4eb87d6..914358abc8 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/ResourcePoolCalculation.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/ResourcePoolCalculation.java @@ -7,7 +7,6 @@ import io.metersphere.base.domain.TestResourcePoolExample; import io.metersphere.base.mapper.TestResourceMapper; import io.metersphere.base.mapper.TestResourcePoolMapper; import io.metersphere.commons.exception.MSException; -import io.metersphere.i18n.Translator; import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; @@ -25,7 +24,7 @@ public class ResourcePoolCalculation { public TestResource getPool() { // 获取可以执行的资源池 TestResourcePoolExample example = new TestResourcePoolExample(); - example.createCriteria().andStatusEqualTo("VALID").andTypeEqualTo("NODE").andNameEqualTo("赵勇资源池"); + example.createCriteria().andStatusEqualTo("VALID").andTypeEqualTo("NODE"); List pools = testResourcePoolMapper.selectByExample(example); // 暂时随机获取一个正常状态NODE TestResource testResource = null; @@ -38,7 +37,7 @@ public class ResourcePoolCalculation { testResource = testResources.get(index); } if (testResource == null) { - MSException.throwException(Translator.get("run_load_test_file_init_error")); + MSException.throwException("未获取到资源池,请检查配置【系统设置-系统-测试资源池】"); } return testResource; } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/TestResult.java b/backend/src/main/java/io/metersphere/api/jmeter/TestResult.java index 551e427b16..bbda0264c8 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/TestResult.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/TestResult.java @@ -14,6 +14,8 @@ public class TestResult { private String reportName; + private String userId; + private boolean isDebug; private String runMode; 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 a9db597bc6..3a5a1a3fa1 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -884,7 +884,8 @@ public class ApiAutomationService { // 调用执行方法 List reportIds = new LinkedList<>(); HashTree hashTree = generateHashTree(apiScenarios, request, reportIds); - jMeterService.runSerial(JSON.toJSONString(reportIds), hashTree, request.getReportId(), runMode, request.getConfig()); + // jMeterService.runSerial(JSON.toJSONString(reportIds), hashTree, request.getReportId(), runMode, request.getConfig()); + jMeterService.runTest(JSON.toJSONString(reportIds), hashTree, runMode, false, request.getConfig()); return request.getId(); } @@ -951,9 +952,9 @@ public class ApiAutomationService { createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(), SessionUtils.getUserId(), null); // 调用执行方法 - // jMeterService.runTest(request.getId(), hashTree, ApiRunMode.SCENARIO.name(), null); + jMeterService.runTest(request.getId(), hashTree, ApiRunMode.SCENARIO.name(), true, null); // 调用执行方法 - jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), ApiRunMode.SCENARIO.name()); + // jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), ApiRunMode.SCENARIO.name()); return request.getId(); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 031cb637ed..ce055f9c10 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -535,7 +535,8 @@ public class ApiDefinitionService { runMode = ApiRunMode.API_PLAN.name(); } // 调用执行方法 - jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode); + //jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode); + jMeterService.runTest(request.getId(), hashTree, runMode, request.getReportId() != null, null); return request.getId(); }