1 4 package com.tc.util; 5 6 import java.io.ByteArrayOutputStream ; 7 import java.io.IOException ; 8 import java.io.ObjectOutputStream ; 9 import java.io.Serializable ; 10 import java.lang.reflect.Array ; 11 import java.lang.reflect.Field ; 12 import java.lang.reflect.Modifier ; 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.IdentityHashMap ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 24 public class SerializationSpeedTester { 25 26 public static class SpeedEntry implements Comparable { 27 private final Class theClass; 28 29 private final double millisecondsPerSerialization; 30 31 public SpeedEntry(Class theClass, double millisecondsPerSerialization) { 32 Assert.assertNotNull(theClass); 33 34 this.theClass = theClass; 35 this.millisecondsPerSerialization = millisecondsPerSerialization; 36 } 37 38 public String toString() { 39 return "Class " + this.theClass.getName() + ": " + this.millisecondsPerSerialization 40 + " ms per serialization, on average."; 41 } 42 43 public int compareTo(Object that) { 44 double other = ((SpeedEntry) that).millisecondsPerSerialization; 45 if (this.millisecondsPerSerialization > other) return 1; 46 else if (this.millisecondsPerSerialization == other) return 0; 47 else return -1; 48 } 49 } 50 51 public SpeedEntry[] testSpeedOf(Serializable rootObject) { 52 return testSpeedOf(rootObject, "com.tc"); 53 } 54 55 public SpeedEntry[] testSpeedOf(Serializable rootObject, String requiredPrefix) { 56 Assert.assertNotNull(rootObject); 57 58 Map classMap = new HashMap (); 59 testSpeedOf(rootObject, classMap, new IdentityHashMap (), requiredPrefix, "root"); 60 61 SpeedEntry[] entries = (SpeedEntry[]) classMap.values().toArray(new SpeedEntry[classMap.size()]); 62 Arrays.sort(entries); 63 return entries; 64 } 65 66 private void testSpeedOf(Object object, Map classMap, Map processedObjects, String requiredPrefix, String fromWhere) { 67 Assert.assertNotNull(object); 68 69 if (object.getClass().isPrimitive()) return; 70 72 if (processedObjects.containsKey(object)) return; 73 else { 74 processedObjects.put(object, new Integer (1)); 75 } 76 77 Class objectClass = object.getClass(); 78 79 if (objectClass.isArray()) { 80 if (!objectClass.getComponentType().isPrimitive()) { 81 int length = Array.getLength(object); 82 for (int i = 0; i < length; ++i) { 83 Object value = Array.get(object, i); 84 if (value != null && (!value.getClass().isPrimitive())) { 85 testSpeedOf(value, classMap, processedObjects, requiredPrefix, fromWhere + "[" + i + "]"); 86 } 87 } 88 } 89 } else { 90 if (Serializable .class.isAssignableFrom(object.getClass()) 91 && object.getClass().getName().startsWith(requiredPrefix) && (!classMap.containsKey(object.getClass()))) { 92 classMap.put(objectClass, new SpeedEntry(objectClass, getSpeedOf(object))); 93 } 94 95 try { 96 Field [] fields = getFields(objectClass); 97 for (int i = 0; i < fields.length; ++i) { 98 if (fields[i].getType().isPrimitive()) continue; 99 if (Modifier.isStatic(fields[i].getModifiers())) continue; 100 if (!fields[i].isAccessible()) fields[i].setAccessible(true); 101 102 Object value = fields[i].get(object); 103 if (value != null && (!value.getClass().isPrimitive())) { 104 testSpeedOf(value, classMap, processedObjects, requiredPrefix, fields[i].toString()); 105 } 106 } 107 } catch (Exception e) { 108 System.err.println("ERROR ON: " + object + " OF CLASS " + object.getClass()); 109 e.printStackTrace(); 110 } 111 } 112 } 113 114 private Field [] getFields(Class objectClass) { 115 List list = new ArrayList (); 116 addFields(objectClass, list); 117 return (Field []) list.toArray(new Field [list.size()]); 118 } 119 120 private void addFields(Class objectClass, List list) { 121 Field [] fields = objectClass.getDeclaredFields(); 122 list.addAll(Arrays.asList(fields)); 123 Class superclass = objectClass.getSuperclass(); 124 if (superclass != null) addFields(superclass, list); 125 } 126 127 private static final int SERIALIZATION_COUNT = 1; 128 129 private double getSpeedOf(Object object) { 130 try { 131 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 132 133 System.err.println("Serializing object of class " + object.getClass() + "..."); 134 long startTime = System.currentTimeMillis(); 135 for (int i = 0; i < SERIALIZATION_COUNT; ++i) { 136 ObjectOutputStream oos = new ObjectOutputStream (baos); 137 oos.writeObject(object); 138 oos.close(); 139 } 140 long endTime = System.currentTimeMillis(); 141 baos.reset(); 142 System.err.println("Took " + (endTime - startTime) + " ms for " + object.getClass()); 143 144 double out = (((double) (endTime - startTime)) / (double) SERIALIZATION_COUNT); 145 return out; 147 } catch (IOException ioe) { 148 System.err.println("Can't get speed of: " + object.getClass().getName()); 149 return -1.0; 150 } 151 } 152 153 public static void main(String [] args) throws Exception { 154 } 156 157 } 158 | Popular Tags |