diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java index ad53bee01b..dd773fdf9b 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -358,7 +358,7 @@ public class ApiAutomationController { @PostMapping(value = "/export/jmx") @RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ_EXPORT_SCENARIO) @MsAuditLog(module = "api_automation", type = OperLogConstants.EXPORT, sourceId = "#request.id", title = "#request.name", project = "#request.projectId") - public List exportJmx(@RequestBody ApiScenarioBatchRequest request) { + public List exportJmx(@RequestBody ApiScenarioBatchRequest request) { return apiAutomationService.exportJmx(request); } diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenrioExportJmx.java b/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioExportJmxDTO.java similarity index 81% rename from backend/src/main/java/io/metersphere/api/dto/automation/ApiScenrioExportJmx.java rename to backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioExportJmxDTO.java index f38f7fd2b9..a7455fef61 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenrioExportJmx.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioExportJmxDTO.java @@ -8,7 +8,7 @@ import java.util.List; @Getter @Setter -public class ApiScenrioExportJmx { +public class ApiScenarioExportJmxDTO { private String name; private String id; private String jmx; @@ -17,7 +17,7 @@ public class ApiScenrioExportJmx { //性能测试引用场景时需要场景下的附件 private List fileMetadataList; - public ApiScenrioExportJmx(String name, String jmx) { + public ApiScenarioExportJmxDTO(String name, String jmx) { this.name = name; this.jmx = jmx; } 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 b024d60b47..5323e2e36a 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 @@ -40,6 +40,8 @@ import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.AbstractTestElement; import org.apache.jmeter.testelement.TestElement; +import org.apache.jmeter.testelement.TestPlan; +import org.apache.jmeter.threads.ThreadGroup; import org.apache.jorphan.collections.HashTree; import java.io.ByteArrayOutputStream; @@ -597,4 +599,28 @@ public class ElementUtil { sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(id, config, parent, indexPath)); LoggerUtil.debug("mqtt sampler resourceId :" + sampler.getPropertyAsString("MS-RESOURCE-ID")); } + + public static void accuracyHashTree(HashTree hashTree) { + Map objects = new LinkedHashMap<>(); + Object groupHashTree = hashTree; + if (hashTree != null && hashTree.size() > 0) { + for (Object key : hashTree.keySet()) { + if (key instanceof TestPlan) { + for (Object node : hashTree.get(key).keySet()) { + if (node instanceof ThreadGroup) { + groupHashTree = hashTree.get(key).get(node); + } + } + } else { + objects.put(key, hashTree.get(key)); + } + } + } + if (!objects.isEmpty() && groupHashTree instanceof HashTree) { + for (Object key : objects.keySet()) { + hashTree.remove(key); + ((HashTree) groupHashTree).add(key, objects.get(key)); + } + } + } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java index 5259a88796..c7c3cadf66 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java @@ -1,7 +1,6 @@ package io.metersphere.api.dto.definition.request.unknown; import com.alibaba.fastjson.annotation.JSONType; -import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.scenario.request.BodyFile; @@ -24,7 +23,6 @@ import org.apache.jorphan.collections.HashTree; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; -import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; @@ -79,20 +77,9 @@ public class MsJmeterElement extends MsTestElement { config.getCsvFilePaths().add(csvPath); } } - if (scriptWrapper instanceof TestPlan && tree.size() > 0) { - for (Object obj : tree.keySet()) { - if (obj instanceof CSVDataSet) { - tree.remove(obj); - } - } - } if (config.isOperating()) { elementTree = tree.add(scriptWrapper); - if ((scriptWrapper instanceof ThreadGroup)) { - ElementUtil.addCsvDataSet(elementTree, config.getVariables(), config, "shareMode.thread"); - } - } else if (!(scriptWrapper instanceof TestPlan) && !(scriptWrapper instanceof ThreadGroup)) { elementTree = tree.add(scriptWrapper); } 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 70514b816c..62e7c825b2 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -865,6 +865,7 @@ public class ApiAutomationService { if (scenario == null) { return null; } + scenario.setId(apiScenario.getId()); GenerateHashTreeUtil.parse(apiScenario.getScenarioDefinition(), scenario); String environmentType = apiScenario.getEnvironmentType(); String environmentJson = apiScenario.getEnvironmentJson(); @@ -876,8 +877,17 @@ public class ApiAutomationService { scenario.setEnvironmentMap(envMap); } // 针对导入的jmx 处理 - if (CollectionUtils.isNotEmpty(scenario.getHashTree()) && (scenario.getHashTree().get(0) instanceof MsJmeterElement)) { + boolean isUseElement = false; + if (CollectionUtils.isNotEmpty(scenario.getHashTree())) { + for (MsTestElement testElement : scenario.getHashTree()) { + if (testElement instanceof MsJmeterElement) { + isUseElement = true; + } + } + } + if (isUseElement) { scenario.toHashTree(jmeterHashTree, scenario.getHashTree(), config); + ElementUtil.accuracyHashTree(jmeterHashTree); return scenario.getJmx(jmeterHashTree); } else { MsThreadGroup group = new MsThreadGroup(); @@ -892,10 +902,12 @@ public class ApiAutomationService { } } catch (Exception ex) { - ex.printStackTrace(); + LogUtil.error(ex); MSException.throwException(ex.getMessage()); } testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), config); + // 检查hashTree 准确性 + return testPlan.getJmx(jmeterHashTree); } @@ -1475,21 +1487,21 @@ public class ApiAutomationService { return result; } - public List exportJmx(ApiScenarioBatchRequest request) { + public List exportJmx(ApiScenarioBatchRequest request) { List apiScenarioWithBLOBs = getExportResult(request); // 生成jmx - List resList = new ArrayList<>(); + List resList = new ArrayList<>(); apiScenarioWithBLOBs.forEach(item -> { if (StringUtils.isNotEmpty(item.getScenarioDefinition())) { String jmx = generateJmx(item); if (StringUtils.isNotEmpty(jmx)) { - ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId()).getXml()); + ApiScenarioExportJmxDTO scenariosExportJmx = new ApiScenarioExportJmxDTO(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId()).getXml()); JmxInfoDTO dto = apiTestService.updateJmxString(jmx, item.getProjectId()); - scenrioExportJmx.setId(item.getId()); - scenrioExportJmx.setVersion(item.getVersion()); + scenariosExportJmx.setId(item.getId()); + scenariosExportJmx.setVersion(item.getVersion()); //扫描需要哪些文件 - scenrioExportJmx.setFileMetadataList(dto.getFileMetadataList()); - resList.add(scenrioExportJmx); + scenariosExportJmx.setFileMetadataList(dto.getFileMetadataList()); + resList.add(scenariosExportJmx); } } }); @@ -1503,24 +1515,24 @@ public class ApiAutomationService { } public byte[] exportZip(ApiScenarioBatchRequest request) { - List apiScenarioWithBLOBs = getExportResult(request); + List scenarios = getExportResult(request); // 生成jmx Map files = new LinkedHashMap<>(); - apiScenarioWithBLOBs.forEach(item -> { + scenarios.forEach(item -> { if (StringUtils.isNotEmpty(item.getScenarioDefinition())) { String jmx = generateJmx(item); if (StringUtils.isNotEmpty(jmx)) { - ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId()).getXml()); + ApiScenarioExportJmxDTO scenariosExportJmx = new ApiScenarioExportJmxDTO(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId()).getXml()); String fileName = item.getName() + ".jmx"; - String jmxStr = scenrioExportJmx.getJmx(); + String jmxStr = scenariosExportJmx.getJmx(); files.put(fileName, jmxStr.getBytes(StandardCharsets.UTF_8)); } } }); - if (CollectionUtils.isNotEmpty(apiScenarioWithBLOBs)) { - List names = apiScenarioWithBLOBs.stream().map(ApiScenarioWithBLOBs::getName).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(scenarios)) { + List names = scenarios.stream().map(ApiScenarioWithBLOBs::getName).collect(Collectors.toList()); request.setName(String.join(",", names)); - List ids = apiScenarioWithBLOBs.stream().map(ApiScenarioWithBLOBs::getId).collect(Collectors.toList()); + List ids = scenarios.stream().map(ApiScenarioWithBLOBs::getId).collect(Collectors.toList()); request.setId(JSON.toJSONString(ids)); } return FileUtils.listBytesToZip(files); diff --git a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java index 4667bc3598..514c88d549 100644 --- a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java +++ b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java @@ -5,7 +5,7 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import io.metersphere.api.dto.JmxInfoDTO; import io.metersphere.api.dto.automation.ApiScenarioBatchRequest; -import io.metersphere.api.dto.automation.ApiScenrioExportJmx; +import io.metersphere.api.dto.automation.ApiScenarioExportJmxDTO; import io.metersphere.api.service.ApiAutomationService; import io.metersphere.api.service.ApiTestCaseService; import io.metersphere.base.domain.*; @@ -795,7 +795,7 @@ public class PerformanceTestService { if (!CollectionUtils.isEmpty(scenarioIds)) { ApiScenarioBatchRequest scenarioRequest = new ApiScenarioBatchRequest(); scenarioRequest.setIds(scenarioIds); - List apiScenrioExportJmxes = apiAutomationService.exportJmx(scenarioRequest); + List apiScenrioExportJmxes = apiAutomationService.exportJmx(scenarioRequest); deleteLoadTestFiles(loadTest.getId());