Mercurial > stress-tester
changeset 510:8d813f86d0c1
XmlValueExtractor.returnNullIfPathNotFound
author | Devel 1 |
---|---|
date | Thu, 17 Aug 2017 15:37:36 +0200 |
parents | 5d20b4bc944d |
children | 9157b42b96a3 |
files | stress-tester-benchmark/src/main/java/com/passus/st/client/http/filter/JsonValueExtractorBenchmark.java stress-tester-benchmark/src/main/java/com/passus/st/client/http/filter/XmlValueExtractorBenchmark.java stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java stress-tester/src/test/java/com/passus/st/client/http/extractor/XmlValueExtractorTest.java |
diffstat | 4 files changed, 128 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester-benchmark/src/main/java/com/passus/st/client/http/filter/JsonValueExtractorBenchmark.java Thu Aug 17 15:36:59 2017 +0200 +++ b/stress-tester-benchmark/src/main/java/com/passus/st/client/http/filter/JsonValueExtractorBenchmark.java Thu Aug 17 15:37:36 2017 +0200 @@ -41,7 +41,7 @@ } public static void main(String[] args) throws Throwable { - JsonValueExtractorBenchmark bench = new JsonValueExtractorBenchmark(); //.extractLongGsonEx(); + JsonValueExtractorBenchmark bench = new JsonValueExtractorBenchmark(); Options opt = new OptionsBuilder().include(JsonValueExtractorBenchmark.class.getSimpleName() + ".*").build(); new Runner(opt).run(); AllocationUtils au = new AllocationUtils();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester-benchmark/src/main/java/com/passus/st/client/http/filter/XmlValueExtractorBenchmark.java Thu Aug 17 15:37:36 2017 +0200 @@ -0,0 +1,86 @@ +package com.passus.st.client.http.filter; + +import java.io.StringReader; +import java.util.concurrent.TimeUnit; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathFactory; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; + +/** + * + * @author mikolaj.podbielski + */ +@State(Scope.Thread) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Fork(value = 1) +@Measurement(iterations = 6) +@Warmup(iterations = 6) +public class XmlValueExtractorBenchmark { + + public static void main(String[] args) throws Throwable { + XmlValueExtractorBenchmark bench = new XmlValueExtractorBenchmark(); + Options opt = new OptionsBuilder().include(XmlValueExtractorBenchmark.class.getSimpleName() + ".*").build(); + new Runner(opt).run(); + } + + private final XPathExpression xPathExpr; + private final DocumentBuilder builder; + + private final String xpath = "/root/node3/node4"; + private final String xml = "<?xml version=\"1.0\"?>" + + "<root>" + + " <node1>value1</node1>" + + " <node2>value2</node2>" + + " <node2>value3</node2>" + + " <node3><node4>value4</node4></node3>" + + " <emptyNode></emptyNode>" + + "</root>"; + + public XmlValueExtractorBenchmark() { + try { + XPath xPath = XPathFactory.newInstance().newXPath(); + xPathExpr = xPath.compile(xpath); + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + builder = builderFactory.newDocumentBuilder(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + @Benchmark + public Object extractSimple() throws Exception { + try (StringReader sr = new StringReader(xml)) { + Document doc = builder.parse(new InputSource(sr)); + return xPathExpr.evaluate(doc); + } + } + + @Benchmark + public Object extractWithNull() throws Exception { + try (StringReader sr = new StringReader(xml)) { + Document doc = builder.parse(new InputSource(sr)); + Node node = (Node) xPathExpr.evaluate(doc, XPathConstants.NODE); + node.getTextContent(); + return xPathExpr.evaluate(doc); + } + } +}
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java Thu Aug 17 15:36:59 2017 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java Thu Aug 17 15:37:36 2017 +0200 @@ -5,12 +5,14 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.w3c.dom.Document; +import org.w3c.dom.Node; import org.xml.sax.InputSource; /** @@ -25,6 +27,8 @@ private DocumentBuilder builder; + private boolean returnNullIfPathNotFound; + public XmlValueExtractor(String expression) throws XPathExpressionException { Assert.notNull(expression, "expression"); XPath xPath = XPathFactory.newInstance().newXPath(); @@ -36,6 +40,10 @@ this.xPathExpr = xPathExpr; } + public void setReturnNullIfPathNotFound(boolean returnNullIfPathNotFound) { + this.returnNullIfPathNotFound = returnNullIfPathNotFound; + } + @Override public String extract(String content) { try { @@ -53,6 +61,10 @@ try (StringReader sr = new StringReader(content)) { Document doc = builder.parse(new InputSource(sr)); + if (returnNullIfPathNotFound) { + Node node = (Node) xPathExpr.evaluate(doc, XPathConstants.NODE); + node.getTextContent(); + } return xPathExpr.evaluate(doc); } catch (Exception e) { if (LOGGER.isDebugEnabled()) {
--- a/stress-tester/src/test/java/com/passus/st/client/http/extractor/XmlValueExtractorTest.java Thu Aug 17 15:36:59 2017 +0200 +++ b/stress-tester/src/test/java/com/passus/st/client/http/extractor/XmlValueExtractorTest.java Thu Aug 17 15:37:36 2017 +0200 @@ -1,6 +1,5 @@ package com.passus.st.client.http.extractor; -import com.passus.st.Log4jConfigurationFactory; import static org.testng.AssertJUnit.*; import org.testng.annotations.Test; @@ -10,21 +9,21 @@ */ public class XmlValueExtractorTest { + final String xml = "<?xml version=\"1.0\"?>" + + "<root>" + + " <node1>value1</node1>" + + " <node2>value2</node2>" + + " <node2>value3</node2>" + + " <node3><node4>value4</node4></node3>" + + " <emptyNode></emptyNode>" + + "</root>"; + @Test public void testExtract() throws Exception { - Log4jConfigurationFactory.enableFactory("debug"); - String xml = "<?xml version=\"1.0\"?>" - + "<root>" - + " <node1>value1</node1>" - + " <node2>value2</node2>" - + " <node2>value3</node2>" - + " <node3><node4>value4</node4></node3>" - + " <emptyNode></emptyNode>" - + "</root>"; - assertEquals("value1", extract("/root/node1", xml)); assertEquals("value2", extract("/root/node2", xml)); assertEquals("value3", extract("/root/node2[2]", xml)); + assertEquals("value4", extract("/root/node3/node4", xml)); assertEquals("", extract("/root/emptyNode", xml)); assertEquals("", extract("/root/noSuchNode", xml)); @@ -35,6 +34,19 @@ } @Test + public void testExtractWithNull() throws Exception { + assertEquals("value1", extractWithNull("/root/node1", xml)); + assertEquals("value2", extractWithNull("/root/node2", xml)); + assertEquals("value3", extractWithNull("/root/node2[2]", xml)); + assertEquals("value4", extractWithNull("/root/node3/node4", xml)); + assertEquals("", extractWithNull("/root/emptyNode", xml)); + + assertEquals(null, extractWithNull("/root/noSuchNode", xml)); + assertEquals(null, extractWithNull("/root/node1/@noSuchAttribute", xml)); + assertEquals(null, extractWithNull("/root/node1", "NOT AN XML")); + } + + @Test public void testInvalidXPath() { try { XmlValueExtractor e = new XmlValueExtractor("not a path"); @@ -46,4 +58,10 @@ static String extract(String path, String content) throws Exception { return new XmlValueExtractor(path).extract(content); } + + static String extractWithNull(String path, String content) throws Exception { + XmlValueExtractor e = new XmlValueExtractor(path); + e.setReturnNullIfPathNotFound(true); + return e.extract(content); + } }