KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > PerfXML


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution;
10
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13
14 import j2me.io.ObjectInputStream;
15 import j2me.io.ObjectOutputStream;
16 import j2me.io.ObjectInput;
17 import j2me.io.ObjectOutput;
18 import j2me.lang.UnsupportedOperationException;
19 import j2me.nio.ByteBuffer;
20 import javolution.io.UTF8ByteBufferReader;
21 import javolution.io.UTF8ByteBufferWriter;
22 import javolution.lang.MathLib;
23 import javolution.text.Text;
24 import javolution.text.TextBuilder;
25 import javolution.util.FastComparator;
26 import javolution.util.FastList;
27 import javolution.util.FastMap;
28 import javolution.util.FastSet;
29 import javolution.util.FastTable;
30 import javolution.util.Index;
31 import javolution.xml.XMLBinding;
32 import javolution.xml.XMLObjectReader;
33 import javolution.xml.XMLObjectWriter;
34
35 /**
36  * <p> This class holds {@link javolution.xml} benchmark.</p>
37  *
38  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle </a>
39  * @version 3.6, March 28, 2005
40  */

41 final class PerfXML extends Javolution implements Runnable JavaDoc {
42
43     private static final int ITERATIONS = 1000; // The nbr of iterations.
44

45     private static final int OBJECT_SIZE = 1; // Nbr of tables per object.
46

47     private static final int BUFFER_SIZE = 1400 * OBJECT_SIZE;
48
49     private ByteArrayOutputStream JavaDoc _Stream = new ByteArrayOutputStream JavaDoc(
50             BUFFER_SIZE);
51
52     private ByteBuffer _Buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
53
54     /**
55      * Executes benchmark.
56      */

57     public void run() {
58         println("/////////////////////////////");
59         println("// Package: javolution.xml //");
60         println("/////////////////////////////");
61
62         println("");
63         println("-- Java(TM) Serialization --");
64         benchmarkJavaSerialization(); // Warming up.
65

66         println("");
67         println("-- XML Serialization (I/O Stream) --");
68         benchmarkXmlIoSerialization(); // Warming up.
69

70         println("");
71         println("-- XML Serialization (NIO ByteBuffer) --");
72         benchmarkXmlNioSerialization(); // Warming up.
73

74         println("");
75     }
76
77     private void benchmarkJavaSerialization() {
78         try {
79             print("Write Time: ");
80             for (int i = 0; i < ITERATIONS; i++) {
81                 ObjectOutput oo = new ObjectOutputStream(_Stream);
82                 Object JavaDoc data = newData();
83                 startTime();
84                 oo.writeObject(data);
85                 oo.close();
86                 keepBestTime(1);
87                 _Stream.reset();
88             }
89             println(endTime());
90
91             print("Read Time: ");
92             for (int i = 0; i < ITERATIONS; i++) {
93                 // Creates input.
94
ObjectOutput oo = new ObjectOutputStream(_Stream);
95                 Object JavaDoc data = newData();
96                 oo.writeObject(data);
97                 oo.close();
98                 ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(_Stream
99                         .toByteArray());
100                 _Stream.reset();
101
102                 ObjectInput oi = new ObjectInputStream(in);
103                 startTime();
104                 Object JavaDoc readObject = oi.readObject();
105                 oi.close();
106                 keepBestTime(1);
107                 if (!data.equals(readObject))
108                     throw new Error JavaDoc("SERIALIZATION ERROR");
109             }
110             println(endTime());
111         } catch (UnsupportedOperationException JavaDoc e) {
112             println("NOT SUPPORTED (J2SE 1.4+ build required)");
113         } catch (Throwable JavaDoc e) {
114             throw new JavolutionError(e);
115         }
116     }
117
118     private void benchmarkXmlIoSerialization() {
119         XMLBinding binding = new XMLBinding();
120         binding.setAlias(String JavaDoc.class, "String");
121         try {
122             print("Write Time: ");
123             for (int i = 0; i < ITERATIONS; i++) {
124                 XMLObjectWriter ow = XMLObjectWriter.newInstance(_Stream);
125                 ow.setBinding(binding);
126                 Object JavaDoc data = newData();
127                 startTime();
128                 ow.write(data);
129                 ow.close();
130                 keepBestTime(1);
131                 _Stream.reset();
132             }
133             println(endTime());
134
135             print("Read Time: ");
136             for (int i = 0; i < ITERATIONS; i++) {
137                 // Creates input.
138
XMLObjectWriter ow = XMLObjectWriter.newInstance(_Stream);
139                 ow.setBinding(binding);
140                 Object JavaDoc data = newData();
141                 ow.write(data);
142                 ow.close();
143                 ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(_Stream
144                         .toByteArray());
145                 _Stream.reset();
146
147                 XMLObjectReader or = XMLObjectReader.newInstance(in);
148                 or.setBinding(binding);
149                 startTime();
150                 Object JavaDoc readObject = or.read();
151                 or.close();
152                 keepBestTime(1);
153                 if (!data.equals(readObject))
154                     throw new Error JavaDoc("SERIALIZATION ERROR");
155             }
156
157             println(endTime());
158
159         } catch (UnsupportedOperationException JavaDoc e) {
160             println("NOT SUPPORTED (J2SE 1.4+ build required)");
161         } catch (Throwable JavaDoc e) {
162             throw new JavolutionError(e);
163         }
164     }
165
166     private void benchmarkXmlNioSerialization() {
167         try {
168             print("Write Time: ");
169             for (int i = 0; i < ITERATIONS; i++) {
170                 XMLObjectWriter ow = XMLObjectWriter
171                         .newInstance(new UTF8ByteBufferWriter()
172                                 .setOutput(_Buffer));
173                 Object JavaDoc data = newData();
174                 startTime();
175                 ow.write(data);
176                 ow.close();
177                 keepBestTime(1);
178                 _Buffer.clear();
179             }
180             println(endTime());
181
182             print("Read Time: ");
183             for (int i = 0; i < ITERATIONS; i++) {
184                 // Creates input.
185
XMLObjectWriter ow = XMLObjectWriter
186                         .newInstance(new UTF8ByteBufferWriter()
187                                 .setOutput(_Buffer));
188                 Object JavaDoc data = newData();
189                 ow.write(data);
190                 ow.close();
191                 _Buffer.flip();
192
193                 XMLObjectReader or = XMLObjectReader
194                         .newInstance(new UTF8ByteBufferReader()
195                                 .setInput(_Buffer));
196                 startTime();
197                 Object JavaDoc readObject = or.read();
198                 or.close();
199                 keepBestTime(1);
200                 if (!data.equals(readObject))
201                     throw new Error JavaDoc("SERIALIZATION ERROR");
202                 _Buffer.clear();
203             }
204             println(endTime());
205         } catch (UnsupportedOperationException JavaDoc e) {
206             println("NOT SUPPORTED (J2SE 1.4+ build required)");
207         } catch (Throwable JavaDoc e) {
208             throw new JavolutionError(e);
209         }
210     }
211
212     private static Object JavaDoc newData() {
213         FastTable v = new FastTable(OBJECT_SIZE);
214         for (int i = 0; i < OBJECT_SIZE; i++) {
215             FastTable ft = new FastTable();
216             ft.add("This is the first String (" + i + ")");
217             ft.add("This is the second String (" + i + ")");
218             ft.add("This is the third String (" + i + ")");
219             v.add(ft);
220             v.add(null);
221             v.add(TextBuilder.newInstance().append(Long.MAX_VALUE));
222             v.add(Text.valueOf(Long.MAX_VALUE, 16));
223             FastMap fm = new FastMap();
224             fm.setKeyComparator(FastComparator.IDENTITY);
225             fm.put(Index.valueOf(i), "INDEX+" + i);
226             fm.put(Index.valueOf(-i), "INDEX-" + i);
227             v.add(fm);
228             // Adds miscellaneous data.
229
FastList fl = new FastList();
230             fl.add("FIRST" + "(" + i + ")");
231             fl.add("SECOND" + "(" + i + ")");
232             fl.add("THIRD" + "(" + i + ")");
233             fl.add("<&'>>>");
234             v.add(fl);
235             FastSet fs = new FastSet();
236             fs.add(new Integer JavaDoc(MathLib.random(Integer.MIN_VALUE,
237                     Integer.MAX_VALUE)));
238             fs.add(new Long JavaDoc(MathLib.random(Long.MIN_VALUE, Long.MAX_VALUE)));
239             fs.add("".getClass());
240             v.add(fs);
241         }
242         return v;
243     }
244 }
Popular Tags