KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > benchmarks > TreeWalker


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.benchmarks;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.UnsupportedEncodingException JavaDoc;
31
32 import nu.xom.Builder;
33 import nu.xom.DocType;
34 import nu.xom.Document;
35 import nu.xom.Node;
36 import nu.xom.ParsingException;
37 import nu.xom.Serializer;
38 import nu.xom.ValidityException;
39
40 /**
41  *
42  * <p>
43  * Benchmark performance against a specified URL for
44  * building, tree-walking, and serializing.
45  * </p>
46  *
47  * @author Elliotte Rusty Harold
48  * @version 1.0
49  *
50  */

51 class TreeWalker {
52
53     public static void main(String JavaDoc[] args) {
54      
55         if (args.length <= 0) {
56           System.out.println(
57             "Usage: java nu.xom.benchmarks.TreeWalker URL"
58           );
59           return;
60         }
61          
62         TreeWalker iterator = new TreeWalker();
63         Builder parser = new Builder();
64         try {
65             // Separate out the basic I/O by parsing document,
66
// and then serializing into a byte array. This caches
67
// the document and removes any dependence on the DTD.
68
Document doc = parser.build(args[0]);
69             DocType type = doc.getDocType();
70             if (type != null) {
71                 doc.removeChild(type);
72             }
73             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
74             Serializer serializer = new Serializer(out);
75             serializer.write(doc);
76             serializer.flush();
77             out.close();
78             byte[] data = out.toByteArray();
79                        
80             warmup(data, parser, iterator, 5, args[0]);
81             
82             // try to avoid garbage collection pauses
83
System.gc(); System.gc(); System.gc();
84             
85             long prebuild = System.currentTimeMillis();
86             // Read the entire document into memory
87
Document document = build(data, args[0], parser);
88             long postbuild = System.currentTimeMillis();
89             System.out.println((postbuild - prebuild)
90               + "ms to build the tree");
91            
92             long prewalk = System.currentTimeMillis();
93             // Process it starting at the root
94
walkTree(iterator, document);
95             long postwalk = System.currentTimeMillis();
96             System.out.println((postwalk - prewalk)
97               + "ms to walk tree");
98             
99             OutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc(3000000);
100
101             long preserialize = System.currentTimeMillis();
102             serialize(document, result);
103             long postserialize = System.currentTimeMillis();
104             System.out.println((postserialize - preserialize)
105               + "ms to serialize the tree in UTF-8");
106
107             out = new ByteArrayOutputStream JavaDoc(4000000);
108             long preprettyserialize = System.currentTimeMillis();
109             prettyPrint(document, result);
110             long postprettyserialize = System.currentTimeMillis();
111             System.out.println(
112              (postprettyserialize - preprettyserialize)
113               + "ms to pretty print the tree in UTF-8");
114
115             long preUTF16serialize = System.currentTimeMillis();
116             serializeUTF16(document, result);
117             long postUTF16serialize = System.currentTimeMillis();
118             System.out.println((postUTF16serialize - preUTF16serialize)
119               + "ms to serialize the tree in UTF-16");
120         }
121         catch (IOException JavaDoc ex) {
122           System.out.println(ex);
123         }
124         catch (ParsingException ex) {
125           System.out.println(ex);
126         }
127   
128     }
129
130     private static void serializeUTF16(
131       Document document, OutputStream JavaDoc out)
132         throws UnsupportedEncodingException JavaDoc, IOException JavaDoc {
133         Serializer serializer = new Serializer(out, "UTF-16");
134         serializer.write(document);
135         serializer.flush();
136     }
137
138     private static void prettyPrint(
139       Document document, OutputStream JavaDoc out)
140         throws UnsupportedEncodingException JavaDoc, IOException JavaDoc {
141         Serializer serializer = new Serializer(out, "UTF-8");
142         serializer.setIndent(2);
143         serializer.setMaxLength(72);
144         serializer.write(document);
145         serializer.flush();
146     }
147
148     private static void serialize(Document document, OutputStream JavaDoc out)
149         throws UnsupportedEncodingException JavaDoc, IOException JavaDoc {
150         Serializer serializer = new Serializer(out, "UTF-8");
151         serializer.write(document);
152         serializer.flush();
153     }
154
155     private static void walkTree(TreeWalker iterator, Document doc)
156       throws IOException JavaDoc {
157         iterator.followNode(doc);
158     }
159     
160     private static Document build(
161       byte[] data, String JavaDoc base, Builder parser)
162       throws ParsingException, ValidityException, IOException JavaDoc {
163         InputStream JavaDoc raw = new BufferedInputStream JavaDoc(
164           new ByteArrayInputStream JavaDoc(data)
165         );
166         Document document = parser.build(raw, base);
167         return document;
168     }
169
170     private static void warmup(byte[] data, Builder parser,
171       TreeWalker iterator, int numPasses, String JavaDoc base)
172       throws IOException JavaDoc, ParsingException {
173         for (int i = 0; i < numPasses; i++) {
174             InputStream JavaDoc raw = new BufferedInputStream JavaDoc(
175               new ByteArrayInputStream JavaDoc(data)
176             );
177             Document doc = parser.build(raw, base);
178             walkTree(iterator, doc);
179             serialize(doc, new ByteArrayOutputStream JavaDoc(3000000));
180             prettyPrint(doc, new ByteArrayOutputStream JavaDoc(4000000));
181             serializeUTF16(doc, new ByteArrayOutputStream JavaDoc(4000000));
182         }
183     }
184
185     // note use of recursion
186
public void followNode(Node node) throws IOException JavaDoc {
187     
188         for (int i = 0; i < node.getChildCount(); i++) {
189             followNode(node.getChild(i));
190         }
191     
192     }
193
194 }
Popular Tags