KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
25 import java.text.DecimalFormat JavaDoc;
26
27 import nu.xom.Builder;
28 import nu.xom.Document;
29 import nu.xom.Element;
30 import nu.xom.ParsingException;
31
32 /**
33  * <p>
34  * Simple memory benchmark focusing on a big document
35  * with lots of text covering all of Unicode.
36  * </p>
37  *
38  * @author Elliotte Rusty Harold
39  * @version 1.0
40  *
41  */

42 class BigText {
43
44     static Document makeFullUnicode() {
45         
46         Element root = new Element("root");
47         Document doc = new Document(root);
48
49         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(65536);
50         for (int i = 0x20; i <= 0xD7FF; i++) {
51             sb.append((char) i);
52         }
53         
54         // skip surrogates between 0xD800 and 0xDFFF
55

56         for (int i = 0xE000; i <= 0xFFFD; i++) {
57             sb.append((char) i);
58         }
59         
60         System.gc();
61
62         // Plane-1 characters are tricky because Java
63
// strings encode them as surrogate pairs. First, fill
64
// a byte array with the characters from 1D100 to 1D1FF
65
// (the musical symbols)
66
for (int i = 0; i < 256; i++) {
67             // algorithm from RFC 2781
68
int u = 0x1D100 + i;
69             int uprime = u - 0x10000;
70             int W1 = 0xD800;
71             int W2 = 0xDC00;
72             W2 = W2 | (uprime & 0x7FF );
73             W1 = W1 | (uprime & 0xFF800);
74             sb.append( ((char) W1) + "" + ((char) W2) );
75         }
76         
77         root.appendChild(sb.toString());
78         
79         return doc;
80         
81     }
82
83     public static void main(String JavaDoc[] args)
84       throws IOException JavaDoc, ParsingException {
85         
86         DecimalFormat JavaDoc format = new DecimalFormat JavaDoc();
87         format.setMaximumFractionDigits(2);
88         Runtime JavaDoc r = Runtime.getRuntime();
89         System.gc(); System.gc(); System.gc();
90         long before = r.totalMemory() - r.freeMemory();
91         Document doc;
92         if (args.length > 0) {
93             Builder builder = new Builder();
94             doc = builder.build(args[0]);
95             builder = null;
96         }
97         else {
98             doc = makeFullUnicode();
99         }
100         long after = r.totalMemory() - r.freeMemory();
101         double usage = (after - before)/(1024.0*1024.0);
102         System.out.println("Memory used: "
103           + format.format(usage) + "M");
104         System.gc(); System.gc(); System.gc();
105         long postGC = r.totalMemory() - r.freeMemory();
106         usage = (postGC - before)/(1024.0*1024.0);
107         System.out.println("Memory used after garbage collection: "
108           + format.format(usage) + "M");
109        
110         // Make sure the document isn't prematurely garbage collected
111
System.out.println("Meaningless number: "
112           + doc.toXML().length());
113     }
114
115 }
116
Popular Tags