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

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

View File

@ -95,11 +95,13 @@ public class MsThreadGroup extends MsTestElement {
postProcessor.setClazzName("io.metersphere.api.dto.definition.request.processors.MsJSR223Processor");
postProcessor.toHashTree(groupTree, postProcessor.getHashTree(), config);
}
if(!config.isOperating()){
MsDebugSampler el = new MsDebugSampler();
el.setName(RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME);
el.toHashTree(groupTree, el.getHashTree(), config);
}
}
}
private String checkProjectId(List<MsTestElement> hashTree) {
String projectId = this.getProjectId();

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() + ".\"");
if(config.isOperating()){
if (script.startsWith("io.metersphere.utils.JMeterVars.addVars")) {
return;
}
}
// 非导出操作且不是启用状态则跳过执行
if (!config.isOperating() && !this.isEnable()) {
return;

View File

@ -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;

View File

@ -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;

View File

@ -156,15 +156,22 @@ public class MsDubboSampler extends MsTestElement {
DubboSample sampler = new DubboSample();
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);
}
}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<String> 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()));

View File

@ -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());

View File

@ -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());

View File

@ -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));

View File

@ -455,72 +455,35 @@ public class APITestService {
* 更新jmx数据处理jmx里的各种参数
* <p>
*
* @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<String> 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<Element> innerHashTreeElementList = rootHashTreeElement.elements("hashTree");
for (Element innerHashTreeElement : innerHashTreeElementList) {
//转换DubboDefaultConfigGui
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");
List<Element> thirdHashTreeElementList = innerHashTreeElement.elements();
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获取路径并读出来
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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
jmxString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + jmxString;
if (!jmx.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
jmx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + jmx;
}
//处理附件
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);
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, "<DubboSample", "<io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample");
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\" ");
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<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) {
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;
}

View File

@ -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);