KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > SerializationSpeedTester


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.io.ObjectOutputStream JavaDoc;
9 import java.io.Serializable JavaDoc;
10 import java.lang.reflect.Array JavaDoc;
11 import java.lang.reflect.Field JavaDoc;
12 import java.lang.reflect.Modifier JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.IdentityHashMap JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * This is a nifty little class that lets you 'profile' the serialization speed of a given object: it will serialize the
22  * object in question, and each class underneath that object, printing the speed of each serialization at the end.
23  */

24 public class SerializationSpeedTester {
25
26   public static class SpeedEntry implements Comparable JavaDoc {
27     private final Class JavaDoc theClass;
28
29     private final double millisecondsPerSerialization;
30
31     public SpeedEntry(Class JavaDoc theClass, double millisecondsPerSerialization) {
32       Assert.assertNotNull(theClass);
33
34       this.theClass = theClass;
35       this.millisecondsPerSerialization = millisecondsPerSerialization;
36     }
37
38     public String JavaDoc toString() {
39       return "Class " + this.theClass.getName() + ": " + this.millisecondsPerSerialization
40              + " ms per serialization, on average.";
41     }
42
43     public int compareTo(Object JavaDoc 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 JavaDoc rootObject) {
52     return testSpeedOf(rootObject, "com.tc");
53   }
54
55   public SpeedEntry[] testSpeedOf(Serializable JavaDoc rootObject, String JavaDoc requiredPrefix) {
56     Assert.assertNotNull(rootObject);
57
58     Map JavaDoc classMap = new HashMap JavaDoc();
59     testSpeedOf(rootObject, classMap, new IdentityHashMap JavaDoc(), 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 JavaDoc object, Map JavaDoc classMap, Map JavaDoc processedObjects, String JavaDoc requiredPrefix, String JavaDoc fromWhere) {
67     Assert.assertNotNull(object);
68
69     if (object.getClass().isPrimitive()) return;
70     // if (object.getClass().getName().equals("sun.reflect.UnsafeStaticObjectFieldAccessorImpl")) return;
71

72     if (processedObjects.containsKey(object)) return;
73     else {
74       processedObjects.put(object, new Integer JavaDoc(1));
75     }
76
77     Class JavaDoc 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 JavaDoc 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 JavaDoc.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 JavaDoc[] 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 JavaDoc 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 JavaDoc e) {
108         System.err.println("ERROR ON: " + object + " OF CLASS " + object.getClass());
109         e.printStackTrace();
110       }
111     }
112   }
113
114   private Field JavaDoc[] getFields(Class JavaDoc objectClass) {
115     List JavaDoc list = new ArrayList JavaDoc();
116     addFields(objectClass, list);
117     return (Field JavaDoc[]) list.toArray(new Field JavaDoc[list.size()]);
118   }
119
120   private void addFields(Class JavaDoc objectClass, List JavaDoc list) {
121     Field JavaDoc[] fields = objectClass.getDeclaredFields();
122     list.addAll(Arrays.asList(fields));
123     Class JavaDoc 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 JavaDoc object) {
130     try {
131       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
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 JavaDoc oos = new ObjectOutputStream JavaDoc(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       // System.err.println("Class " + object.getClass() + ": " + out);
146
return out;
147     } catch (IOException JavaDoc 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 JavaDoc[] args) throws Exception JavaDoc {
154     // needs a good main
155
}
156
157 }
158
Popular Tags