From 86c4e2ad31d90bd1a7789e830393daec7f7aa93a Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 20 Jan 2022 19:05:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=9C=BA=E6=99=AF=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=BD=AC=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=9C=BA=E6=99=AF=E6=B5=8B=E8=AF=95=E8=BD=AC=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E6=B5=8B=E8=AF=95=E6=97=B6=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化场景测试转性能测试时的数据结构处理 --- .../dto/definition/request/MsThreadGroup.java | 8 +- .../request/processors/MsJSR223Processor.java | 6 + .../post/MsJSR223PostProcessor.java | 5 + .../processors/pre/MsJSR223PreProcessor.java | 6 + .../request/sampler/MsDubboSampler.java | 13 +- .../request/sampler/MsHTTPSamplerProxy.java | 7 + .../request/sampler/MsJDBCSampler.java | 7 + .../request/sampler/MsTCPSampler.java | 7 + .../api/service/APITestService.java | 144 +++--------------- .../api/service/ApiAutomationService.java | 10 +- 10 files changed, 77 insertions(+), 136 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsThreadGroup.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsThreadGroup.java index 5ff7baec02..0e07925294 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsThreadGroup.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsThreadGroup.java @@ -95,9 +95,11 @@ public class MsThreadGroup extends MsTestElement { postProcessor.setClazzName("io.metersphere.api.dto.definition.request.processors.MsJSR223Processor"); postProcessor.toHashTree(groupTree, postProcessor.getHashTree(), config); } - MsDebugSampler el = new MsDebugSampler(); - el.setName(RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME); - el.toHashTree(groupTree, el.getHashTree(), config); + if(!config.isOperating()){ + MsDebugSampler el = new MsDebugSampler(); + el.setName(RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME); + el.toHashTree(groupTree, el.getHashTree(), config); + } } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java index 1110d8bff2..a3b73204de 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java @@ -61,6 +61,12 @@ public class MsJSR223Processor extends MsTestElement { } script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); + if(config.isOperating()){ + if (script.startsWith("io.metersphere.utils.JMeterVars.addVars")) { + return; + } + } + // 非导出操作,且不是启用状态则跳过执行 if (!config.isOperating() && !this.isEnable()) { return; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJSR223PostProcessor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJSR223PostProcessor.java index 8e04a288b0..6b1e5670fa 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJSR223PostProcessor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJSR223PostProcessor.java @@ -57,6 +57,11 @@ public class MsJSR223PostProcessor extends MsTestElement { //替换Metersphere环境变量 script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); + if(config.isOperating()){ + if (script.startsWith("io.metersphere.utils.JMeterVars.addVars")) { + return; + } + } // 非导出操作,且不是启用状态则跳过执行 if (!config.isOperating() && !this.isEnable()) { return; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJSR223PreProcessor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJSR223PreProcessor.java index bbbb6635f4..5870b827c5 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJSR223PreProcessor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJSR223PreProcessor.java @@ -57,6 +57,12 @@ public class MsJSR223PreProcessor extends MsTestElement { //替换Metersphere环境变量 script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); + if(config.isOperating()){ + if (script.startsWith("io.metersphere.utils.JMeterVars.addVars")) { + return; + } + } + // 非导出操作,且不是启用状态则跳过执行 if (!config.isOperating() && !this.isEnable()) { return; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java index c4f5ae67e8..9bb8bf2bca 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java @@ -156,15 +156,22 @@ public class MsDubboSampler extends MsTestElement { DubboSample sampler = new DubboSample(); sampler.setEnabled(this.isEnable()); sampler.setName(this.getName()); - sampler.setProperty(TestElement.TEST_CLASS, DubboSample.class.getName()); - sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DubboSampleGui")); + if(config.isOperating()){ + String[] testNameArr = sampler.getName().split("<->"); + if (testNameArr.length > 0) { + String testName = testNameArr[0]; + sampler.setName(testName); + } + }else { + sampler.setProperty(TestElement.TEST_CLASS, DubboSample.class.getName()); + sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DubboSampleGui")); + } sampler.setProperty("MS-ID", this.getId()); String indexPath = this.getIndex(); sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath)); List id_names = new LinkedList<>(); ElementUtil.getScenarioSet(this, id_names); sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names)); - sampler.addTestElement(configCenter(this.getConfigCenter())); sampler.addTestElement(registryCenter(this.getRegistryCenter())); sampler.addTestElement(consumerAndService(this.getConsumerAndService())); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index f502c1fd2a..3773f5656c 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -204,6 +204,13 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (StringUtils.isEmpty(this.getName())) { sampler.setName("HTTPSamplerProxy"); } + if(config.isOperating()){ + String[] testNameArr = sampler.getName().split("<->"); + if (testNameArr.length > 0) { + String testName = testNameArr[0]; + sampler.setName(testName); + } + } sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName()); sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui")); sampler.setProperty("MS-ID", this.getId()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index 7ffee269e4..7d7182b9fd 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -328,6 +328,13 @@ public class MsJDBCSampler extends MsTestElement { JDBCSampler sampler = new JDBCSampler(); sampler.setEnabled(this.isEnable()); sampler.setName(this.getName()); + if(config.isOperating()){ + String[] testNameArr = sampler.getName().split("<->"); + if (testNameArr.length > 0) { + String testName = testNameArr[0]; + sampler.setName(testName); + } + } sampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName()); sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); sampler.setProperty("MS-ID", this.getId()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java index ef80d579e0..46c2774078 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java @@ -259,6 +259,13 @@ public class MsTCPSampler extends MsTestElement { TCPSampler tcpSampler = new TCPSampler(); tcpSampler.setEnabled(this.isEnable()); tcpSampler.setName(this.getName()); + if(config.isOperating()){ + String[] testNameArr = tcpSampler.getName().split("<->"); + if (testNameArr.length > 0) { + String testName = testNameArr[0]; + tcpSampler.setName(testName); + } + } tcpSampler.setProperty("MS-ID", this.getId()); String indexPath = this.getIndex(); tcpSampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath)); diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index 14c7cadc18..6dcd8b7c06 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -455,72 +455,35 @@ public class APITestService { * 更新jmx数据,处理jmx里的各种参数 *

* - * @param jmxString 原JMX文件 - * @param testNameParam 某些节点要替换的testName - * @param isFromScenario 是否来源于场景 (来源于场景的话,testName要进行处理) + * @param jmx 原JMX文件 * @return * @author song tianyang */ - public JmxInfoDTO updateJmxString(String jmxString, String testNameParam, boolean isFromScenario) { - String attribute_testName = "testname"; - String[] requestElementNameArr = new String[]{"HTTPSamplerProxy", "TCPSampler", "JDBCSampler", "DubboSample"}; + public JmxInfoDTO updateJmxString(String jmx) { + jmx = this.updateJmxMessage(jmx); + //获取要转化的文件 List attachmentFilePathList = new ArrayList<>(); - try { - //将ThreadGroup的testname改为接口名称 - Document doc = DocumentHelper.parseText(jmxString);// 获取可续保保单列表报文模板 + Document doc = DocumentHelper.parseText(jmx);// 获取可续保保单列表报文模板 Element root = doc.getRootElement(); Element rootHashTreeElement = root.element("hashTree"); - List innerHashTreeElementList = rootHashTreeElement.elements("hashTree"); for (Element innerHashTreeElement : innerHashTreeElementList) { - //转换DubboDefaultConfigGui - List configTestElementList = innerHashTreeElement.elements("ConfigTestElement"); - for (Element configTestElement : configTestElementList) { - this.updateDubboDefaultConfigGuiElement(configTestElement); - } - - List theadGroupElementList = innerHashTreeElement.elements("ThreadGroup"); - for (Element theadGroupElement : theadGroupElementList) { - if (StringUtils.isNotEmpty(testNameParam)) { - theadGroupElement.attribute(attribute_testName).setText(testNameParam); - } - } - List thirdHashTreeElementList = innerHashTreeElement.elements("hashTree"); + List thirdHashTreeElementList = innerHashTreeElement.elements(); for (Element element : thirdHashTreeElementList) { - String testName = testNameParam; - - //更新请求类节点的部份属性 - this.updateRequestElementInfo(element, testNameParam, requestElementNameArr, isFromScenario); - //检查有无jmeter不是别的自定义参数 - this.checkPrivateFunctionNode(element); - - //转换DubboDefaultConfigGui - List hashTreeConfigTestElementList = element.elements("ConfigTestElement"); - for (Element configTestElement : hashTreeConfigTestElementList) { - this.updateDubboDefaultConfigGuiElement(configTestElement); - } - //HTTPSamplerProxy, 进行附件转化: 1.elementProp里去掉路径; 2。elementProp->filePath获取路径并读出来 attachmentFilePathList.addAll(this.parseAttachmentFileInfo(element)); - - //检查并去掉RunningDebugSampler,转jmx的时候去掉 - this.checkAndRemoveRunningDebugSampler(element); - } //如果存在证书文件,也要匹配出来 - attachmentFilePathList.addAll(this.parseAttachmentFileInfo(innerHashTreeElement)); + attachmentFilePathList.addAll(this.parseAttachmentFileInfo(rootHashTreeElement)); } - jmxString = root.asXML(); } catch (Exception e) { LogUtil.error(e); } - - if (!jmxString.startsWith("")) { - jmxString = "" + jmxString; + if (!jmx.startsWith("")) { + jmx = "" + jmx; } - //处理附件 Map attachmentFiles = new HashMap<>(); //去重处理 @@ -541,7 +504,7 @@ public class APITestService { } } - JmxInfoDTO returnDTO = new JmxInfoDTO("Demo.jmx", jmxString, attachmentFiles); + JmxInfoDTO returnDTO = new JmxInfoDTO("Demo.jmx", jmx, attachmentFiles); returnDTO.setFileMetadataList(fileMetadataList); return returnDTO; } @@ -633,83 +596,17 @@ public class APITestService { return attachmentFilePathList; } - private void updateDubboDefaultConfigGuiElement(Element configTestElement) { - String dubboDefaultConfigGuiClassName = "io.github.ningyu.jmeter.plugin.dubbo.gui.DubboDefaultConfigGui"; - if (configTestElement == null) { - return; - } - String guiClassValue = configTestElement.attributeValue("guiclass"); - if (StringUtils.equals(guiClassValue, "DubboDefaultConfigGui")) { - configTestElement.attribute("guiclass").setText(dubboDefaultConfigGuiClassName); + private String updateJmxMessage(String jmx) { + if (StringUtils.isNotEmpty(jmx)) { + jmx = StringUtils.replace(jmx, "", ""); + jmx = StringUtils.replace(jmx, " guiclass=\"DubboSampleGui\" ", " guiclass=\"io.github.ningyu.jmeter.plugin.dubbo.gui.DubboSampleGui\" "); + jmx = StringUtils.replace(jmx, " guiclass=\"DubboDefaultConfigGui\" ", " guiclass=\"io.github.ningyu.jmeter.plugin.dubbo.gui.DubboDefaultConfigGui\" "); + jmx = StringUtils.replace(jmx, " testclass=\"DubboSample\" ", " testclass=\"io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample\" "); } + return jmx; } - private void checkPrivateFunctionNode(Element element) { - List scriptHashTreeElementList = element.elements("hashTree"); - for (Element scriptHashTreeElement : scriptHashTreeElementList) { - boolean isRemove = false; - List removeElement = new ArrayList<>(); - List scriptElementItemList = scriptHashTreeElement.elements(); - for (Element hashTreeItemElement : scriptElementItemList) { - String className = hashTreeItemElement.attributeValue("testclass"); - String qname = hashTreeItemElement.getQName().getName(); - - if (isRemove) { - if (org.apache.commons.lang3.StringUtils.equals("hashTree", qname)) { - removeElement.add(hashTreeItemElement); - } - } - - isRemove = false; - if (org.apache.commons.lang3.StringUtils.equals(className, "JSR223PostProcessor")) { - List scriptElements = hashTreeItemElement.elements("stringProp"); - for (Element scriptElement : scriptElements) { - String scriptName = scriptElement.attributeValue("name"); - String contentValue = scriptElement.getStringValue(); - - if ("script".equals(scriptName) && contentValue.startsWith("io.metersphere.utils.JMeterVars.addVars")) { - isRemove = true; - removeElement.add(hashTreeItemElement); - } - } - } - } - for (Element itemElement : removeElement) { - scriptHashTreeElement.remove(itemElement); - } - } - } - - private void updateRequestElementInfo(Element element, String testNameParam, String[] requestElementNameArr, boolean isFromScenario) { - String attribute_testName = "testname"; - String scenarioCaseNameSplit = "<->"; - String testName = testNameParam; - - for (String requestElementName : requestElementNameArr) { - List sampleProxyElementList = element.elements(requestElementName); - for (Element itemElement : sampleProxyElementList) { - if (isFromScenario) { - testName = itemElement.attributeValue(attribute_testName); - if (StringUtils.isNotBlank(testName)) { - String[] testNameArr = testName.split(scenarioCaseNameSplit); - if (testNameArr.length > 0) { - testName = testNameArr[0]; - } - } - } - itemElement.attribute(attribute_testName).setText(testName); - - //double的话有额外处理方式 - if (StringUtils.equals(requestElementName, "DubboSample")) { - //dubbo节点要更新 标签、guiClass 和 testClass - itemElement.setName("io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample"); - itemElement.attribute("testclass").setText("io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample"); - itemElement.attribute("guiclass").setText("io.github.ningyu.jmeter.plugin.dubbo.gui.DubboSampleGui"); - } - - } - } - } public JmxInfoDTO getJmxInfoDTO(RunDefinitionRequest runRequest, List bodyFiles) { ParameterConfig config = new ParameterConfig(); @@ -725,11 +622,8 @@ public class APITestService { } HashTree hashTree = runRequest.getTestElement().generateHashTree(config); String jmxString = runRequest.getTestElement().getJmx(hashTree); - - String testName = runRequest.getName(); - //将jmx处理封装为通用方法 - JmxInfoDTO dto = updateJmxString(jmxString, testName, false); + JmxInfoDTO dto = updateJmxString(jmxString); dto.setName(runRequest.getName() + ".jmx"); return dto; } 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 ee2aea6bdc..f5e29bc1f6 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -1116,7 +1116,7 @@ public class ApiAutomationService { } MsTestPlan testPlan = new MsTestPlan(); testPlan.setHashTree(new LinkedList<>()); - JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(apiScenarios.get(0)), testName, true); + JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(apiScenarios.get(0))); String name = request.getName() + ".jmx"; dto.setName(name); @@ -1465,8 +1465,8 @@ public class ApiAutomationService { if (StringUtils.isNotEmpty(item.getScenarioDefinition())) { String jmx = generateJmx(item); if (StringUtils.isNotEmpty(jmx)) { - ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx, null, true).getXml()); - JmxInfoDTO dto = apiTestService.updateJmxString(jmx, item.getName(), true); + ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx).getXml()); + JmxInfoDTO dto = apiTestService.updateJmxString(jmx); scenrioExportJmx.setId(item.getId()); scenrioExportJmx.setVersion(item.getVersion()); //扫描需要哪些文件 @@ -1492,7 +1492,7 @@ public class ApiAutomationService { if (StringUtils.isNotEmpty(item.getScenarioDefinition())) { String jmx = generateJmx(item); if (StringUtils.isNotEmpty(jmx)) { - ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx, null, true).getXml()); + ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx).getXml()); String fileName = item.getName() + ".jmx"; String jmxStr = scenrioExportJmx.getJmx(); files.put(fileName, jmxStr.getBytes(StandardCharsets.UTF_8)); @@ -1882,7 +1882,7 @@ public class ApiAutomationService { String testName = item.getName(); MsTestPlan testPlan = new MsTestPlan(); testPlan.setHashTree(new LinkedList<>()); - JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item), testName, true); + JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item)); String name = item.getName() + ".jmx"; dto.setId(item.getId()); dto.setName(name);