refactor(性能测试): 性能测试转出jmx格式化修改

This commit is contained in:
CaptainB 2022-02-08 13:50:17 +08:00 committed by CountryBuilder
parent 1f0161b190
commit e3d3b34f8f
2 changed files with 31 additions and 8 deletions

View File

@ -1,19 +1,25 @@
package io.metersphere.performance.parse; package io.metersphere.performance.parse;
import io.metersphere.commons.constants.FileType; import io.metersphere.commons.constants.FileType;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.performance.parse.xml.reader.JmeterDocumentParser; import io.metersphere.performance.parse.xml.reader.JmeterDocumentParser;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLFilterImpl; import org.xml.sax.helpers.XMLFilterImpl;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.util.List;
public class EngineSourceParserFactory { public class EngineSourceParserFactory {
public static final boolean IS_TRANS = true; public static final boolean IS_TRANS = false;
public static EngineSourceParser createEngineSourceParser(String type) { public static EngineSourceParser createEngineSourceParser(String type) {
@ -35,8 +41,28 @@ public class EngineSourceParserFactory {
} }
public static byte[] getBytes(Document document) throws Exception { public static byte[] getBytes(Document document) throws Exception {
// todo 格式化代码会导致前后置脚本缩进有问题先使用基本方式 OutputFormat format = new OutputFormat();
return document.asXML().getBytes(StandardCharsets.UTF_8); format.setIndentSize(2);
format.setNewlines(true);
format.setPadText(true);
format.setTrimText(false);
try (
ByteArrayOutputStream out = new ByteArrayOutputStream();
) {
// 删除空白的行
List<Node> nodes = document.selectNodes("//text()[normalize-space(.)='']");
nodes.forEach(node -> node.setText(""));
XMLWriter xw = new XMLWriter(out, format);
xw.setEscapeText(IS_TRANS);
xw.write(document);
xw.flush();
xw.close();
return out.toByteArray();
} catch (IOException e) {
LogUtil.error(e);
}
return new byte[0];
} }
public static XMLFilterImpl getFilter() { public static XMLFilterImpl getFilter() {

View File

@ -1127,9 +1127,6 @@ public class JmeterDocumentParser implements EngineSourceParser {
} }
private void removeChildren(Element node) { private void removeChildren(Element node) {
List<Element> elements = node.elements(); node.setText("");
for (Element ele : elements) {
node.remove(ele);
}
} }
} }