advanced config

This commit is contained in:
Captain.B 2020-03-30 17:31:39 +08:00
parent 8b1e4f2a79
commit caf9444db4
4 changed files with 370 additions and 4 deletions

View File

@ -42,6 +42,10 @@ public class EngineContext {
this.properties.put(key, value);
}
public void addProperties(Map<String, Object> props) {
this.properties.putAll(props);
}
public Object getProperty(String key) {
return this.properties.get(key);
}

View File

@ -77,7 +77,7 @@ public class EngineFactory {
engineContext.setThreadNum(threadNum);
engineContext.setResourcePoolId(loadTest.getTestResourcePoolId());
if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) {
if (StringUtils.isNotEmpty(loadTest.getLoadConfiguration())) {
final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration());
for (int i = 0; i < jsonArray.size(); i++) {
@ -85,6 +85,13 @@ public class EngineFactory {
engineContext.addProperty(jsonObject.getString("key"), jsonObject.get("value"));
}
}
/*
{"timeout":10,"statusCode":["302","301"],"params":[{"name":"param1","enable":true,"value":"0","edit":false}],"domains":[{"domain":"baidu.com","enable":true,"ip":"127.0.0.1","edit":false}]}
*/
if (StringUtils.isNotEmpty(loadTest.getAdvancedConfiguration())) {
JSONObject advancedConfiguration = JSONObject.parseObject(loadTest.getAdvancedConfiguration());
engineContext.addProperties(advancedConfiguration);
}
final EngineSourceParser engineSourceParser = EngineSourceParserFactory.createEngineSourceParser(engineContext.getFileType());
@ -95,6 +102,8 @@ public class EngineFactory {
try (ByteArrayInputStream source = new ByteArrayInputStream(fileContent.getFile())) {
String content = engineSourceParser.parse(engineContext, source);
engineContext.setContent(content);
} catch (Exception e) {
MSException.throwException(e);
}
if (CollectionUtils.isNotEmpty(csvFiles)) {

View File

@ -1,10 +1,12 @@
package io.metersphere.parse.xml.reader.jmx;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.config.KafkaProperties;
import io.metersphere.engine.EngineContext;
import io.metersphere.parse.xml.reader.DocumentParser;
import org.junit.platform.commons.util.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -16,6 +18,7 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.List;
public class JmeterDocumentParser implements DocumentParser {
private final static String HASH_TREE_ELEMENT = "hashTree";
@ -26,7 +29,9 @@ public class JmeterDocumentParser implements DocumentParser {
private final static String VARIABLE_THROUGHPUT_TIMER = "kg.apc.jmeter.timers.VariableThroughputTimer";
private final static String THREAD_GROUP = "ThreadGroup";
private final static String BACKEND_LISTENER = "BackendListener";
private final static String CONFIG_TEST_ELEMENT = "ConfigTestElement";
private final static String DNS_CACHE_MANAGER = "DNSCacheManager";
private final static String ARGUMENTS = "Arguments";
private EngineContext context;
@Override
@ -79,6 +84,9 @@ public class JmeterDocumentParser implements DocumentParser {
parseHashTree(ele);
} else if (nodeNameEquals(ele, TEST_PLAN)) {
processTearDownTestPlan(ele);
processCheckoutConfigTestElement(ele);
processCheckoutDnsCacheManager(ele);
processCheckoutArguments(ele);
} else if (nodeNameEquals(ele, CONCURRENCY_THREAD_GROUP)) {
processConcurrencyThreadGroup(ele);
processCheckoutTimer(ele);
@ -93,12 +101,249 @@ public class JmeterDocumentParser implements DocumentParser {
processCheckoutBackendListener(ele);
} else if (nodeNameEquals(ele, BACKEND_LISTENER)) {
processBackendListener(ele);
} else if (nodeNameEquals(ele, CONFIG_TEST_ELEMENT)) {
processConfigTestElement(ele);
} else if (nodeNameEquals(ele, DNS_CACHE_MANAGER)) {
processDnsCacheManager(ele);
} else if (nodeNameEquals(ele, ARGUMENTS)) {
processArguments(ele);
}
}
}
}
}
private void processCheckoutArguments(Element ele) {
Node hashTree = ele.getNextSibling();
while (!(hashTree instanceof Element)) {
hashTree = hashTree.getNextSibling();
}
NodeList childNodes = hashTree.getChildNodes();
for (int i = 0, size = childNodes.getLength(); i < size; i++) {
Node item = childNodes.item(i);
if (nodeNameEquals(item, ARGUMENTS)) {
// 已经存在不再添加
return;
}
}
/*
<Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="BASE_URL_1" elementType="Argument">
<stringProp name="Argument.name">BASE_URL_1</stringProp>
<stringProp name="Argument.value">rddev2.fit2cloud.com</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</Arguments>
*/
Document document = ele.getOwnerDocument();
Element element = document.createElement(ARGUMENTS);
element.setAttribute("guiclass", "ArgumentsPanel");
element.setAttribute("testclass", "Arguments");
element.setAttribute("testname", "User Defined Variables");
element.setAttribute("enabled", "true");
Element collectionProp = document.createElement(COLLECTION_PROP);
collectionProp.setAttribute("name", "Arguments.arguments");
element.appendChild(collectionProp);
hashTree.appendChild(element);
// 空的 hashTree
hashTree.appendChild(document.createElement(HASH_TREE_ELEMENT));
}
private void processCheckoutDnsCacheManager(Element ele) {
Node hashTree = ele.getNextSibling();
while (!(hashTree instanceof Element)) {
hashTree = hashTree.getNextSibling();
}
NodeList childNodes = hashTree.getChildNodes();
for (int i = 0, size = childNodes.getLength(); i < size; i++) {
Node item = childNodes.item(i);
if (nodeNameEquals(item, DNS_CACHE_MANAGER)) {
// 已经存在不再添加
return;
}
}
/*
<DNSCacheManager guiclass="DNSCachePanel" testclass="DNSCacheManager" testname="DNS Cache Manager" enabled="true">
<collectionProp name="DNSCacheManager.servers"/>
<collectionProp name="DNSCacheManager.hosts">
<elementProp name="baiud.com" elementType="StaticHost">
<stringProp name="StaticHost.Name">baiud.com</stringProp>
<stringProp name="StaticHost.Address">172.16.10.187</stringProp>
</elementProp>
</collectionProp>
<boolProp name="DNSCacheManager.clearEachIteration">true</boolProp>
<boolProp name="DNSCacheManager.isCustomResolver">true</boolProp>
</DNSCacheManager>
*/
Document document = ele.getOwnerDocument();
Element element = document.createElement(DNS_CACHE_MANAGER);
element.setAttribute("guiclass", "DNSCachePanel");
element.setAttribute("testclass", "DNSCacheManager");
element.setAttribute("testname", "DNS Cache Manager");
element.setAttribute("enabled", "true");
Element collectionProp = document.createElement(COLLECTION_PROP);
collectionProp.setAttribute("name", "DNSCacheManager.servers");
element.appendChild(collectionProp);
Element collectionProp2 = document.createElement(COLLECTION_PROP);
collectionProp2.setAttribute("name", "DNSCacheManager.hosts");
element.appendChild(collectionProp2);
element.appendChild(createBoolProp(document, "DNSCacheManager.clearEachIteration", true));
element.appendChild(createBoolProp(document, "DNSCacheManager.isCustomResolver", true));
hashTree.appendChild(element);
// 空的 hashTree
hashTree.appendChild(document.createElement(HASH_TREE_ELEMENT));
}
private void processCheckoutConfigTestElement(Element ele) {
Node hashTree = ele.getNextSibling();
while (!(hashTree instanceof Element)) {
hashTree = hashTree.getNextSibling();
}
NodeList childNodes = hashTree.getChildNodes();
for (int i = 0, size = childNodes.getLength(); i < size; i++) {
Node item = childNodes.item(i);
if (nodeNameEquals(item, CONFIG_TEST_ELEMENT)) {
// 已经存在不再添加
return;
}
}
/*
<ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP Request Defaults" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain"></stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.protocol"></stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path"></stringProp>
<boolProp name="HTTPSampler.image_parser">true</boolProp>
<boolProp name="HTTPSampler.concurrentDwn">true</boolProp>
<stringProp name="HTTPSampler.concurrentPool">6</stringProp>
<stringProp name="HTTPSampler.connect_timeout">30000</stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
</ConfigTestElement>
*/
Document document = ele.getOwnerDocument();
Element element = document.createElement(CONFIG_TEST_ELEMENT);
element.setAttribute("guiclass", "HttpDefaultsGui");
element.setAttribute("testclass", "ConfigTestElement");
element.setAttribute("testname", "HTTP Request Defaults");
element.setAttribute("enabled", "true");
Element elementProp = document.createElement("elementProp");
elementProp.setAttribute("name", "HTTPsampler.Arguments");
elementProp.setAttribute("elementType", "Arguments");
elementProp.setAttribute("guiclass", "HTTPArgumentsPanel");
elementProp.setAttribute("testclass", "Arguments");
elementProp.setAttribute("enabled", "true");
Element collectionProp = document.createElement(COLLECTION_PROP);
collectionProp.setAttribute("name", "Arguments.arguments");
elementProp.appendChild(collectionProp);
element.appendChild(elementProp);
element.appendChild(createStringProp(document, "HTTPSampler.domain", ""));
element.appendChild(createStringProp(document, "HTTPSampler.port", ""));
element.appendChild(createStringProp(document, "HTTPSampler.protocol", ""));
element.appendChild(createStringProp(document, "HTTPSampler.contentEncoding", ""));
element.appendChild(createStringProp(document, "HTTPSampler.path", ""));
element.appendChild(createStringProp(document, "HTTPSampler.concurrentPool", "6"));
element.appendChild(createStringProp(document, "HTTPSampler.connect_timeout", ""));
element.appendChild(createStringProp(document, "HTTPSampler.response_timeout", ""));
element.appendChild(createBoolProp(document, "HTTPSampler.image_parser", true));
element.appendChild(createBoolProp(document, "HTTPSampler.concurrentDwn", true));
hashTree.appendChild(element);
// 空的 hashTree
hashTree.appendChild(document.createElement(HASH_TREE_ELEMENT));
}
private void processArguments(Element ele) {
NodeList childNodes = ele.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (item instanceof Element && nodeNameEquals(item, "collectionProp")) {
removeChildren(item);
Document document = item.getOwnerDocument();
Object params = context.getProperty("params");
if (params instanceof List) {
for (Object p : (List) params) {
JSONObject jsonObject = JSON.parseObject(p.toString());
if (!jsonObject.getBooleanValue("enable")) {
continue;
}
Element elementProp = document.createElement("elementProp");
elementProp.setAttribute("name", jsonObject.getString("name"));
elementProp.setAttribute("elementType", "Argument");
elementProp.appendChild(createStringProp(document, "Argument.name", jsonObject.getString("name")));
elementProp.appendChild(createStringProp(document, "Argument.value", jsonObject.getString("value")));
elementProp.appendChild(createStringProp(document, "Argument.metadata", "="));
item.appendChild(elementProp);
}
}
}
}
}
private void processDnsCacheManager(Element ele) {
NodeList childNodes = ele.getChildNodes();
for (int i = 0, size = childNodes.getLength(); i < size; i++) {
Node item = childNodes.item(i);
if (item instanceof Element && nodeNameEquals(item, "collectionProp")
&& org.apache.commons.lang3.StringUtils.equals(((Element) item).getAttribute("name"), "DNSCacheManager.hosts")) {
removeChildren(item);
Document document = item.getOwnerDocument();
Object domains = context.getProperty("domains");
if (domains instanceof List) {
for (Object d : (List) domains) {
JSONObject jsonObject = JSON.parseObject(d.toString());
if (!jsonObject.getBooleanValue("enable")) {
continue;
}
Element elementProp = document.createElement("elementProp");
elementProp.setAttribute("name", jsonObject.getString("domain"));
elementProp.setAttribute("elementType", "StaticHost");
elementProp.appendChild(createStringProp(document, "StaticHost.Name", jsonObject.getString("domain")));
elementProp.appendChild(createStringProp(document, "StaticHost.Address", jsonObject.getString("ip")));
item.appendChild(elementProp);
}
}
}
if (item instanceof Element && nodeNameEquals(item, "boolProp")
&& org.apache.commons.lang3.StringUtils.equals(((Element) item).getAttribute("name"), "DNSCacheManager.isCustomResolver")) {
item.getFirstChild().setNodeValue("true");
}
}
}
private void processConfigTestElement(Element ele) {
NodeList childNodes = ele.getChildNodes();
for (int i = 0, size = childNodes.getLength(); i < size; i++) {
Node item = childNodes.item(i);
if (item instanceof Element && nodeNameEquals(item, STRING_PROP)
&& StringUtils.equals(((Element) item).getAttribute("name"), "HTTPSampler.connect_timeout")) {
if (context.getProperty("timeout") != null) {
removeChildren(item);
item.appendChild(ele.getOwnerDocument().createTextNode(context.getProperty("timeout").toString()));
}
}
}
}
private void processTearDownTestPlan(Element ele) {
/*<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>*/
Document document = ele.getOwnerDocument();

View File

@ -1,8 +1,8 @@
package io.metersphere;
import io.metersphere.config.KafkaProperties;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@ -33,6 +33,9 @@ public class JmxFileParseTest {
private final static String VARIABLE_THROUGHPUT_TIMER = "kg.apc.jmeter.timers.VariableThroughputTimer";
private final static String BACKEND_LISTENER = "BackendListener";
private final static String THREAD_GROUP = "ThreadGroup";
private final static String CONFIG_TEST_ELEMENT = "ConfigTestElement";
private final static String DNS_CACHE_MANAGER = "DNSCacheManager";
private final static String ARGUMENTS = "Arguments";
@Resource
private KafkaProperties kafkaProperties;
@ -110,12 +113,117 @@ public class JmxFileParseTest {
} else if (nodeNameEquals(ele, BACKEND_LISTENER)) {
processBackendListener(ele);
} else if (nodeNameEquals(ele, CONFIG_TEST_ELEMENT)) {
processConfigTestElement(ele);
} else if (nodeNameEquals(ele, DNS_CACHE_MANAGER)) {
processDnsCacheManager(ele);
} else if (nodeNameEquals(ele, ARGUMENTS)) {
processArguments(ele);
}
}
}
}
}
private void processArguments(Element ele) {
/*
<Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="BASE_URL_1" elementType="Argument">
<stringProp name="Argument.name">BASE_URL_1</stringProp>
<stringProp name="Argument.value">rddev2.fit2cloud.com</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</Arguments>
*/
NodeList childNodes = ele.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (item instanceof Element && nodeNameEquals(item, "collectionProp")) {
removeChildren(item);
Document document = item.getOwnerDocument();
Element elementProp = document.createElement("elementProp");
elementProp.setAttribute("name", "");
elementProp.setAttribute("elementType", "");
elementProp.appendChild(createStringProp(document, "Argument.name", ""));
elementProp.appendChild(createStringProp(document, "Argument.value", ""));
elementProp.appendChild(createStringProp(document, "Argument.metadata", "="));
item.appendChild(elementProp);
}
}
}
private void processDnsCacheManager(Element ele) {
/*
<DNSCacheManager guiclass="DNSCachePanel" testclass="DNSCacheManager" testname="DNS Cache Manager" enabled="true">
<collectionProp name="DNSCacheManager.servers"/>
<collectionProp name="DNSCacheManager.hosts">
<elementProp name="baiud.com" elementType="StaticHost">
<stringProp name="StaticHost.Name">baiud.com</stringProp>
<stringProp name="StaticHost.Address">172.16.10.187</stringProp>
</elementProp>
</collectionProp>
<boolProp name="DNSCacheManager.clearEachIteration">true</boolProp>
<boolProp name="DNSCacheManager.isCustomResolver">true</boolProp>
</DNSCacheManager>
*/
NodeList childNodes = ele.getChildNodes();
for (int i = 0, size = childNodes.getLength(); i < size; i++) {
Node item = childNodes.item(i);
if (item instanceof Element && nodeNameEquals(item, "collectionProp")
&& StringUtils.equals(((Element) item).getAttribute("name"), "DNSCacheManager.hosts")) {
Node childNode = item.getFirstChild();
// todo 绑定域名 多个
while (!(childNode instanceof Element)) {
childNode = childNode.getNextSibling();
}
Element elementProp = ((Element) childNode);
elementProp.setAttribute("name", "baidu.com");
elementProp.setAttribute("elementType", "StaticHost");
removeChildren(elementProp);
elementProp.appendChild(createStringProp(ele.getOwnerDocument(), "StaticHost.Name", ""));
elementProp.appendChild(createStringProp(ele.getOwnerDocument(), "StaticHost.Address", ""));
}
if (item instanceof Element && nodeNameEquals(item, "boolProp")
&& StringUtils.equals(((Element) item).getAttribute("name"), "DNSCacheManager.isCustomResolver")) {
item.getFirstChild().setNodeValue("true");
}
}
}
private void processConfigTestElement(Element ele) {
/*
<ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP Request Defaults" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain"></stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.protocol"></stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path"></stringProp>
<boolProp name="HTTPSampler.image_parser">true</boolProp>
<boolProp name="HTTPSampler.concurrentDwn">true</boolProp>
<stringProp name="HTTPSampler.concurrentPool">6</stringProp>
<stringProp name="HTTPSampler.connect_timeout">30000</stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
</ConfigTestElement>
*/
NodeList childNodes = ele.getChildNodes();
for (int i = 0, size = childNodes.getLength(); i < size; i++) {
Node item = childNodes.item(i);
if (item instanceof Element && nodeNameEquals(item, STRING_PROP)
&& StringUtils.equals(((Element) item).getAttribute("name"), "HTTPSampler.connect_timeout")) {
item.getFirstChild().setNodeValue("30000");
}
}
}
private void processTearDownTestPlan(Element ele) {
/*<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>*/
Document document = ele.getOwnerDocument();