1 9 package javolution; 10 11 import java.io.ByteArrayInputStream ; 12 import java.io.ByteArrayOutputStream ; 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 41 final class PerfXML extends Javolution implements Runnable { 42 43 private static final int ITERATIONS = 1000; 45 private static final int OBJECT_SIZE = 1; 47 private static final int BUFFER_SIZE = 1400 * OBJECT_SIZE; 48 49 private ByteArrayOutputStream _Stream = new ByteArrayOutputStream ( 50 BUFFER_SIZE); 51 52 private ByteBuffer _Buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); 53 54 57 public void run() { 58 println("/////////////////////////////"); 59 println("// Package: javolution.xml //"); 60 println("/////////////////////////////"); 61 62 println(""); 63 println("-- Java(TM) Serialization --"); 64 benchmarkJavaSerialization(); 66 println(""); 67 println("-- XML Serialization (I/O Stream) --"); 68 benchmarkXmlIoSerialization(); 70 println(""); 71 println("-- XML Serialization (NIO ByteBuffer) --"); 72 benchmarkXmlNioSerialization(); 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 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 ObjectOutput oo = new ObjectOutputStream(_Stream); 95 Object data = newData(); 96 oo.writeObject(data); 97 oo.close(); 98 ByteArrayInputStream in = new ByteArrayInputStream (_Stream 99 .toByteArray()); 100 _Stream.reset(); 101 102 ObjectInput oi = new ObjectInputStream(in); 103 startTime(); 104 Object readObject = oi.readObject(); 105 oi.close(); 106 keepBestTime(1); 107 if (!data.equals(readObject)) 108 throw new Error ("SERIALIZATION ERROR"); 109 } 110 println(endTime()); 111 } catch (UnsupportedOperationException e) { 112 println("NOT SUPPORTED (J2SE 1.4+ build required)"); 113 } catch (Throwable e) { 114 throw new JavolutionError(e); 115 } 116 } 117 118 private void benchmarkXmlIoSerialization() { 119 XMLBinding binding = new XMLBinding(); 120 binding.setAlias(String .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 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 XMLObjectWriter ow = XMLObjectWriter.newInstance(_Stream); 139 ow.setBinding(binding); 140 Object data = newData(); 141 ow.write(data); 142 ow.close(); 143 ByteArrayInputStream in = new ByteArrayInputStream (_Stream 144 .toByteArray()); 145 _Stream.reset(); 146 147 XMLObjectReader or = XMLObjectReader.newInstance(in); 148 or.setBinding(binding); 149 startTime(); 150 Object readObject = or.read(); 151 or.close(); 152 keepBestTime(1); 153 if (!data.equals(readObject)) 154 throw new Error ("SERIALIZATION ERROR"); 155 } 156 157 println(endTime()); 158 159 } catch (UnsupportedOperationException e) { 160 println("NOT SUPPORTED (J2SE 1.4+ build required)"); 161 } catch (Throwable 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 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 XMLObjectWriter ow = XMLObjectWriter 186 .newInstance(new UTF8ByteBufferWriter() 187 .setOutput(_Buffer)); 188 Object 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 readObject = or.read(); 198 or.close(); 199 keepBestTime(1); 200 if (!data.equals(readObject)) 201 throw new Error ("SERIALIZATION ERROR"); 202 _Buffer.clear(); 203 } 204 println(endTime()); 205 } catch (UnsupportedOperationException e) { 206 println("NOT SUPPORTED (J2SE 1.4+ build required)"); 207 } catch (Throwable e) { 208 throw new JavolutionError(e); 209 } 210 } 211 212 private static Object 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 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 (MathLib.random(Integer.MIN_VALUE, 237 Integer.MAX_VALUE))); 238 fs.add(new Long (MathLib.random(Long.MIN_VALUE, Long.MAX_VALUE))); 239 fs.add("".getClass()); 240 v.add(fs); 241 } 242 return v; 243 } 244 } | Popular Tags |