fix(场景测试转性能测试): 优化场景测试转性能测试时的数据结构处理

优化场景测试转性能测试时的数据结构处理
This commit is contained in:
song-tianyang 2022-01-20 19:05:40 +08:00 committed by 刘瑞斌
parent 106139367f
commit c8102b9941
10 changed files with 77 additions and 136 deletions

View File

@ -95,9 +95,11 @@ public class MsThreadGroup extends MsTestElement {
postProcessor.setClazzName("io.metersphere.api.dto.definition.request.processors.MsJSR223Processor"); postProcessor.setClazzName("io.metersphere.api.dto.definition.request.processors.MsJSR223Processor");
postProcessor.toHashTree(groupTree, postProcessor.getHashTree(), config); postProcessor.toHashTree(groupTree, postProcessor.getHashTree(), config);
} }
MsDebugSampler el = new MsDebugSampler(); if(!config.isOperating()){
el.setName(RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME); MsDebugSampler el = new MsDebugSampler();
el.toHashTree(groupTree, el.getHashTree(), config); el.setName(RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME);
el.toHashTree(groupTree, el.getHashTree(), config);
}
} }
} }

View File

@ -61,6 +61,12 @@ public class MsJSR223Processor extends MsTestElement {
} }
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); 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()) { if (!config.isOperating() && !this.isEnable()) {
return; return;

View File

@ -57,6 +57,11 @@ public class MsJSR223PostProcessor extends MsTestElement {
//替换Metersphere环境变量 //替换Metersphere环境变量
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); 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()) { if (!config.isOperating() && !this.isEnable()) {
return; return;

View File

@ -57,6 +57,12 @@ public class MsJSR223PreProcessor extends MsTestElement {
//替换Metersphere环境变量 //替换Metersphere环境变量
script = StringUtils.replace(script, RunningParamKeys.API_ENVIRONMENT_ID, "\"" + RunningParamKeys.RUNNING_PARAMS_PREFIX + this.getEnvironmentId() + ".\""); 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()) { if (!config.isOperating() && !this.isEnable()) {
return; return;

View File

@ -156,15 +156,22 @@ public class MsDubboSampler extends MsTestElement {
DubboSample sampler = new DubboSample(); DubboSample sampler = new DubboSample();
sampler.setEnabled(this.isEnable()); sampler.setEnabled(this.isEnable());
sampler.setName(this.getName()); sampler.setName(this.getName());
sampler.setProperty(TestElement.TEST_CLASS, DubboSample.class.getName()); if(config.isOperating()){
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DubboSampleGui")); 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()); sampler.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex(); String indexPath = this.getIndex();
sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath)); sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath));
List<String> id_names = new LinkedList<>(); List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names); ElementUtil.getScenarioSet(this, id_names);
sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names)); sampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));
sampler.addTestElement(configCenter(this.getConfigCenter())); sampler.addTestElement(configCenter(this.getConfigCenter()));
sampler.addTestElement(registryCenter(this.getRegistryCenter())); sampler.addTestElement(registryCenter(this.getRegistryCenter()));
sampler.addTestElement(consumerAndService(this.getConsumerAndService())); sampler.addTestElement(consumerAndService(this.getConsumerAndService()));

View File

@ -204,6 +204,13 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (StringUtils.isEmpty(this.getName())) { if (StringUtils.isEmpty(this.getName())) {
sampler.setName("HTTPSamplerProxy"); 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.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui")); sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui"));
sampler.setProperty("MS-ID", this.getId()); sampler.setProperty("MS-ID", this.getId());

View File

@ -328,6 +328,13 @@ public class MsJDBCSampler extends MsTestElement {
JDBCSampler sampler = new JDBCSampler(); JDBCSampler sampler = new JDBCSampler();
sampler.setEnabled(this.isEnable()); sampler.setEnabled(this.isEnable());
sampler.setName(this.getName()); 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.TEST_CLASS, JDBCSampler.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
sampler.setProperty("MS-ID", this.getId()); sampler.setProperty("MS-ID", this.getId());

View File

@ -259,6 +259,13 @@ public class MsTCPSampler extends MsTestElement {
TCPSampler tcpSampler = new TCPSampler(); TCPSampler tcpSampler = new TCPSampler();
tcpSampler.setEnabled(this.isEnable()); tcpSampler.setEnabled(this.isEnable());
tcpSampler.setName(this.getName()); 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()); tcpSampler.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex(); String indexPath = this.getIndex();
tcpSampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath)); tcpSampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(this.getId(), config, this.getParent(), indexPath));

View File

@ -455,72 +455,35 @@ public class APITestService {
* 更新jmx数据处理jmx里的各种参数 * 更新jmx数据处理jmx里的各种参数
* <p> * <p>
* *
* @param jmxString 原JMX文件 * @param jmx 原JMX文件
* @param testNameParam 某些节点要替换的testName
* @param isFromScenario 是否来源于场景 来源于场景的话testName要进行处理
* @return * @return
* @author song tianyang * @author song tianyang
*/ */
public JmxInfoDTO updateJmxString(String jmxString, String testNameParam, boolean isFromScenario) { public JmxInfoDTO updateJmxString(String jmx) {
String attribute_testName = "testname"; jmx = this.updateJmxMessage(jmx);
String[] requestElementNameArr = new String[]{"HTTPSamplerProxy", "TCPSampler", "JDBCSampler", "DubboSample"};
//获取要转化的文件
List<String> attachmentFilePathList = new ArrayList<>(); List<String> attachmentFilePathList = new ArrayList<>();
try { try {
//将ThreadGroup的testname改为接口名称 Document doc = DocumentHelper.parseText(jmx);// 获取可续保保单列表报文模板
Document doc = DocumentHelper.parseText(jmxString);// 获取可续保保单列表报文模板
Element root = doc.getRootElement(); Element root = doc.getRootElement();
Element rootHashTreeElement = root.element("hashTree"); Element rootHashTreeElement = root.element("hashTree");
List<Element> innerHashTreeElementList = rootHashTreeElement.elements("hashTree"); List<Element> innerHashTreeElementList = rootHashTreeElement.elements("hashTree");
for (Element innerHashTreeElement : innerHashTreeElementList) { for (Element innerHashTreeElement : innerHashTreeElementList) {
//转换DubboDefaultConfigGui List<Element> thirdHashTreeElementList = innerHashTreeElement.elements();
List<Element> configTestElementList = innerHashTreeElement.elements("ConfigTestElement");
for (Element configTestElement : configTestElementList) {
this.updateDubboDefaultConfigGuiElement(configTestElement);
}
List<Element> theadGroupElementList = innerHashTreeElement.elements("ThreadGroup");
for (Element theadGroupElement : theadGroupElementList) {
if (StringUtils.isNotEmpty(testNameParam)) {
theadGroupElement.attribute(attribute_testName).setText(testNameParam);
}
}
List<Element> thirdHashTreeElementList = innerHashTreeElement.elements("hashTree");
for (Element element : thirdHashTreeElementList) { for (Element element : thirdHashTreeElementList) {
String testName = testNameParam;
//更新请求类节点的部份属性
this.updateRequestElementInfo(element, testNameParam, requestElementNameArr, isFromScenario);
//检查有无jmeter不是别的自定义参数
this.checkPrivateFunctionNode(element);
//转换DubboDefaultConfigGui
List<Element> hashTreeConfigTestElementList = element.elements("ConfigTestElement");
for (Element configTestElement : hashTreeConfigTestElementList) {
this.updateDubboDefaultConfigGuiElement(configTestElement);
}
//HTTPSamplerProxy 进行附件转化 1.elementProp里去掉路径 2elementProp->filePath获取路径并读出来 //HTTPSamplerProxy 进行附件转化 1.elementProp里去掉路径 2elementProp->filePath获取路径并读出来
attachmentFilePathList.addAll(this.parseAttachmentFileInfo(element)); 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) { } catch (Exception e) {
LogUtil.error(e); LogUtil.error(e);
} }
if (!jmx.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
if (!jmxString.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) { jmx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + jmx;
jmxString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + jmxString;
} }
//处理附件 //处理附件
Map<String, String> attachmentFiles = new HashMap<>(); Map<String, String> 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); returnDTO.setFileMetadataList(fileMetadataList);
return returnDTO; return returnDTO;
} }
@ -633,83 +596,17 @@ public class APITestService {
return attachmentFilePathList; return attachmentFilePathList;
} }
private void updateDubboDefaultConfigGuiElement(Element configTestElement) { private String updateJmxMessage(String jmx) {
String dubboDefaultConfigGuiClassName = "io.github.ningyu.jmeter.plugin.dubbo.gui.DubboDefaultConfigGui"; if (StringUtils.isNotEmpty(jmx)) {
if (configTestElement == null) { jmx = StringUtils.replace(jmx, "<DubboSample", "<io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample");
return; jmx = StringUtils.replace(jmx, "</DubboSample>", "</io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample>");
} jmx = StringUtils.replace(jmx, " guiclass=\"DubboSampleGui\" ", " guiclass=\"io.github.ningyu.jmeter.plugin.dubbo.gui.DubboSampleGui\" ");
String guiClassValue = configTestElement.attributeValue("guiclass"); jmx = StringUtils.replace(jmx, " guiclass=\"DubboDefaultConfigGui\" ", " guiclass=\"io.github.ningyu.jmeter.plugin.dubbo.gui.DubboDefaultConfigGui\" ");
if (StringUtils.equals(guiClassValue, "DubboDefaultConfigGui")) { jmx = StringUtils.replace(jmx, " testclass=\"DubboSample\" ", " testclass=\"io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample\" ");
configTestElement.attribute("guiclass").setText(dubboDefaultConfigGuiClassName);
} }
return jmx;
} }
private void checkPrivateFunctionNode(Element element) {
List<Element> scriptHashTreeElementList = element.elements("hashTree");
for (Element scriptHashTreeElement : scriptHashTreeElementList) {
boolean isRemove = false;
List<Element> removeElement = new ArrayList<>();
List<Element> 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<Element> 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<Element> 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<MultipartFile> bodyFiles) { public JmxInfoDTO getJmxInfoDTO(RunDefinitionRequest runRequest, List<MultipartFile> bodyFiles) {
ParameterConfig config = new ParameterConfig(); ParameterConfig config = new ParameterConfig();
@ -725,11 +622,8 @@ public class APITestService {
} }
HashTree hashTree = runRequest.getTestElement().generateHashTree(config); HashTree hashTree = runRequest.getTestElement().generateHashTree(config);
String jmxString = runRequest.getTestElement().getJmx(hashTree); String jmxString = runRequest.getTestElement().getJmx(hashTree);
String testName = runRequest.getName();
//将jmx处理封装为通用方法 //将jmx处理封装为通用方法
JmxInfoDTO dto = updateJmxString(jmxString, testName, false); JmxInfoDTO dto = updateJmxString(jmxString);
dto.setName(runRequest.getName() + ".jmx"); dto.setName(runRequest.getName() + ".jmx");
return dto; return dto;
} }

View File

@ -1116,7 +1116,7 @@ public class ApiAutomationService {
} }
MsTestPlan testPlan = new MsTestPlan(); MsTestPlan testPlan = new MsTestPlan();
testPlan.setHashTree(new LinkedList<>()); 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"; String name = request.getName() + ".jmx";
dto.setName(name); dto.setName(name);
@ -1465,8 +1465,8 @@ public class ApiAutomationService {
if (StringUtils.isNotEmpty(item.getScenarioDefinition())) { if (StringUtils.isNotEmpty(item.getScenarioDefinition())) {
String jmx = generateJmx(item); String jmx = generateJmx(item);
if (StringUtils.isNotEmpty(jmx)) { 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());
JmxInfoDTO dto = apiTestService.updateJmxString(jmx, item.getName(), true); JmxInfoDTO dto = apiTestService.updateJmxString(jmx);
scenrioExportJmx.setId(item.getId()); scenrioExportJmx.setId(item.getId());
scenrioExportJmx.setVersion(item.getVersion()); scenrioExportJmx.setVersion(item.getVersion());
//扫描需要哪些文件 //扫描需要哪些文件
@ -1492,7 +1492,7 @@ public class ApiAutomationService {
if (StringUtils.isNotEmpty(item.getScenarioDefinition())) { if (StringUtils.isNotEmpty(item.getScenarioDefinition())) {
String jmx = generateJmx(item); String jmx = generateJmx(item);
if (StringUtils.isNotEmpty(jmx)) { 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 fileName = item.getName() + ".jmx";
String jmxStr = scenrioExportJmx.getJmx(); String jmxStr = scenrioExportJmx.getJmx();
files.put(fileName, jmxStr.getBytes(StandardCharsets.UTF_8)); files.put(fileName, jmxStr.getBytes(StandardCharsets.UTF_8));
@ -1882,7 +1882,7 @@ public class ApiAutomationService {
String testName = item.getName(); String testName = item.getName();
MsTestPlan testPlan = new MsTestPlan(); MsTestPlan testPlan = new MsTestPlan();
testPlan.setHashTree(new LinkedList<>()); testPlan.setHashTree(new LinkedList<>());
JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item), testName, true); JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item));
String name = item.getName() + ".jmx"; String name = item.getName() + ".jmx";
dto.setId(item.getId()); dto.setId(item.getId());
dto.setName(name); dto.setName(name);