Repository: brooklyn-server Updated Branches: refs/heads/master 4b11f509d -> 746a81906
Improve/test performance of XmlUtil.xpath Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/e15697f4 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/e15697f4 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/e15697f4 Branch: refs/heads/master Commit: e15697f42eb103fb393ab391f53bcd1d7ebd4d38 Parents: 99d073f Author: Aled Sage <[email protected]> Authored: Wed Jun 22 13:24:01 2016 +0100 Committer: Aled Sage <[email protected]> Committed: Wed Jun 22 13:24:01 2016 +0100 ---------------------------------------------------------------------- .../brooklyn/util/core/xstream/XmlUtil.java | 24 +++++++-- .../test/qa/performance/XmlPerformanceTest.java | 53 ++++++++++++++++++++ .../brooklyn/util/core/xstream/XmlUtilTest.java | 5 +- 3 files changed, 77 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/e15697f4/core/src/main/java/org/apache/brooklyn/util/core/xstream/XmlUtil.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/util/core/xstream/XmlUtil.java b/core/src/main/java/org/apache/brooklyn/util/core/xstream/XmlUtil.java index 0619811..a969dea 100644 --- a/core/src/main/java/org/apache/brooklyn/util/core/xstream/XmlUtil.java +++ b/core/src/main/java/org/apache/brooklyn/util/core/xstream/XmlUtil.java @@ -34,11 +34,29 @@ import org.xml.sax.SAXException; public class XmlUtil { + /** + * Thread-local storage for sharing the {@link DocumentBuilder}, to avoid repeated construction. + * See {@linkplain http://stackoverflow.com/questions/9828254/is-documentbuilderfactory-thread-safe-in-java-5}. + */ + private static class SharedDocumentBuilder { + private static ThreadLocal<DocumentBuilder> instance = new ThreadLocal<DocumentBuilder>(); + + public static DocumentBuilder get() throws ParserConfigurationException { + DocumentBuilder result = instance.get(); + if (result == null) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + result = factory.newDocumentBuilder(); + instance.set(result); + } else { + result.reset(); + } + return result; + } + } + public static Object xpath(String xml, String xpath) { - // TODO Could share factory/doc in thread-local storage; see http://stackoverflow.com/questions/9828254/is-documentbuilderfactory-thread-safe-in-java-5 try { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); + DocumentBuilder builder = SharedDocumentBuilder.get(); Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes())); XPathFactory xPathfactory = XPathFactory.newInstance(); XPathExpression expr = xPathfactory.newXPath().compile(xpath); http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/e15697f4/core/src/test/java/org/apache/brooklyn/core/test/qa/performance/XmlPerformanceTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/test/qa/performance/XmlPerformanceTest.java b/core/src/test/java/org/apache/brooklyn/core/test/qa/performance/XmlPerformanceTest.java new file mode 100644 index 0000000..8658a7c --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/test/qa/performance/XmlPerformanceTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.core.test.qa.performance; + +import org.apache.brooklyn.test.performance.PerformanceTestDescriptor; +import org.apache.brooklyn.util.core.xstream.XmlUtil; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class XmlPerformanceTest extends AbstractPerformanceTest { + + @BeforeMethod(alwaysRun=true) + @Override + public void setUp() throws Exception { + super.setUp(); + } + + protected int numIterations() { + return 100; + } + + @Test(groups = { "Integration", "Acceptance" }) + public void testXpath() throws Exception { + int numIterations = numIterations(); + double minRatePerSec = 100 * PERFORMANCE_EXPECTATION; + + measure(PerformanceTestDescriptor.create() + .summary("XmlPerformanceTest.testXpath") + .iterations(numIterations) + .minAcceptablePerSecond(minRatePerSec) + .job(new Runnable() { + public void run() { + String xml = "<a><b>myb</b></a>"; + XmlUtil.xpath(xml, "/a/b[text()]"); + }})); + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/e15697f4/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlUtilTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlUtilTest.java b/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlUtilTest.java index 3391862..65983e3 100644 --- a/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlUtilTest.java +++ b/core/src/test/java/org/apache/brooklyn/util/core/xstream/XmlUtilTest.java @@ -20,7 +20,6 @@ package org.apache.brooklyn.util.core.xstream; import static org.testng.Assert.assertEquals; -import org.apache.brooklyn.util.core.xstream.XmlUtil; import org.testng.annotations.Test; @@ -29,6 +28,8 @@ public class XmlUtilTest { @Test public void testXpath() throws Exception { String xml = "<a><b>myb</b></a>"; - assertEquals(XmlUtil.xpath(xml, "/a/b[text()]"), "myb"); + for (int i = 0; i < 2; i++) { + assertEquals(XmlUtil.xpath(xml, "/a/b[text()]"), "myb"); + } } }
