1
/* Copyright 2002, 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.benchmarks;
24
import java.io.IOException;
25
import java.text.DecimalFormat;
27
import nu.xom.Attribute;
28
import nu.xom.Builder;
29
import nu.xom.Document;
30
import nu.xom.Element;
31
import nu.xom.ParsingException;
35
* Simple memory benchmark focusing on a big document
36
* full of small elements or a user supplied document.
40
* @author Elliotte Rusty Harold
46
public static Document makeFullUnicode() {
48
Element root = new Element("root");
49
Document doc = new Document(root);
51
for (int i = 0x20; i <= 0xD7FF; i++) {
52
Element data = new Element("d");
53
data.appendChild(((char) i) + "");
54
data.addAttribute(new Attribute("c", String.valueOf(i)));
55
root.appendChild(data);
58
// skip surrogates between 0xD800 and 0xDFFF
60
for (int i = 0xE000; i <= 0xFFFD; i++) {
61
Element data = new Element("d");
62
data.appendChild(((char) i) + "");
63
data.addAttribute(new Attribute("c", String.valueOf(i)));
64
root.appendChild(data);
69
// Plane-1 characters are tricky because Java
70
// strings encode them as surrogate pairs. First, fill
71
// a byte array with the characters from 1D100 to 1D1FF
72
// (the musical symbols)
73
for (int i = 0; i < 256; i++) {
74
// algorithm from RFC 2781
76
int uprime = u - 0x10000;
79
W2 = W2 | (uprime & 0x7FF );
80
W1 = W1 | (uprime & 0xFF800);
81
Element data = new Element("d");
82
data.appendChild( ((char) W1) + "" + ((char) W2) );
83
data.addAttribute(new Attribute("c", String.valueOf(u)));
84
root.appendChild(data);
91
public static void main(String[] args)
92
throws IOException, ParsingException {
94
DecimalFormat format = new DecimalFormat();
95
format.setMaximumFractionDigits(2);
96
Runtime r = Runtime.getRuntime();
97
System.gc(); System.gc(); System.gc();
98
long before = r.totalMemory() - r.freeMemory();
100
if (args.length > 0) {
101
Builder builder = new Builder();
102
doc = builder.build(args[0]);
106
doc = makeFullUnicode();
108
long after = r.totalMemory() - r.freeMemory();
109
double usage = (after - before)/(1024.0*1024.0);
110
System.out.println("Memory used: "
111
+ format.format(usage) + "M");
112
System.gc(); System.gc(); System.gc();
113
long postGC = r.totalMemory() - r.freeMemory();
114
usage = (postGC - before)/(1024.0*1024.0);
115
System.out.println("Memory used after garbage collection: "
116
+ format.format(usage) + "M");
118
// Make sure the document isn't prematurely garbage collected
119
System.out.println("Meaningless number: "
120
+ doc.toXML().length());