1 21 22 package nu.xom.benchmarks; 23 24 import java.io.BufferedInputStream ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.io.UnsupportedEncodingException ; 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 51 class TreeWalker { 52 53 public static void main(String [] 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 Document doc = parser.build(args[0]); 69 DocType type = doc.getDocType(); 70 if (type != null) { 71 doc.removeChild(type); 72 } 73 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 System.gc(); System.gc(); System.gc(); 84 85 long prebuild = System.currentTimeMillis(); 86 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 walkTree(iterator, document); 95 long postwalk = System.currentTimeMillis(); 96 System.out.println((postwalk - prewalk) 97 + "ms to walk tree"); 98 99 OutputStream result = new ByteArrayOutputStream (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 (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 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 out) 132 throws UnsupportedEncodingException , IOException { 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 out) 140 throws UnsupportedEncodingException , IOException { 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 out) 149 throws UnsupportedEncodingException , IOException { 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 { 157 iterator.followNode(doc); 158 } 159 160 private static Document build( 161 byte[] data, String base, Builder parser) 162 throws ParsingException, ValidityException, IOException { 163 InputStream raw = new BufferedInputStream ( 164 new ByteArrayInputStream (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 base) 172 throws IOException , ParsingException { 173 for (int i = 0; i < numPasses; i++) { 174 InputStream raw = new BufferedInputStream ( 175 new ByteArrayInputStream (data) 176 ); 177 Document doc = parser.build(raw, base); 178 walkTree(iterator, doc); 179 serialize(doc, new ByteArrayOutputStream (3000000)); 180 prettyPrint(doc, new ByteArrayOutputStream (4000000)); 181 serializeUTF16(doc, new ByteArrayOutputStream (4000000)); 182 } 183 } 184 185 public void followNode(Node node) throws IOException { 187 188 for (int i = 0; i < node.getChildCount(); i++) { 189 followNode(node.getChild(i)); 190 } 191 192 } 193 194 } | Popular Tags |