KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > PerfStream


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - 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 /**
15  * <p> This class holds {@link javolution.xml.stream} benchmark.</p>
16  *
17  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
18  * @version 4.0, September 4, 2004
19  */

20 final class PerfStream extends Javolution implements Runnable JavaDoc {
21
22     static final int N = 1; // Number of elements (in/out).
23

24     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
25
26     ByteArrayInputStream JavaDoc in;
27
28      /**
29      * Executes benchmark.
30      */

31     public void run() {
32         println("////////////////////////////////////");
33         println("// Package: javolution.xml.stream //");
34         println("////////////////////////////////////");
35         println("");
36         
37         try {
38             /*@JVM-1.6+@
39             print("StAX XMLStreamWriter (namespace repairing disabled): ");
40             for (int i=0; i < 1000; i++) {
41                 startTime();
42                 staxStreamWriter(false);
43                 keepBestTime(1);
44                 out.reset();
45             }
46             println(endTime());
47             /**/

48
49             /*@JVM-1.4+@
50             print("Javolution XMLStreamWriter (namespace repairing disabled): ");
51             for (int i=0; i < 1000; i++) {
52                 startTime();
53                 javoStreamWriter(false);
54                 keepBestTime(1);
55                 out.reset();
56             }
57             println(endTime());
58             /**/

59
60             /*@JVM-1.6+@
61             print("StAX XMLStreamWriter (namespace repairing enabled): ");
62             for (int i=0; i < 1000; i++) {
63                 startTime();
64                 staxStreamWriter(true);
65                 keepBestTime(1);
66                 out.reset();
67             }
68             println(endTime());
69             /**/

70
71             /*@JVM-1.4+@
72             print("Javolution XMLStreamWriter (namespace repairing enabled): ");
73             for (int i=0; i < 1000; i++) {
74                 startTime();
75                 javoStreamWriter(true);
76                 keepBestTime(1);
77                 out.reset();
78             }
79             println(endTime());
80             /**/

81
82             /*@JVM-1.6+@
83             print("StAX XMLStreamReader: ");
84             for (int i=0; i < 1000; i++) {
85                 staxCreateInput();
86                 startTime();
87                 staxStreamReader();
88                 keepBestTime(1);
89             }
90             println(endTime());
91             /**/

92
93             /*@JVM-1.4+@
94             print("Javolution XMLStreamReader: ");
95             for (int i=0; i < 1000; i++) {
96                 javoCreateInput();
97                 startTime();
98                 javoStreamReader();
99                 keepBestTime(1);
100             }
101             println(endTime());
102             /**/

103
104             println("");
105         } catch (Exception JavaDoc e) {
106             e.printStackTrace();
107         }
108     }
109
110     /*@JVM-1.6+@
111     private void staxStreamWriter(boolean repairingNamespaces) throws Exception {
112         javax.xml.stream.XMLOutputFactory outFactory = javax.xml.stream.XMLOutputFactory.newInstance();
113         outFactory.setProperty(javax.xml.stream.XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(repairingNamespaces));
114         javax.xml.stream.XMLStreamWriter writer = outFactory.createXMLStreamWriter(out, "UTF-8");
115
116         // Block identical for StAX or Javolution.
117         //
118         writer.writeStartDocument("UTF-8", "1.0");
119         writer.setPrefix("ns0", "http://javolution.org/namespace/root");
120         writer.setPrefix("ns1", "http://javolution.org/namespace/first");
121         writer.setPrefix("ns2", "http://javolution.org/namespace/second");
122         writer.setPrefix("ns3", "http://javolution.org/namespace/third");
123         writer.writeStartElement("http://javolution.org/namespace/root", "Element_0");
124         if (!repairingNamespaces) {
125             writer.writeNamespace("ns0", "http://javolution.org/namespace/root");
126             writer.writeNamespace("ns1", "http://javolution.org/namespace/first");
127             writer.writeNamespace("ns2", "http://javolution.org/namespace/second");
128             writer.writeNamespace("ns3", "http://javolution.org/namespace/third");
129         }
130         for (int i = 0; i < N; i++) {
131             writer.writeStartElement("http://javolution.org/namespace/first",
132                     "Element_1");
133             for (int j = 0; j < 10; j++) {
134                 writer.writeStartElement("http://javolution.org/namespace/second",
135                         "Element_2");
136                 for (int k = 0; k < 10; k++) {
137                     writer.writeEmptyElement("http://javolution.org/namespace/third",
138                             "Element_3");
139                     writer.writeAttribute("http://javolution.org/namespace/first",
140                             "Attribute_1", "Value of first attribute");
141                     writer.writeAttribute("http://javolution.org/namespace/second",
142                             "Attribute_2", "Value of second attribute");
143                     writer.writeAttribute("http://javolution.org/namespace/third",
144                             "Attribute_3", "Value of third attribute");
145                     writer.writeCData("This is a block of CDATA being written");
146                     writer.writeCharacters("Here characters (&,<,>) are escaped");
147                     writer.writeCData("And finally a last CDATA block to be merged (coalescing performed)");
148                     writer.writeStartElement("http://javolution.org/namespace/root", "TextOnlyElement");
149                     writer.writeCharacters("Here are some characters"); // 24 characters
150                     writer.writeComment("Comments are ignored !!");
151                     writer.writeCharacters("More characters to be coalesced"); // 31 characters
152                     writer.writeCData("The final text should be 97 character long"); // 42 characters
153                     writer.writeEndElement();
154                 }
155                 writer.writeEndElement();
156             }
157             writer.writeEndElement();
158         }
159         writer.writeEndDocument();
160         writer.close();
161         //
162         // End Block.
163     }
164     /**/

165     
166     /*@JVM-1.4+@
167     private void javoStreamWriter(boolean repairingNamespaces) throws Exception {
168         javolution.xml.stream.XMLOutputFactory outFactory = javolution.xml.stream.XMLOutputFactory.newInstance();
169         outFactory.setProperty(javolution.xml.stream.XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(repairingNamespaces));
170         javolution.xml.stream.XMLStreamWriter writer = outFactory.createXMLStreamWriter(out, "UTF-8");
171
172         // Block identical for StAX or Javolution.
173         //
174         writer.writeStartDocument();
175         writer.setPrefix("ns0", "http://javolution.org/namespace/root");
176         writer.setPrefix("ns1", "http://javolution.org/namespace/first");
177         writer.setPrefix("ns2", "http://javolution.org/namespace/second");
178         writer.setPrefix("ns3", "http://javolution.org/namespace/third");
179         writer.writeStartElement("http://javolution.org/namespace/root", "Element_0");
180         if (!repairingNamespaces) {
181             writer.writeNamespace("ns0", "http://javolution.org/namespace/root");
182             writer.writeNamespace("ns1", "http://javolution.org/namespace/first");
183             writer.writeNamespace("ns2", "http://javolution.org/namespace/second");
184             writer.writeNamespace("ns3", "http://javolution.org/namespace/third");
185         }
186         for (int i = 0; i < N; i++) {
187             writer.writeStartElement("http://javolution.org/namespace/first",
188                     "Element_1");
189             for (int j = 0; j < 10; j++) {
190                 writer.writeStartElement("http://javolution.org/namespace/second",
191                         "Element_2");
192                 for (int k = 0; k < 10; k++) {
193                     writer.writeEmptyElement("http://javolution.org/namespace/third",
194                             "Element_3");
195                     writer.writeAttribute("http://javolution.org/namespace/first",
196                             "Attribute_1", "Value of first attribute");
197                     writer.writeAttribute("http://javolution.org/namespace/second",
198                             "Attribute_2", "Value of second attribute");
199                     writer.writeAttribute("http://javolution.org/namespace/third",
200                             "Attribute_3", "Value of third attribute");
201                     writer.writeCData("This is a block of CDATA being written");
202                     writer.writeCharacters("Here characters (&,<,>) are escaped");
203                     writer.writeCData("And finally a last CDATA block to be merged (coalescing performed)");
204                     writer.writeStartElement("http://javolution.org/namespace/root",
205                             "TextOnlyElement");
206                     writer.writeCharacters("Here are some characters"); // 24 characters
207                     writer.writeComment("Comments are ignored");
208                     writer.writeCharacters("More characters to be coalesced"); // 31 characters
209                     writer.writeCData("The final text should be 97 character long"); // 42 characters
210                     writer.writeEndElement();
211                 }
212                 writer.writeEndElement();
213             }
214             writer.writeEndElement();
215         }
216         writer.writeEndDocument();
217         writer.close();
218         //
219         // End Block.
220     }
221     /**/

222     
223     /*@JVM-1.6+@
224     private void staxStreamReader() throws Exception {
225         javax.xml.stream.XMLInputFactory inFactory = javax.xml.stream.XMLInputFactory.newInstance();
226         inFactory.setProperty(javax.xml.stream.XMLInputFactory.IS_COALESCING, new Boolean(true));
227         javax.xml.stream.XMLStreamReader reader = inFactory.createXMLStreamReader(in);
228         final int startElement = javax.xml.stream.XMLStreamConstants.START_ELEMENT;
229
230         // Block identical for StAX or Javolution.
231         //
232         if (!reader.getEncoding().equals("UTF-8")) throw new Error("Wrong encoding");
233         while (reader.hasNext()) {
234             reader.next();
235             if (reader.isStartElement()) {
236                 if (reader.getLocalName().equals("Element_3")) {
237                     if (!reader.getNamespaceURI().equals("http://javolution.org/namespace/third"))
238                         throw new Error();
239                     if (reader.getAttributeValue("http://javolution.org/namespace/first", "Attribute_1") == null)
240                         throw new Error();
241                     if (reader.getAttributeValue("http://javolution.org/namespace/second", "Attribute_2") == null)
242                         throw new Error();
243                     if (reader.getAttributeValue("http://javolution.org/namespace/third", "Attribute_3") == null)
244                         throw new Error();
245                     reader.next();
246                     if (!reader.isEndElement()) throw new Error();
247                     reader.next();
248                     if (!reader.isCharacters() || (reader.getText().length() < 80))
249                          throw new Error(); // Coalescing not performed.
250                     reader.next();
251                     reader.require(startElement, "http://javolution.org/namespace/root", "TextOnlyElement");
252                     int textLength = reader.getElementText().length();
253                     if (textLength != 97) throw new Error("Length: " + textLength);
254                 }
255             }
256         }
257         reader.close();
258         //
259         // End Block.
260     }
261     private void staxCreateInput() throws Exception {
262         in = null;
263         staxStreamWriter(false);
264         System.gc();
265         in = new ByteArrayInputStream(out.toByteArray());
266         out.reset();
267     }
268     /**/

269     
270     /*@JVM-1.4+@
271     private void javoStreamReader() throws Exception {
272         javolution.xml.stream.XMLInputFactory inFactory = javolution.xml.stream.XMLInputFactory.newInstance();
273         inFactory.setProperty(javolution.xml.stream.XMLInputFactory.IS_COALESCING, new Boolean(true));
274         javolution.xml.stream.XMLStreamReader reader = inFactory.createXMLStreamReader(in);
275         final int startElement = javolution.xml.stream.XMLStreamConstants.START_ELEMENT;
276
277         // Block identical for StAX or Javolution.
278         //
279         if (!reader.getEncoding().equals("UTF-8")) throw new Error("Wrong encoding");
280         while (reader.hasNext()) {
281             reader.next();
282             if (reader.isStartElement()) {
283                 if (reader.getLocalName().equals("Element_3")) {
284                     if (!reader.getNamespaceURI().equals("http://javolution.org/namespace/third"))
285                         throw new Error();
286                     if (reader.getAttributeValue("http://javolution.org/namespace/first", "Attribute_1") == null)
287                         throw new Error();
288                     if (reader.getAttributeValue("http://javolution.org/namespace/second", "Attribute_2") == null)
289                         throw new Error();
290                     if (reader.getAttributeValue("http://javolution.org/namespace/third", "Attribute_3") == null)
291                         throw new Error();
292                     reader.next();
293                     if (!reader.isEndElement()) throw new Error();
294                     reader.next();
295                     if (!reader.isCharacters() || (reader.getText().length() < 80))
296                          throw new Error(reader.getText().toString()); // Coalescing not performed.
297                     reader.next();
298                     reader.require(startElement, "http://javolution.org/namespace/root", "TextOnlyElement");
299                     int textLength = reader.getElementText().length();
300                     if (textLength != 97) throw new Error("Length: " + textLength);
301                 }
302             }
303         }
304         reader.close();
305         //
306         // End Block.
307     }
308     private void javoCreateInput() throws Exception {
309          in = null;
310          javoStreamWriter(false);
311          System.gc();
312          in = new ByteArrayInputStream(out.toByteArray());
313          out.reset();
314     }
315     /**/

316
317 };
318
Popular Tags