Permalink
Browse files

add comparison test for collection serialization

  • Loading branch information...
1 parent 66ef6af commit 51c97fec6db0c1acb19ea7999cca957326f140a8 Soo Kim committed Apr 4, 2012
Showing with 34 additions and 0 deletions.
  1. +34 −0 src/test/java/org/sooo/ProtocolBufferTest.java
@@ -95,4 +95,38 @@ private LoginLogs createLoginLogs(int size) {
.setId(id + i).setUrl(url + i).build());
return LoginLogs.newBuilder().addAllLoginLogs(list).build();
}
+
+ @Test
+ public void compareByteLengthForCollections() throws Exception {
+ int size = 100;
+
+ LoginLogs protobufObject = createLoginLogs(size);
+ long started = System.nanoTime();
+ byte[] serialized = protobufObject.toByteArray();
+ System.out.println("ElapsedForSerializingProtobufObject="
+ + (System.nanoTime() - started));
+ System.out.println("serializedProtonufObject.bytes="
+ + serialized.length);
+
+ List<SerializableLoginLog> list = createSerializableLoginLogList(size);
+ started = System.nanoTime();
+ serialized = getBytes(list);
+ System.out.println("ElapsedForSerializingProtobufObject="
+ + (System.nanoTime() - started));
+ System.out.println("serializedProtonufObject.bytes="
+ + serialized.length);
+
+ started = System.nanoTime();
+ serialized = new Gson().toJson(list).getBytes();
+ System.out.println("ElapsedForSerializingJsonString="
+ + (System.nanoTime() - started));
+ System.out.println("serializedJsonString.bytes=" + serialized.length);
+ }
+
+ private List<SerializableLoginLog> createSerializableLoginLogList(int size) {
+ List<SerializableLoginLog> list = Lists.newArrayList();
+ for (int i = 0; i < size; i++)
+ list.add(new SerializableLoginLog(timestamp + i, id + i, url + i));
+ return list;
+ }
}

0 comments on commit 51c97fe

Please sign in to comment.