Created
November 16, 2012 13:00
-
-
Save muga/4087169 to your computer and use it in GitHub Desktop.
Sample Program for Performance Measurement of BeansTemplateBuilder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import org.msgpack.MessagePack; | |
import org.msgpack.annotation.Beans; | |
import org.msgpack.packer.Packer; | |
import org.msgpack.template.Template; | |
import org.msgpack.template.TemplateRegistry; | |
import org.msgpack.template.builder.ReflectionBeansTemplateBuilder; | |
import org.msgpack.unpacker.Unpacker; | |
public class BeanPerformanceTest { | |
@Beans | |
public static class TestCase { | |
private int f0; | |
private String f1; | |
public TestCase() {} | |
public void setF0(int f0) { this.f0 = f0; } | |
public int getF0() { return f0; } | |
public void setF1(String f1) { this.f1 = f1; } | |
public String getF1() { return f1; } | |
} | |
private int loopCount = 20000; | |
private MessagePack msgpack; | |
public BeanPerformanceTest() { | |
msgpack = new MessagePack(); | |
} | |
public void testCreationTime() throws Exception { | |
long t = System.currentTimeMillis(); | |
TemplateRegistry registry = new TemplateRegistry(null); | |
ReflectionBeansTemplateBuilder builder = new ReflectionBeansTemplateBuilder(registry); | |
for (int i = 0; i < loopCount; i++) { | |
try { | |
builder.buildTemplate(TestCase.class); | |
} finally { | |
registry.unregister(TestCase.class); | |
} | |
} | |
t = System.currentTimeMillis() - t; | |
System.out.println("elapsed time of template creation: " + t); | |
} | |
public void testSerializationTime() throws Exception { | |
TemplateRegistry registry = new TemplateRegistry(null); | |
ReflectionBeansTemplateBuilder builder = new ReflectionBeansTemplateBuilder(registry); | |
Template<TestCase> tmpl = builder.buildTemplate(TestCase.class); | |
TestCase src = new TestCase(); | |
src.f0 = 10; | |
src.f1 = "muga"; | |
long t = System.currentTimeMillis(); | |
for (int i = 0; i < loopCount; i++) { | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
Packer packer = msgpack.createPacker(out); | |
tmpl.write(packer, src); | |
byte[] bytes = out.toByteArray(); | |
} | |
t = System.currentTimeMillis() - t; | |
System.out.println("elapsed time of serialization: " + t); | |
} | |
public void testDeserializationTime() throws Exception { | |
TemplateRegistry registry = new TemplateRegistry(null); | |
ReflectionBeansTemplateBuilder builder = new ReflectionBeansTemplateBuilder(registry); | |
Template<TestCase> tmpl = builder.buildTemplate(TestCase.class); | |
TestCase src = new TestCase(); | |
src.f0 = 10; | |
src.f1 = "muga"; | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
Packer packer = msgpack.createPacker(out); | |
tmpl.write(packer, src); | |
byte[] bytes = out.toByteArray(); | |
long t = System.currentTimeMillis(); | |
for (int i = 0; i < loopCount; i++) { | |
ByteArrayInputStream in = new ByteArrayInputStream(bytes); | |
Unpacker unpacker = msgpack.createUnpacker(in); | |
TestCase dst = tmpl.read(unpacker, null); | |
} | |
t = System.currentTimeMillis() - t; | |
System.out.println("elapsed time of deserialization: " + t); | |
} | |
public static void main(String[] args) throws Exception { | |
BeanPerformanceTest test = new BeanPerformanceTest(); | |
System.gc(); | |
test.testCreationTime(); | |
System.gc(); | |
test.testSerializationTime(); | |
System.gc(); | |
test.testDeserializationTime(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment