1 22 package test.performance.serialize; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 29 import junit.framework.TestCase; 30 import test.performance.PerformanceSUITE; 31 32 37 public class SerializeTEST 38 extends TestCase 39 { 40 42 45 private Object obj; 46 47 50 private String desc; 51 52 54 57 public SerializeTEST(String s, Object obj, String desc) 58 { 59 super(s); 60 this.obj = obj; 61 this.desc = desc; 62 } 63 64 67 public void testIt() 68 { 69 System.out.println("\n" + desc); 70 System.out.println(PerformanceSUITE.SERIALIZE_ITERATION_COUNT + " Serializations, Repeat: x" + PerformanceSUITE.REPEAT_COUNT); 71 System.out.println("(this may take a while...)"); 72 73 long start = 0, end = 0; 74 float avg = 0l; 75 int size = 0; 76 77 try 78 { 79 80 Object result = null; 81 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 82 83 for (int testIterations = 0; testIterations < PerformanceSUITE.REPEAT_COUNT + 1; ++testIterations) 85 { 86 start = System.currentTimeMillis(); 87 for (int invocationIterations = 0; invocationIterations < PerformanceSUITE.SERIALIZE_ITERATION_COUNT; ++invocationIterations) 88 { 89 baos.reset(); 91 ObjectOutputStream oos = new ObjectOutputStream (baos); 92 oos.writeObject(obj); 93 94 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 96 ObjectInputStream ois = new ObjectInputStream (bais); 97 result = ois.readObject(); 98 } 99 end = System.currentTimeMillis(); 100 101 if (testIterations != 0) 102 { 103 long time = end - start; 104 System.out.print( time + " "); 105 avg += time; 106 } 107 } 108 109 System.out.print("\nAverage: " + (avg/PerformanceSUITE.REPEAT_COUNT)); 110 System.out.println(" Size: " + baos.toByteArray().length); 111 } 112 catch (Exception e) 113 { 114 fail(e.toString()); 115 } 116 } 117 } 118 | Popular Tags |