001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.objectserver.managedobject;
005:
006: import com.sleepycat.bind.EntryBinding;
007: import com.sleepycat.bind.serial.SerialBinding;
008: import com.sleepycat.bind.serial.StoredClassCatalog;
009: import com.sleepycat.je.DatabaseEntry;
010: import com.tc.logging.NullTCLogger;
011: import com.tc.object.ObjectID;
012: import com.tc.object.tx.TransactionID;
013: import com.tc.objectserver.api.ObjectInstanceMonitor;
014: import com.tc.objectserver.core.api.ManagedObject;
015: import com.tc.objectserver.core.api.TestDNA;
016: import com.tc.objectserver.core.api.TestDNACursor;
017: import com.tc.objectserver.impl.ObjectInstanceMonitorImpl;
018: import com.tc.objectserver.persistence.api.ManagedObjectPersistor;
019: import com.tc.objectserver.persistence.api.PersistenceTransaction;
020: import com.tc.objectserver.persistence.sleepycat.CustomSerializationAdapterFactory;
021: import com.tc.objectserver.persistence.sleepycat.DBEnvironment;
022: import com.tc.objectserver.persistence.sleepycat.SerializationAdapter;
023: import com.tc.objectserver.persistence.sleepycat.SerializationAdapterFactory;
024: import com.tc.objectserver.persistence.sleepycat.SleepycatPersistor;
025: import com.tc.objectserver.persistence.sleepycat.SleepycatSerializationAdapterFactory;
026: import com.tc.test.TCTestCase;
027:
028: import java.io.File;
029: import java.io.IOException;
030: import java.util.ArrayList;
031: import java.util.Collection;
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Set;
036:
037: public class ManagedObjectSerializationPerformanceTest extends
038: TCTestCase {
039:
040: private StoredClassCatalog classCatalog;
041: private int fieldSetCount = 10;
042: private SleepycatPersistor sleepycatSerializerPersistor;
043: private SleepycatPersistor customSerializerPersistor;
044:
045: private boolean paranoid;
046: private DBEnvironment sleepycatSerializerEnvironment;
047: private DBEnvironment customSerializerEnvironment;
048: private Set environments = new HashSet();
049: private ObjectInstanceMonitor imo;
050:
051: public void setUp() throws Exception {
052:
053: paranoid = false;
054: imo = new ObjectInstanceMonitorImpl();
055:
056: // ManagedObjectChangeListenerProvider listenerProvider = new NullManagedObjectChangeListenerProvider();
057:
058: // set up sleepycat serializer persistor
059: sleepycatSerializerEnvironment = newEnvironment();
060: SerializationAdapterFactory saf = new SleepycatSerializationAdapterFactory();
061: sleepycatSerializerPersistor = new SleepycatPersistor(
062: new NullTCLogger(), sleepycatSerializerEnvironment, saf);
063: classCatalog = (StoredClassCatalog) sleepycatSerializerEnvironment
064: .getClassCatalogWrapper().getClassCatalog();
065:
066: ManagedObjectStateFactory.disableSingleton(true);
067: ManagedObjectStateFactory.createInstance(
068: new NullManagedObjectChangeListenerProvider(),
069: sleepycatSerializerPersistor);
070:
071: // set up custom serializer persistor
072: customSerializerEnvironment = newEnvironment();
073:
074: CustomSerializationAdapterFactory customSaf = new CustomSerializationAdapterFactory();
075: customSerializerPersistor = new SleepycatPersistor(
076: new NullTCLogger(), customSerializerEnvironment,
077: customSaf);
078: }
079:
080: public void tearDown() throws Exception {
081: for (Iterator i = environments.iterator(); i.hasNext();) {
082: ((DBEnvironment) i.next()).close();
083: }
084: }
085:
086: public void testStoreAndLoad() throws Exception {
087: ManagedObjectImpl customSerializerMo = newManagedObject();
088: ManagedObjectImpl sleepycatSerializerMo = newManagedObject();
089:
090: int iterations = 100;
091: int cycles = 5;
092:
093: List sleepycatStoreStats = new ArrayList(cycles);
094: List sleepycatLoadStats = new ArrayList(cycles);
095: List tcStoreStats = new ArrayList(cycles);
096: List tcLoadStats = new ArrayList(cycles);
097: for (int i = 0; i < cycles; i++) {
098: Stats scStoreStat = new Stats(), scLoadStat = new Stats();
099: sleepycatStoreStats.add(scStoreStat);
100: sleepycatLoadStats.add(scLoadStat);
101:
102: Stats tcStoreStat = new Stats(), tcLoadStat = new Stats();
103: tcStoreStats.add(tcStoreStat);
104: tcLoadStats.add(tcLoadStat);
105:
106: storeAndLoad(scStoreStat, scLoadStat, iterations,
107: sleepycatSerializerMo, sleepycatSerializerPersistor);
108: storeAndLoad(tcStoreStat, tcLoadStat, iterations,
109: customSerializerMo, customSerializerPersistor);
110: }
111: System.err
112: .println("===============================================================");
113: System.err.println("Paranoid: " + paranoid + ", field sets: "
114: + fieldSetCount + ", cycles: " + cycles
115: + ", iterations per cycle: " + iterations);
116: System.err.println("TC store : " + summary(tcStoreStats)
117: + "; " + tcStoreStats);
118: System.err.println("Sleepycat store: "
119: + summary(sleepycatStoreStats) + "; "
120: + sleepycatStoreStats);
121: System.err.println("TC load : " + summary(tcLoadStats)
122: + "; " + tcLoadStats);
123: System.err.println("Sleepycat load : "
124: + summary(sleepycatLoadStats) + "; "
125: + sleepycatLoadStats);
126:
127: }
128:
129: private void storeAndLoad(Stats storeStat, Stats loadStat,
130: int iterations, ManagedObjectImpl mo,
131: SleepycatPersistor persistor) {
132: ManagedObjectPersistor mop = persistor
133: .getManagedObjectPersistor();
134: ObjectID objectID = mo.getID();
135: long now = System.currentTimeMillis();
136: PersistenceTransaction ptx = persistor
137: .getPersistenceTransactionProvider().newTransaction();
138: for (int i = 0; i < iterations; i++) {
139: mo.setIsDirty(true);
140: mop.saveObject(ptx, mo);
141: }
142: ptx.commit();
143: storeStat.time += System.currentTimeMillis() - now;
144:
145: ManagedObject test = null;
146: now = System.currentTimeMillis();
147: for (int i = 0; i < iterations; i++) {
148: test = mop.loadObjectByID(objectID);
149: }
150: loadStat.time += System.currentTimeMillis() - now;
151: assertTrue(mo.isEqual(test));
152: }
153:
154: public void testSerialization() throws Exception {
155: ManagedObjectImpl customSerializerMo = newManagedObject();
156: ManagedObjectImpl sleepycatSerializerMo = newManagedObject();
157:
158: EntryBinding binding = new SerialBinding(classCatalog,
159: sleepycatSerializerMo.getClass());
160:
161: int iterations = 100;
162: int cycles = 5;
163:
164: DatabaseEntry entry = new DatabaseEntry();
165: List sleepycatSstats = new ArrayList(cycles);
166: List sleepycatDsstats = new ArrayList(cycles);
167: List tcSstats = new ArrayList(cycles);
168: List tcDsstats = new ArrayList(cycles);
169:
170: for (int i = 0; i < cycles; i++) {
171: Stats ssstat = new Stats(), sdsstat = new Stats();
172: sleepycatSstats.add(ssstat);
173: sleepycatDsstats.add(sdsstat);
174:
175: Stats tcsstat = new Stats(), tcdsstat = new Stats();
176: tcSstats.add(tcsstat);
177: tcDsstats.add(tcdsstat);
178:
179: sleepycatSerialize(ssstat, sdsstat, iterations,
180: sleepycatSerializerMo, binding, entry);
181: tcSerialize(tcsstat, tcdsstat, iterations,
182: customSerializerMo);
183: }
184: System.err
185: .println("===============================================================");
186: System.err.println("Paranoid: " + paranoid + ", field sets: "
187: + fieldSetCount + ", cycles: " + cycles
188: + ", serializations per cycle: " + iterations);
189: System.err.println("TC serialization stats : "
190: + summary(tcSstats) + "; " + tcSstats);
191: System.err.println("Sleepycat serialization stats : "
192: + summary(sleepycatSstats) + "; " + sleepycatSstats);
193:
194: System.err.println("TC deserialization stats : "
195: + summary(tcDsstats) + "; " + tcDsstats);
196: System.err.println("Sleepycat deserialization stats: "
197: + summary(sleepycatDsstats) + "; " + sleepycatDsstats);
198: }
199:
200: private ManagedObjectImpl newManagedObject() {
201: ManagedObjectImpl mo = new ManagedObjectImpl(new ObjectID(1));
202: TestDNA dna = newDNA();
203: mo.apply(dna, new TransactionID(1), new BackReferences(), imo,
204: false);
205: return mo;
206: }
207:
208: private void sleepycatSerialize(Stats serializeStats,
209: Stats deserializeStats, int iterations,
210: ManagedObjectImpl mo, EntryBinding binding,
211: DatabaseEntry entry) {
212: long now = System.currentTimeMillis();
213: for (int i = 0; i < iterations; i++) {
214: binding.objectToEntry(mo, entry);
215: }
216: serializeStats.time += System.currentTimeMillis() - now;
217: serializeStats.size += entry.getData().length;
218:
219: ManagedObject test = null;
220: now = System.currentTimeMillis();
221: for (int i = 0; i < iterations; i++) {
222: test = (ManagedObject) binding.entryToObject(entry);
223: }
224: deserializeStats.time += System.currentTimeMillis() - now;
225: assertTrue(mo.isEqual(test));
226: }
227:
228: private void tcSerialize(Stats serializeStats,
229: Stats deserializeStats, int iterations, ManagedObjectImpl mo)
230: throws IOException, ClassNotFoundException {
231: long now = System.currentTimeMillis();
232: SerializationAdapter serializer = customSerializerPersistor
233: .getSerializationAdapter();
234: DatabaseEntry entry = new DatabaseEntry();
235: for (int i = 0; i < iterations; i++) {
236: serializer.serializeManagedObject(entry, mo);
237: }
238: serializeStats.time += System.currentTimeMillis() - now;
239: serializeStats.size += entry.getData().length;
240:
241: ManagedObject test = null;
242: now = System.currentTimeMillis();
243: for (int i = 0; i < iterations; i++) {
244: test = serializer.deserializeManagedObject(entry);
245: }
246: deserializeStats.time += System.currentTimeMillis() - now;
247: assertTrue(mo.isEqual(test));
248: }
249:
250: private TestDNA newDNA() {
251: TestDNACursor cursor = new TestDNACursor();
252: for (int i = 0; i < fieldSetCount; i++) {
253: cursor.addPhysicalAction("refField" + i, new ObjectID(1));
254: cursor.addPhysicalAction("booleanField" + i, new Boolean(
255: true));
256: cursor.addPhysicalAction("byteField" + i,
257: new Byte((byte) 1));
258: cursor.addPhysicalAction("characterField" + i,
259: new Character('c'));
260: cursor.addPhysicalAction("doubleField" + i, new Double(
261: 100.001d));
262: cursor.addPhysicalAction("floatField" + i, new Float(
263: 100.001f));
264: cursor.addPhysicalAction("intField" + i, new Integer(100));
265: cursor.addPhysicalAction("longField" + i, new Long(100));
266: cursor.addPhysicalAction("shortField" + i, new Short(
267: (short) 1));
268: cursor.addPhysicalAction("stringField" + i,
269: "This is a nice string to add" + i);
270: }
271: TestDNA dna = new TestDNA(cursor);
272: return dna;
273: }
274:
275: private DBEnvironment newEnvironment() throws Exception {
276: File envHome;
277: int counter = 0;
278: do {
279: envHome = new File(getTempDirectory(),
280: "database-environment-home" + (++counter));
281: } while (envHome.exists());
282: envHome.mkdir();
283: assertTrue(envHome.exists());
284: assertTrue(envHome.isDirectory());
285: DBEnvironment rv = new DBEnvironment(paranoid, envHome);
286: environments.add(rv);
287: return rv;
288: }
289:
290: private Summary summary(Collection stats) {
291: Summary avg = new Summary();
292: Stats stat = null;
293: for (Iterator i = stats.iterator(); i.hasNext();) {
294: stat = (Stats) i.next();
295: avg.sum += stat.time;
296: }
297: avg.average = avg.sum / stats.size();
298: avg.size = stat.size;
299: return avg;
300: }
301:
302: private static final class Summary {
303: public long sum;
304: public double average;
305: public long size;
306:
307: public String toString() {
308: return "avg: " + average + ", size: " + size;
309: }
310: }
311:
312: private static final class Stats {
313: public long time;
314: public long size;
315:
316: public String toString() {
317: return "time: " + time;
318: }
319: }
320: }
|