1 /* Copyright 2002-2006 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@ibiblio.org. 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.IOException;
25 
26 import nu.xom.Attribute;
27 import nu.xom.Builder;
28 import nu.xom.Comment;
29 import nu.xom.DocType;
30 import nu.xom.Document;
31 import nu.xom.Element;
32 import nu.xom.Node;
33 import nu.xom.ParsingException;
34 import nu.xom.ProcessingInstruction;
35 import nu.xom.Text;
36 
37 /**
38  *
39  * <p>
40  * Benchmarks building a tree in memory by copying an existing document
41  * without using copy. Thus everything is reverified so that
42  * constructors and <code>Verifier</code> are hit heavily.
43  * </p>
44  *
45  * @author Elliotte Rusty Harold
46  * @version 1.2d1
47  *
48  */
49 class Reproducer {
50 
main(String[] args)51     public static void main(String[] args) {
52 
53         if (args.length <= 0) {
54           System.out.println(
55             "Usage: java nu.xom.benchmarks.Reproducer URL"
56           );
57           return;
58         }
59 
60         Reproducer iterator = new Reproducer();
61         Builder parser = new Builder();
62         try {
63             Document document = parser.build(args[0]);
64 
65             // warmup Hotspot
66             bench(iterator, document);
67 
68             long ms = 0;
69             int repeat = 100;
70             for (int i = 1; i < repeat; i++) {
71                 ms += bench(iterator, document);
72             }
73             System.out.println(ms/(double) repeat + "ms to build tree on average");
74 
75         }
76         catch (IOException ex) {
77           System.out.println(ex);
78         }
79         catch (ParsingException ex) {
80           System.out.println(ex);
81         }
82 
83     }
84 
bench(Reproducer iterator, Document document)85     private static long bench(Reproducer iterator, Document document) throws IOException {
86 
87         long prewalk = System.currentTimeMillis();
88         // Process it starting at the root
89         iterator.copy(document);
90         long postwalk = System.currentTimeMillis();
91         return postwalk - prewalk;
92     }
93 
copy(Document doc)94     private Document copy(Document doc)
95       throws IOException {
96 
97         Element originalRoot = doc.getRootElement();
98         Element root = copy(originalRoot);
99         Document copy = new Document(root);
100         copy.setBaseURI(doc.getBaseURI());
101         for (int i = 0; i < doc.getChildCount(); i++) {
102             Node child = doc.getChild(i);
103             if (child == originalRoot) continue;
104             Node node = copy(child);
105             copy.insertChild(node, i);
106         }
107         return copy;
108 
109     }
110 
111 
copy(Element original)112     private Element copy(Element original) {
113 
114         Element copy = new Element(original.getQualifiedName(),
115           original.getNamespaceURI());
116         for (int i = original.getAttributeCount()-1; i >= 0; i--) {
117             Attribute att = original.getAttribute(i);
118             copy.addAttribute(copy(att));
119         }
120         // Weird; need to find just the additional namespaces????
121         /* for (int i = original.getNamespaceDeclarationCount()-1; i >= 0; i--) {
122              copy.addNamespaceDeclaration(original.);
123         } */
124         for (int i = 0; i < original.getChildCount(); i++) {
125             Node node = copy(original.getChild(i));
126             copy.appendChild(node);
127         }
128         return copy;
129 
130     }
131 
132 
copy(Node node)133     private Node copy(Node node) {
134 
135         if (node instanceof Text) {
136             return copy((Text) node);
137         }
138         else if (node instanceof Element) {
139             return copy((Element) node);
140         }
141         else if (node instanceof Comment) {
142             return copy((Comment) node);
143         }
144         else if (node instanceof ProcessingInstruction) {
145             return copy((ProcessingInstruction) node);
146         }
147         else if (node instanceof DocType) {
148             return copy((DocType) node);
149         }
150         return null;
151 
152     }
153 
154 
copy(Text text)155     private Node copy(Text text) {
156         return new Text(text.getValue());
157     }
158 
159 
copy(Comment comment)160     private Node copy(Comment comment) {
161         return new Comment(comment.getValue());
162     }
163 
164 
copy(ProcessingInstruction pi)165     private Node copy(ProcessingInstruction pi) {
166         return new ProcessingInstruction(pi.getTarget(), pi.getValue());
167     }
168 
169 
copy(DocType doctype)170     private Node copy(DocType doctype) {
171         return new DocType(
172           doctype.getRootElementName(),
173           doctype.getPublicID(),
174           doctype.getSystemID());
175     }
176 
177 
copy(Attribute original)178     private Attribute copy(Attribute original) {
179 
180         Attribute copy = new Attribute(original.getQualifiedName(),
181           original.getNamespaceURI(),
182           original.getValue(),
183           original.getType());
184         return copy;
185 
186     }
187 
188 
189 }