1 2 import java.io.File ; 3 import java.io.FileWriter ; 4 import java.io.IOException ; 5 import java.util.List ; 6 7 import javax.xml.transform.Source ; 8 import javax.xml.transform.Transformer ; 9 import javax.xml.transform.TransformerFactory ; 10 import javax.xml.transform.stream.StreamSource ; 11 import org.dom4j.Document; 12 import org.dom4j.DocumentHelper; 13 import org.dom4j.Element; 14 import org.dom4j.Node; 15 import org.dom4j.XPath; 16 import org.dom4j.io.DOMWriter; 17 import org.dom4j.io.DocumentResult; 18 import org.dom4j.io.DocumentSource; 19 import org.dom4j.io.OutputFormat; 20 import org.dom4j.io.SAXReader; 21 import org.dom4j.io.XMLWriter; 22 23 public class PerfDOM4JAttr { 24 25 public static void main(String args[]) { 26 Document doc; 27 28 try { 29 int numrec = 1; 30 31 numrec = 10000; 32 System.out.println("\n1000 Elements -------------------"); 33 doc = PerfDOM4JAttr.createDocument(numrec, 20, 1); 34 PerfDOM4JAttr.createW3CDOM(doc); 35 PerfDOM4JAttr.write(doc, "DOM4JAttr_" + numrec + ".xml"); 36 PerfDOM4JAttr.parse(numrec, 1); 37 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"1\"]", 3); 39 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"500\"]", 3); 40 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"999\"]", 3); 41 PerfDOM4JAttr 42 .xpathNodes(doc, "/ItemResultSet/Item[@guid=\"1\"]", 3); 43 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"500\"]", 44 3); 45 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"999\"]", 46 3); 47 PerfDOM4JAttr.xpathNodes(doc, "/*/Item", 100); 48 49 numrec = 1000; 50 System.out.println("\n1000 Elements -------------------"); 51 doc = PerfDOM4JAttr.createDocument(numrec, 20, 1); 52 PerfDOM4JAttr.createW3CDOM(doc); 53 PerfDOM4JAttr.write(doc, "DOM4JAttr_" + numrec + ".xml"); 54 PerfDOM4JAttr.parse(numrec, 3); 55 PerfDOM4JAttr.transform(doc, "item.xslt", 3); 56 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"1\"]", 3); 57 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"500\"]", 3); 58 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"999\"]", 3); 59 PerfDOM4JAttr 60 .xpathNodes(doc, "/ItemResultSet/Item[@guid=\"1\"]", 3); 61 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"500\"]", 62 3); 63 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"999\"]", 64 3); 65 PerfDOM4JAttr.xpathNodes(doc, "/*/Item", 100); 66 67 numrec = 100; 68 System.out.println("\n100 Elements --------------------"); 69 doc = PerfDOM4JAttr.createDocument(numrec, 20, 10); 70 PerfDOM4JAttr.createW3CDOM(doc); 71 PerfDOM4JAttr.write(doc, "DOM4JAttr_" + numrec + ".xml"); 72 PerfDOM4JAttr.parse(numrec, 10); 73 PerfDOM4JAttr.transform(doc, "item.xslt", 10); 74 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"1\"]", 10); 75 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"50\"]", 10); 76 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"99\"]", 10); 77 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"1\"]", 78 10); 79 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"50\"]", 80 10); 81 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"99\"]", 82 10); 83 PerfDOM4JAttr.xpathNodes(doc, "/*/Item", 100); 84 85 numrec = 10; 86 System.out.println("\n10 Elements ---------------------"); 87 doc = PerfDOM4JAttr.createDocument(numrec, 20, 100); 88 PerfDOM4JAttr.createW3CDOM(doc); 89 PerfDOM4JAttr.write(doc, "DOM4JAttr_" + numrec + ".xml"); 90 PerfDOM4JAttr.parse(numrec, 100); 91 PerfDOM4JAttr.transform(doc, "item.xslt", 10); 92 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"1\"]", 100); 93 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"5\"]", 100); 94 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"9\"]", 100); 95 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"1\"]", 96 100); 97 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"5\"]", 98 100); 99 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"9\"]", 100 100); 101 PerfDOM4JAttr.xpathNodes(doc, "/*/Item", 100); 102 103 numrec = 1; 104 System.out.println("\n1 Element -----------------------"); 105 doc = PerfDOM4JAttr.createDocument(numrec, 20, 100); 106 PerfDOM4JAttr.createW3CDOM(doc); 107 PerfDOM4JAttr.write(doc, "DOM4JAttr_" + numrec + ".xml"); 108 PerfDOM4JAttr.parse(numrec, 100); 109 PerfDOM4JAttr.transform(doc, "item.xslt", 10); 110 PerfDOM4JAttr.xpath(doc, "/ItemResultSet/Item[@guid=\"1\"]", 100); 111 PerfDOM4JAttr.xpathNodes(doc, "/ItemResultSet/Item[@guid=\"1\"]", 112 100); 113 PerfDOM4JAttr.xpathNodes(doc, "/*/Item", 100); 114 115 } catch (IOException ie) { 116 ie.printStackTrace(); 117 } 118 } 119 120 public static Document createDocument(int iNumRecs, int iNumFlds, int pp) { 121 122 double start = System.currentTimeMillis(); 123 Document document = null; 124 for (int kk = 0; kk < pp; kk++) { 125 document = DocumentHelper.createDocument(); 126 127 Element root = document.addElement("ItemResultSet"); 128 for (int ii = 0; ii < iNumRecs; ii++) { 129 130 Element Record = root.addElement("Item").addAttribute("guid", 131 "" + ii); 132 for (int jj = 0; jj < iNumFlds; jj++) { 133 Record.addAttribute("Attr" + jj, "123456789"); 134 } 135 136 } 137 } 138 double end = System.currentTimeMillis(); 139 140 System.err.println("Creation time : " + (end - start) / pp); 141 142 return document; 143 } 144 145 public static Document parse(int iNumRecs, int kk) { 146 147 File file = new File ("DOM4JAttr_" + iNumRecs + ".xml"); 148 double start = System.currentTimeMillis(); 149 Document document = null; 150 for (int pp = 0; pp < kk; pp++) { 151 try { 152 SAXReader SAXrd = new SAXReader(); 153 SAXrd.read(file); 154 } catch (Exception e) { 155 e.printStackTrace(); 156 } 157 } 158 double end = System.currentTimeMillis(); 159 160 System.err.println("Parsing time for :" + 1.000 * (end - start) / kk); 161 162 return document; 163 } 164 165 public static void createW3CDOM(Document doc) { 166 167 long start = System.currentTimeMillis(); 168 try { 169 DOMWriter dw = new DOMWriter(); 170 dw.write(doc); 171 172 } catch (Exception de) { 173 } 174 175 long end = System.currentTimeMillis(); 176 177 System.err.println("W3C Creation time for :" + (end - start)); 178 } 179 180 public static void write(Document document, String name) throws IOException { 181 182 long start = System.currentTimeMillis(); 183 185 try { 186 OutputFormat format = OutputFormat.createPrettyPrint(); 187 XMLWriter writer = new XMLWriter(new FileWriter (name), format); 188 writer.write(document); 189 writer.close(); 190 191 } catch (IOException e) { 192 e.printStackTrace(); 193 } 194 195 long end = System.currentTimeMillis(); 196 197 System.err.println("DOM4JAttr File write time :" + (end - start) 198 + " " + name); 199 } 200 201 public static void transform(Document xmlDoc, String xslFile, int kk) { 202 203 System.err.println("DOM4JAttr start transform "); 204 int ii = 1; 205 try { 206 TransformerFactory factory = TransformerFactory.newInstance(); 207 Transformer transformer = factory.newTransformer(new StreamSource ( 208 xslFile)); 209 210 long start = System.currentTimeMillis(); 211 for (ii = 0; ii < kk; ii++) { 212 Source source = new DocumentSource(xmlDoc); 213 DocumentResult result = new DocumentResult(); 214 transformer.transform(source, result); 215 216 } 218 long end = System.currentTimeMillis(); 219 220 System.err.println("DOM4JAttr transform time :" + (end - start) 221 / ii); 222 } catch (Exception e) { 223 e.printStackTrace(); 224 } 225 226 } 227 228 public static void xpath(Document document, String xpathExp, int kk) { 229 230 long start = System.currentTimeMillis(); 231 XPath xpath = document.createXPath(xpathExp); 232 for (int ii = 0; ii < kk; ii++) { 233 Node node = xpath.selectSingleNode(document); 234 235 if ((node != null) & (ii == 0)) { 236 String val = node.getStringValue(); 237 } 239 } 240 241 long end = System.currentTimeMillis(); 242 System.err.println("DOM4JAttr xpath time :" + (end - start) / kk); 243 } 244 245 public static void xpathNodes(Document document, String xpathExp, int kk) { 246 247 long start = System.currentTimeMillis(); 248 XPath xpath = document.createXPath(xpathExp); 249 250 for (int ii = 0; ii < kk; ii++) { 251 252 try { 253 254 List nodeList = xpath.selectNodes(document); 255 if ((nodeList != null) && (nodeList.size() > 0)) { 256 Node node = (Node) nodeList.get(0); 257 if ((node != null) & (ii == 0)) { 258 String val = node.getStringValue(); 259 } 261 } 262 } catch (Exception e) { 263 e.printStackTrace(); 264 265 } 266 267 } 268 269 long end = System.currentTimeMillis(); 270 271 System.err.println("DOM4JAttr xpath Nodes time :" + 1.000 272 * (end - start) / kk); 273 } 274 275 } 276 | Popular Tags |