You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by jb...@apache.org on 2014/11/29 20:34:16 UTC

svn commit: r1642467 - /tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/xml/ForEachTagTest.java

Author: jboynes
Date: Sat Nov 29 19:34:16 2014
New Revision: 1642467

URL: http://svn.apache.org/r1642467
Log:
Add benchmark to compare Xalan vs. JDK XPath speed. Xalan is still faster

Modified:
    tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/xml/ForEachTagTest.java

Modified: tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/xml/ForEachTagTest.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/xml/ForEachTagTest.java?rev=1642467&r1=1642466&r2=1642467&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/xml/ForEachTagTest.java (original)
+++ tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/xml/ForEachTagTest.java Sat Nov 29 19:34:16 2014
@@ -20,7 +20,12 @@ import java.io.InputStream;
 
 import javax.servlet.jsp.PageContext;
 import javax.servlet.jsp.tagext.IterationTag;
+import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathFactory;
+import javax.xml.xpath.XPathVariableResolver;
 
 import org.junit.Assert;
 import org.junit.Before;
@@ -28,6 +33,7 @@ import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import org.apache.xml.dtm.DTMIterator;
 import org.apache.xpath.XPath;
 import org.apache.xpath.XPathContext;
 import org.apache.xpath.objects.XObject;
@@ -35,9 +41,11 @@ import static org.easymock.EasyMock.crea
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /**
  */
@@ -74,13 +82,13 @@ public class ForEachTagTest {
         XPathContext context = tag.getContext();
         Assert.assertTrue(tag.hasNext());
         Node one = (Node) tag.next();
-        Assert.assertEquals("one", one.getTextContent());
+        assertEquals("one", one.getTextContent());
         Assert.assertTrue(tag.hasNext());
         Node two = (Node) tag.next();
-        Assert.assertEquals("two", two.getTextContent());
+        assertEquals("two", two.getTextContent());
         Assert.assertTrue(tag.hasNext());
         Node three = (Node) tag.next();
-        Assert.assertEquals("three", three.getTextContent());
+        assertEquals("three", three.getTextContent());
         Assert.assertFalse(tag.hasNext());
         tag.doFinally();
         Assert.assertTrue(context.getContextNodeListsStack().isEmpty());
@@ -99,17 +107,17 @@ public class ForEachTagTest {
         XPathContext context = tag.getContext();
         tag.hasNext();
         tag.next();
-        Assert.assertEquals("3", last.execute(context, context.getCurrentNode(), null).str());
-        Assert.assertEquals("one", dot.execute(context, context.getCurrentNode(), null).str());
-        Assert.assertEquals("1", position.execute(context, context.getCurrentNode(), null).str());
+        assertEquals("3", last.execute(context, context.getCurrentNode(), null).str());
+        assertEquals("one", dot.execute(context, context.getCurrentNode(), null).str());
+        assertEquals("1", position.execute(context, context.getCurrentNode(), null).str());
         tag.hasNext();
         tag.next();
-        Assert.assertEquals("two", dot.execute(context, context.getCurrentNode(), null).str());
-        Assert.assertEquals("2", position.execute(context, context.getCurrentNode(), null).str());
+        assertEquals("two", dot.execute(context, context.getCurrentNode(), null).str());
+        assertEquals("2", position.execute(context, context.getCurrentNode(), null).str());
         tag.hasNext();
         tag.next();
-        Assert.assertEquals("three", dot.execute(context, context.getCurrentNode(), null).str());
-        Assert.assertEquals("3", position.execute(context, context.getCurrentNode(), null).str());
+        assertEquals("three", dot.execute(context, context.getCurrentNode(), null).str());
+        assertEquals("3", position.execute(context, context.getCurrentNode(), null).str());
         verify(pageContext);
     }
 
@@ -117,15 +125,7 @@ public class ForEachTagTest {
     @Test
     public void testIterationPerformance() throws Exception {
         // create a large document
-        final int SIZE = 200000;
-        test = XmlUtil.newEmptyDocument();
-        Element root = test.createElement("root");
-        test.appendChild(root);
-        for (int i = 0; i < SIZE; i++) {
-            Element child = test.createElement("a");
-            child.setTextContent(Integer.toString(i));
-            root.appendChild(child);
-        }
+        test = newBenchmarkDocument(200000);
 
         XPath dot = new XPath(".", null, null, XPath.SELECT);
         expect(pageContext.findAttribute("doc")).andStubReturn(test);
@@ -135,13 +135,90 @@ public class ForEachTagTest {
         XObject result = null;
         if (tag.doStartTag() == IterationTag.EVAL_BODY_INCLUDE) {
             do {
-//                XPathContext context = tag.getContext();
-//                result = dot.execute(context, context.getCurrentNode(), null);
+                XPathContext context = tag.getContext();
+                result = dot.execute(context, context.getCurrentNode(), null);
             } while (tag.doAfterBody() == IterationTag.EVAL_BODY_AGAIN);
             tag.doFinally();
         }
         time += System.nanoTime();
-//        System.err.println("result = " + result.str());
         System.err.println("time = " + time/1000000 + "ms.");
+        assertEquals("199999", result.str());
+    }
+
+    @Ignore
+    @Test
+    public void xalanPerformance() throws Exception{
+        Document doc = newBenchmarkDocument(200000);
+        expect(pageContext.findAttribute("doc")).andStubReturn(doc);
+        replay(pageContext);
+
+        XPath select = new XPath("$doc/root/a", null, null, XPath.SELECT);
+        XPath dot = new XPath(".", null, null, XPath.SELECT);
+        XObject result = null;
+
+        long time = -System.nanoTime();
+        XPathContext context = new XPathContext(false);
+        context.setVarStack(new JSTLVariableStack(pageContext));
+        int dtm = context.getDTMHandleFromNode(XmlUtil.newEmptyDocument());
+        context.pushCurrentNodeAndExpression(dtm, dtm);
+
+        // create an iterator over the returned nodes and push into the context
+        XObject nodes = select.execute(context, context.getCurrentNode(), null);
+        DTMIterator iterator = nodes.iter();
+        context.pushContextNodeList(iterator);
+        while (iterator.getCurrentPos() < iterator.getLength()) {
+            int next = iterator.nextNode();
+            context.pushCurrentNode(next);
+            iterator.getDTM(next).getNode(next);
+
+            result = dot.execute(context, context.getCurrentNode(), null);
+
+            context.popCurrentNode();
+        }
+        time += System.nanoTime();
+        System.err.println("time = " + time/1000000 + "ms.");
+        assertEquals("199999", result.str());
+    }
+
+    @Ignore
+    @Test
+    public void xpathPerformance() throws Exception {
+        final Document doc = newBenchmarkDocument(200000);
+        expect(pageContext.findAttribute("doc")).andStubReturn(doc);
+        replay(pageContext);
+
+        XPathVariableResolver resolver = new XPathVariableResolver() {
+            public Object resolveVariable(QName variableName) {
+                return doc;
+            }
+        };
+        XPathFactory factory = XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI, "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", null);
+        System.out.println("factory.getClass() = " + factory.getClass());
+        factory.setXPathVariableResolver(resolver);
+        XPathExpression select = factory.newXPath().compile("$doc/root/a");
+        XPathExpression dot = factory.newXPath().compile(".");
+        Node result = null;
+
+        long time = -System.nanoTime();
+        NodeList nodes = (NodeList) select.evaluate(XmlUtil.newEmptyDocument(), XPathConstants.NODESET);
+        for (int i = 0; i < nodes.getLength(); i++) {
+            Node context = nodes.item(i);
+            result = (Node) dot.evaluate(context, XPathConstants.NODE);
+        }
+        time += System.nanoTime();
+        System.err.println("time = " + time/1000000 + "ms.");
+        assertEquals("199999", result.getTextContent());
+    }
+
+    private static Document newBenchmarkDocument(int size) {
+        Document doc = XmlUtil.newEmptyDocument();
+        Element root = doc.createElement("root");
+        doc.appendChild(root);
+        for (int i = 0; i < size; i++) {
+            Element child = doc.createElement("a");
+            child.setTextContent(Integer.toString(i));
+            root.appendChild(child);
+        }
+        return doc;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org