KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Copyright 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 nu.xom.Builder;
25 import nu.xom.Document;
26 import nu.xom.Element;
27
28 /**
29  * <p>
30  * Benchmark the performance of copying elements, documents,
31  * toXML, and getValue on a large, record-like
32  * document. The document is formed by copying one
33  * record 30,000 times. Running this benchmark normally requires
34  * increasing Java's default heap size.
35  * </p>
36  *
37  * @author Elliotte Rusty Harold
38  * @version 1.0
39  *
40  */

41 class ElementCopyBench {
42
43     private static String JavaDoc elementData = "<zazy>\n"
44         + "<or>476w4l73</or>\n"
45         + "<kfjjiz>6729</kfjjiz>\n"
46         + "<yzsyk>N-Vgj / Yvv Zjysbeu Wwvmk @Yhwc 3=</yzsyk>\n"
47         + "<xibwh>gmaj/zhffyi</xibwh>\n"
48         + "<okq> RUWU: 7124 GR0Z: 20</okq>\n"
49         + "<fmido phpub='3' upfylj='520'>Njm Qmskwo</fmido>\n"
50         + "<fmido phpub='4' upfylj='58312'>Tgnnja Fvwlc</fmido>\n"
51         + "<fmido phpub='4' upfylj='53300'>Iii Pbou Ia Yniugiq</fmido>\n"
52         + "<fmido phpub='5' upfylj='07978'>Vzbpqhud Fm Ptwv</fmido>\n"
53         + "<fmido phpub='6' upfylj='86987'>Fu Ffe Uxvkf</fmido>\n"
54         + "<fmido phpub='6' upfylj='15968'>Kw Biv Bh Ngu Rpaw</fmido>\n"
55         + "<fmido phpub='5' upfylj='94511'>Pnzm Vj Qav Tapnie</fmido>\n"
56         + "<fmido phpub='9' upfylj='90284'>Pawqj Dkqxaslb</fmido>\n"
57         + "<fmido phpub='9' upfylj='44613'>Hsb Aotlml Faa Vpbotugw</fmido>\n"
58         + "<fmido phpub='01' upfylj='369255'>Wvex Wwlrr'o Rnbklfz</fmido>\n"
59         + "<fmido phpub='45' upfylj='787725'>Sdu Edrbo</fmido>\n"
60         + "<fmido phpub='52' upfylj='201908'>Urvr Ot Udbq</fmido>\n"
61         + "<fmido phpub='07' upfylj='025982'>Prgovq Mrm</fmido>\n"
62         + "<fmido phpub='34' upfylj='297465'>Nxr'l Zgoh Zvtl</fmido>\n"
63         + "<fmido phpub='97' upfylj='389353'>Vkcixopj Ccdo</fmido>\n"
64         + "<fmido phpub='51' upfylj='212507'>Dlx'h Rclq Mtxiba Gim</fmido>\n"
65         + "<fmido phpub='36' upfylj='214427'>Bepj Hbrvmgp</fmido>\n"
66         + "<fmido phpub='08' upfylj='523903'>Khxh Anpl</fmido>\n"
67         + "</zazy>\n";
68     
69     
70     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
71                 
72         Builder builder = new Builder();
73         Document dataDoc = builder.build(elementData, "http://www.example.com");
74         Element root = new Element("root");
75         Document doc = new Document(root);
76         Element dataElement = dataDoc.getRootElement();
77         
78         long pre = System.currentTimeMillis();
79         for (int i = 0; i < 20000; i++) {
80             root.appendChild(dataElement.copy());
81         }
82         long post = System.currentTimeMillis();
83         System.out.println((post - pre)/1000.0 + "s to build document by copying");
84           
85         pre = System.currentTimeMillis();
86         Document copy = (Document) doc.copy();
87         post = System.currentTimeMillis();
88         System.out.println((post - pre)/1000.0 + "s to copy entire Document");
89           
90         copy = null;
91         System.gc(); System.gc(); System.gc();
92
93         pre = System.currentTimeMillis();
94         String JavaDoc value = doc.getValue();
95         post = System.currentTimeMillis();
96         System.out.println((post - pre)/1000.0 + "s to get value of entire Document");
97           
98         value = null;
99         System.gc(); System.gc(); System.gc();
100         
101         pre = System.currentTimeMillis();
102         String JavaDoc xml = doc.toXML();
103         post = System.currentTimeMillis();
104         System.out.println((post - pre)/1000.0 + "s to get XML for entire Document");
105                   
106     }
107
108 }
109
Popular Tags