KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > JBossSerializationTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.marshall;
8
9 import junit.framework.TestCase;
10
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.util.Random JavaDoc;
17
18 /**
19  * Tests the performance of JBoss Serialization
20  *
21  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
22  */

23 public class JBossSerializationTest extends TestCase
24 {
25
26    // you can change these variables to change behavior of this testsuite, for debug purposes:
27

28    public static boolean SAVE_FILE = false;
29    public static boolean USE_STRING = false;
30
31    static class DummyObject implements java.io.Serializable JavaDoc
32    {
33       private static final long serialVersionUID = -1374365296408936578L;
34       String JavaDoc stringName;
35
36       public DummyObject()
37       {
38          stringName = "Hello world, blah blah blah." + (new Random JavaDoc().nextInt());
39       }
40
41       public boolean equals(Object JavaDoc obj)
42       {
43          return stringName.equals(((DummyObject) obj).stringName);
44       }
45    }
46
47    private static void saveBytes(File JavaDoc fileName, byte[] bytes) throws Exception JavaDoc
48    {
49       if (SAVE_FILE)
50       {
51          FileOutputStream JavaDoc fileOut = new FileOutputStream JavaDoc(fileName);
52          fileOut.write(bytes);
53          fileOut.close();
54       }
55    }
56
57    public void testSpeedSizeAndDefaults() throws Exception JavaDoc
58    {
59
60       assertTrue("JBoss Serialization should be enabled by default", ObjectSerializationFactory.useJBossSerialization);
61       assertEquals("Should be using the JBossObjectStreamFactory", JBossObjectStreamFactory.class, ObjectSerializationFactory.factory.getClass());
62       Object JavaDoc dummyObject = null;
63
64       if (USE_STRING)
65       {
66          dummyObject = "Hello world, blah blah blah.";
67       }
68       else
69       {
70          dummyObject = new DummyObject();
71       }
72
73       ObjectSerializationFactory.useJBossSerialization = false;
74       ObjectSerializationFactory.factory = new JavaObjectStreamFactory();
75       assertTrue("Should be false", !ObjectSerializationFactory.useJBossSerialization);
76
77
78       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
79
80       long start = 0;
81
82       for (int i = 0; i < 50000; i++)
83       {
84          out.reset();
85          if (i == 1000)
86          {
87             start = System.currentTimeMillis();
88          }
89          ObjectOutputStream JavaDoc objectOut = ObjectSerializationFactory.createObjectOutputStream(out);
90          objectOut.writeObject(dummyObject);
91          objectOut.close();
92
93          ObjectInputStream JavaDoc objectInput = ObjectSerializationFactory.createObjectInputStream(out.toByteArray());
94          Object JavaDoc newObject = objectInput.readObject();
95
96          assertEquals(dummyObject, newObject);
97          assertNotSame(dummyObject, newObject);
98       }
99
100
101       long javaSerializationTime = System.currentTimeMillis() - start;
102       int javaSerializationSize = out.toByteArray().length;
103
104       saveBytes(new File JavaDoc("/tmp/fileJava.bin"), out.toByteArray());
105
106       ObjectSerializationFactory.useJBossSerialization = true;
107       ObjectSerializationFactory.factory = new JBossObjectStreamFactory();
108       assertTrue("Should be false", ObjectSerializationFactory.useJBossSerialization);
109
110       start = 0;
111       for (int i = 0; i < 50000; i++)
112       {
113          out.reset();
114          if (i == 1000)
115          {
116             start = System.currentTimeMillis();
117          }
118          ObjectOutputStream JavaDoc objectOut = ObjectSerializationFactory.createObjectOutputStream(out);
119          objectOut.writeObject(dummyObject);
120          objectOut.close();
121
122          ObjectInputStream JavaDoc objectInput = ObjectSerializationFactory.createObjectInputStream(out.toByteArray());
123          Object JavaDoc newObject = objectInput.readObject();
124
125          assertEquals(dummyObject, newObject);
126          assertNotSame(dummyObject, newObject);
127       }
128       long jbossSerializationTime = System.currentTimeMillis() - start;
129       int jbossSerializationSize = out.toByteArray().length;
130
131       System.out.println("FinishJBoss:" + System.currentTimeMillis());
132       saveBytes(new File JavaDoc("/tmp/fileJBoss.bin"), out.toByteArray());
133
134       System.out.println("JBossSerialization Time:" + jbossSerializationTime);
135       System.out.println("JavaSerialization Time:" + javaSerializationTime);
136
137       System.out.println("JBossSerialization Size:" + jbossSerializationSize);
138       System.out.println("JavaSerialization Size:" + javaSerializationSize);
139
140       assertTrue("JBoss Serialization should be FASTER than Java Serialization. Java: " + javaSerializationTime + "; jboss: " + jbossSerializationTime, jbossSerializationTime < javaSerializationTime);
141       assertTrue("JBoss Serialization output should be SMALLER than Java Serialization. Java: " + javaSerializationSize + "; jboss: " + jbossSerializationSize, jbossSerializationSize < javaSerializationSize);
142    }
143
144 }
145
Popular Tags