package com.bubble.serializer.benchmark;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import com.bubble.serializer.DeserializationContext;
import com.bubble.serializer.LightDeserializationContext;
import com.bubble.serializer.LightSerializationContext;
import com.bubble.serializer.SerializationContext;
public abstract class AbstractSerializeTest {
private int loops;
private int repeats;
private String filePrefix = "testFile_"+System.currentTimeMillis()+"_";
private Object testObject;
public AbstractSerializeTest(int loops, int repeats) {
this.loops = loops;
this.repeats = repeats;
}
protected void setUp() throws Exception {
testObject = createTestObject();
}
protected abstract Object createTestObject();
protected void tearDown() throws Exception {
for (int i = 0; i < loops; i++) {
new File(filePrefix).delete();
}
}
public long testJavaSerialize() throws Exception {
FileOutputStream fs = new FileOutputStream(filePrefix);
ObjectOutputStream os = new ObjectOutputStream(fs);
long start = System.currentTimeMillis();
for (int i = 0; i < loops; i++) {
testObject = createTestObject();
os.writeObject(testObject);
}
long total = System.currentTimeMillis()- start;
fs.close();
return total;
}
public long testJavaDeserialize() throws Exception {
FileOutputStream fs = new FileOutputStream(filePrefix);
ObjectOutputStream os = new ObjectOutputStream(fs);
Object[] objs = new Object[loops];
for (int i = 0; i < loops; i++) {
objs[i] = createTestObject();
os.writeObject(objs[i]);
}
fs.close();
FileInputStream fi = new FileInputStream(filePrefix);
ObjectInputStream is = new ObjectInputStream(fi);
long start = System.currentTimeMillis();
for (int i = 0; i < loops; i++) {
is.readObject();
}
long total = System.currentTimeMillis()- start;
fi.close();
return total;
}
public long testLightSerialize() throws Exception {
FileChannel channel = new FileOutputStream(filePrefix).getChannel();
LightSerializationContext sc = new LightSerializationContext();
ByteBuffer buffer = ByteBuffer.allocateDirect(102400);
long start = System.currentTimeMillis();
for (int i = 0; i < loops; i++) {
testObject = createTestObject();
sc.serialize(testObject, buffer);
buffer.flip();
channel.write(buffer);
buffer.clear();
}
long total = System.currentTimeMillis()- start;
channel.close();
return total;
}
public long testBubbleSerialize() throws Exception {
FileChannel channel = new FileOutputStream(filePrefix).getChannel();
SerializationContext sc = new SerializationContext();
ByteBuffer buffer = ByteBuffer.allocateDirect(102400);
long start = System.currentTimeMillis();
for (int i = 0; i < loops; i++) {
testObject = createTestObject();
sc.serialize(testObject, buffer);
buffer.flip();
channel.write(buffer);
buffer.clear();
}
long total = System.currentTimeMillis()- start;
channel.close();
return total;
}
public long testBubbleDeserialize() throws Exception {
FileChannel channel = new FileOutputStream(filePrefix).getChannel();
SerializationContext sc = new SerializationContext();
ByteBuffer buffer = ByteBuffer.allocateDirect(102400);
for (int i = 0; i < loops; i++) {
testObject = createTestObject();
sc.serialize(testObject, buffer);
buffer.flip();
channel.write(buffer);
buffer.clear();
}
channel.close();
channel = new FileInputStream(filePrefix).getChannel();
DeserializationContext dc = new DeserializationContext();
long start = System.currentTimeMillis();
for (int i = 0; i < loops; i++) {
channel.read(buffer);
buffer.flip();
dc.deserialize(buffer);
buffer.compact();
}
long total = System.currentTimeMillis()- start;
channel.close();
return total;
}
public long testLightDeserialize() throws Exception {
FileChannel channel = new FileOutputStream(filePrefix).getChannel();
SerializationContext sc = new SerializationContext();
ByteBuffer buffer = ByteBuffer.allocateDirect(102400);
for (int i = 0; i < loops; i++) {
testObject = createTestObject();
sc.serialize(testObject, buffer);
buffer.flip();
channel.write(buffer);
buffer.clear();
}
channel.close();
channel = new FileInputStream(filePrefix).getChannel();
LightDeserializationContext dc = new LightDeserializationContext();
long start = System.currentTimeMillis();
for (int i = 0; i < loops; i++) {
channel.read(buffer);
buffer.flip();
dc.deserialize(buffer);
buffer.compact();
}
long total = System.currentTimeMillis()- start;
channel.close();
return total;
}
public void runTest() throws Exception {
Object obj = createTestObject();
System.out.println("Testing with "+obj.getClass());
// warm up
for(int i = 0; i < 20; i++) {
setUp();
testJavaSerialize();
tearDown();
}
for(int i = 0; i < 20; i++) {
setUp();
testBubbleSerialize();
tearDown();
}
for(int i = 0; i < 20; i++) {
setUp();
testLightSerialize();
tearDown();
}
for(int i = 0; i < 20; i++) {
setUp();
testJavaDeserialize();
tearDown();
}
for(int i = 0; i < 20; i++) {
setUp();
testBubbleDeserialize();
tearDown();
}
for(int i = 0; i < 20; i++) {
setUp();
testLightDeserialize();
tearDown();
}
long totalBubbleTime = 0;
long totalLightTime = 0;
long totalJavaTime = 0;
for(int i = 0; i < repeats; i++) {
setUp();
totalLightTime += testLightSerialize();
tearDown();
}
for(int i = 0; i < repeats; i++) {
setUp();
totalBubbleTime += testBubbleSerialize();
tearDown();
}
for(int i = 0; i < repeats; i++) {
setUp();
totalJavaTime += testJavaSerialize();
tearDown();
}
long totalBubbleDeserializeTime = 0;
long totalLightDeserializeTime = 0;
long totalJavaDeserializeTime = 0;
for(int i = 0; i < repeats; i++) {
setUp();
totalJavaDeserializeTime += testJavaDeserialize();
tearDown();
}
for(int i = 0; i < repeats; i++) {
setUp();
totalBubbleDeserializeTime += testBubbleDeserialize();
tearDown();
}
for(int i = 0; i < repeats; i++) {
setUp();
totalLightDeserializeTime += testLightDeserialize();
tearDown();
}
System.out.println("Number of runs: "+repeats);
System.out.println("Number of objects per run: "+loops);
float frac = repeats*loops;
System.out.println("Total Java serialize time: "+totalJavaTime+"ms ("+(int)frac+" objects)");
System.out.println("Total Bubble serialize time: "+totalBubbleTime+"ms ("+(int)frac+" objects)");
System.out.println("Total Light serialize time: "+totalLightTime+"ms ("+(int)frac+" objects)");
System.out.println("Avg Java serialize time: "+(totalJavaTime/frac)+"ms/object");
System.out.println("Avg Bubble serialize time: "+(totalBubbleTime/frac)+"ms/object");
System.out.println("Avg Light serialize time: "+(totalLightTime/frac)+"ms/object");
System.out.println("");
System.out.println("Total Java deserialize time: "+totalJavaDeserializeTime+"ms ("+(int)frac+" objects)");
System.out.println("Total Bubble deserialize time: "+totalBubbleDeserializeTime+"ms ("+(int)frac+" objects)");
System.out.println("Total Light deserialize time: "+totalLightDeserializeTime+"ms ("+(int)frac+" objects)");
System.out.println("Avg Java deserialize time: "+(totalJavaDeserializeTime/frac)+"ms/object");
System.out.println("Avg Bubble deserialize time: "+(totalBubbleDeserializeTime/frac)+"ms/object");
System.out.println("Avg Light deserialize time: "+(totalLightDeserializeTime/frac)+"ms/object");
System.out.println("---");
}
}