1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 /*
19 * $Id$
20 */
21 package org.apache.qetest.dtm;
22
23 import org.apache.xml.dtm.Axis;
24 import org.apache.xml.dtm.DTM;
25
26
27 /**
28 * Unit test for DTMManager/DTM
29 *
30 * Loads an XML document from a file (or, if no filename is supplied,
31 * an internal string), then dumps its contents. Replaces the old
32 * version, which was specific to the ultra-compressed implementation.
33 * (Which, by the way, we probably ought to revisit as part of our ongoing
34 * speed/size performance evaluation.)
35 *
36 * %REVIEW% Extend to test DOM2DTM, incremental, DOM view of the DTM,
37 * whitespace-filtered, indexed/nonindexed, ...
38 * */
39 public class TimeDTMTraverser
40 {
41
main(String argv[])42 public static void main(String argv[])
43 {
44 long dtmStart = 0; // Time the creation of dtmManager, and dtm initialization.
45
46 System.out.println("\n#### Timing Traversal of DEEP documents. ####");
47
48 StringBuffer buf = new StringBuffer();
49
50 // Preload once to prime the JVM.
51 DTM dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
52
53 // Time the creation of the dtm
54 buf.setLength(0);
55 dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
56 buf.setLength(0);
57 dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
58 buf.setLength(0);
59 dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
60 buf.setLength(0);
61 dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
62 buf.setLength(0);
63 dtm = QeDtmUtils.createDTM(0, QeDtmUtils.deepFile, buf);
64
65 // Get various nodes to use as context nodes.
66 int dtmRoot = dtm.getDocument(); // #document
67 String dtmRootName = dtm.getNodeName(dtmRoot); // Used for output
68 int DNode = dtm.getFirstChild(dtmRoot); // <Doc>
69 String DNodeName = dtm.getNodeName(DNode);
70 int ANode = dtm.getFirstChild(DNode); // <A>
71 String ANodeName = dtm.getNodeName(ANode);
72
73 int[] rtData = {0,0,0}; // returns Traversal time, last node, number of nodes traversed
74
75 // Get a traverser for Descendant:: axis.
76 System.out.println("\n* DESCENDANT from "+"<"+DNodeName+">");
77 QeDtmUtils.timeAxisTraverser(dtm, Axis.DESCENDANT, DNode, rtData);
78 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
79
80 // Get a traverser for Descendant:: axis.
81 System.out.println("\n* DESCENDANT-OR-SELF from "+"<"+DNodeName+">");
82 QeDtmUtils.timeAxisTraverser(dtm, Axis.DESCENDANTORSELF, DNode, rtData);
83 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
84
85 // Use last node from Child traverse as Context node for subsequent traversals
86 int lastNode = rtData[1];
87 String lastNodeName = dtm.getNodeName(lastNode);
88
89 // Get a traverser for Ancestor:: axis.
90 System.out.println("\n* ANCESTOR from "+"<"+lastNodeName+">");
91 QeDtmUtils.timeAxisTraverser(dtm, Axis.ANCESTOR, lastNode, rtData);
92 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
93
94 // Get a traverser for Ancestor:: axis.
95 System.out.println("\n* ANCESTOR-OR-SELF from "+"<"+lastNodeName+">");
96 QeDtmUtils.timeAxisTraverser(dtm, Axis.ANCESTORORSELF, lastNode, rtData);
97 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
98
99 System.out.println("\n#### Timing Traversal of FLAT documents. ####");
100
101 buf.setLength(0);
102 DTM dtm2 = QeDtmUtils.createDTM(0, QeDtmUtils.flatFile, buf);
103 buf.setLength(0);
104 dtm2 = QeDtmUtils.createDTM(0, QeDtmUtils.flatFile, buf);
105 buf.setLength(0);
106 dtm2 = QeDtmUtils.createDTM(0, QeDtmUtils.flatFile, buf);
107 buf.setLength(0);
108 dtm2 = QeDtmUtils.createDTM(0, QeDtmUtils.flatFile, buf);
109 buf.setLength(0);
110 dtm2 = QeDtmUtils.createDTM(0, QeDtmUtils.flatFile, buf);
111
112 // Get various nodes to use as context nodes.
113 dtmRoot = dtm2.getDocument(); // #document
114 dtmRootName = dtm2.getNodeName(dtmRoot); // Used for output
115 DNode = dtm2.getFirstChild(dtmRoot); // <Doc>
116 DNodeName = dtm2.getNodeName(DNode);
117 int fiNode = dtm2.getFirstChild(DNode); // first <item>
118 String fiNodeName = dtm2.getNodeName(fiNode);
119
120 // Get a traverser for Child:: axis.
121 System.out.println("\n* CHILD from "+"<"+DNodeName+"> " + DNode);
122 QeDtmUtils.timeAxisTraverser(dtm2, Axis.CHILD, DNode, rtData);
123 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
124
125 // Get a traverser for Following:: axis.
126 System.out.println("\n* FOLLOWING from "+"<"+fiNodeName+"> " + fiNode);
127 QeDtmUtils.timeAxisTraverser(dtm2, Axis.FOLLOWING, fiNode, rtData);
128 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
129
130 // Get a traverser for Following-sibling:: axis.
131 System.out.println("\n* FOLLOWINGSIBLING from "+"<"+fiNodeName+"> " +fiNode);
132 QeDtmUtils.timeAxisTraverser(dtm2, Axis.FOLLOWINGSIBLING, fiNode, rtData);
133 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
134
135 // Get a traverser for Descendant:: axis.
136 System.out.println("\n* DESCENDANT from "+"<"+DNodeName+"> " + DNode);
137 QeDtmUtils.timeAxisTraverser(dtm2, Axis.DESCENDANT, DNode, rtData);
138 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
139
140 // Use last node from Descendant traverse as Context node for subsequent traversals
141 lastNode = rtData[1];
142 lastNodeName = dtm2.getNodeName(lastNode);
143
144 // Get a traverser for Ancestor:: axis.
145 System.out.println("\n* ANCESTOR from "+"<"+lastNodeName+"> " + lastNode);
146 QeDtmUtils.timeAxisTraverser(dtm2, Axis.ANCESTOR, lastNode, rtData);
147 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
148
149 // Get a traverser for Preceding:: axis.
150 System.out.println("\n* PRECEDING from "+"<"+lastNodeName+"> " + lastNode);
151 QeDtmUtils.timeAxisTraverser(dtm2, Axis.PRECEDING, lastNode, rtData);
152 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
153
154 // Get a traverser for Preceding:: axis.
155 System.out.println("\n* PRECEDING-SIBLING from "+"<"+lastNodeName+"> " + lastNode);
156 QeDtmUtils.timeAxisTraverser(dtm2, Axis.PRECEDINGSIBLING, lastNode, rtData);
157 System.out.println("Time="+rtData[0] + " : " + "LastNode="+rtData[1]+" nodes="+rtData[2]);
158
159 }
160
161 }
162
163