1 7 package org.jboss.cache.marshall; 8 9 import junit.framework.TestCase; 10 11 import java.io.ByteArrayOutputStream ; 12 import java.io.File ; 13 import java.io.FileOutputStream ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.util.Random ; 17 18 23 public class JBossSerializationTest extends TestCase 24 { 25 26 28 public static boolean SAVE_FILE = false; 29 public static boolean USE_STRING = false; 30 31 static class DummyObject implements java.io.Serializable 32 { 33 private static final long serialVersionUID = -1374365296408936578L; 34 String stringName; 35 36 public DummyObject() 37 { 38 stringName = "Hello world, blah blah blah." + (new Random ().nextInt()); 39 } 40 41 public boolean equals(Object obj) 42 { 43 return stringName.equals(((DummyObject) obj).stringName); 44 } 45 } 46 47 private static void saveBytes(File fileName, byte[] bytes) throws Exception 48 { 49 if (SAVE_FILE) 50 { 51 FileOutputStream fileOut = new FileOutputStream (fileName); 52 fileOut.write(bytes); 53 fileOut.close(); 54 } 55 } 56 57 public void testSpeedSizeAndDefaults() throws Exception 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 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 out = new ByteArrayOutputStream (); 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 objectOut = ObjectSerializationFactory.createObjectOutputStream(out); 90 objectOut.writeObject(dummyObject); 91 objectOut.close(); 92 93 ObjectInputStream objectInput = ObjectSerializationFactory.createObjectInputStream(out.toByteArray()); 94 Object 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 ("/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 objectOut = ObjectSerializationFactory.createObjectOutputStream(out); 119 objectOut.writeObject(dummyObject); 120 objectOut.close(); 121 122 ObjectInputStream objectInput = ObjectSerializationFactory.createObjectInputStream(out.toByteArray()); 123 Object 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 ("/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 |