PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/protostuff-core/src/test/java/com/dyuproject/protostuff/CompareOutputsTest.java

http://protostuff.googlecode.com/
Java | 344 lines | 254 code | 57 blank | 33 comment | 10 complexity | fe79f1f177aab055630faf7b1f24ce49 MD5 | raw file
Possible License(s): Apache-2.0
  1. //========================================================================
  2. //Copyright 2007-2009 David Yu dyuproject@gmail.com
  3. //------------------------------------------------------------------------
  4. //Licensed under the Apache License, Version 2.0 (the "License");
  5. //you may not use this file except in compliance with the License.
  6. //You may obtain a copy of the License at
  7. //http://www.apache.org/licenses/LICENSE-2.0
  8. //Unless required by applicable law or agreed to in writing, software
  9. //distributed under the License is distributed on an "AS IS" BASIS,
  10. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. //See the License for the specific language governing permissions and
  12. //limitations under the License.
  13. //========================================================================
  14. package com.dyuproject.protostuff;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.io.PrintStream;
  20. import com.dyuproject.protostuff.Foo.EnumSample;
  21. /**
  22. * Benchmark to compare the serialization speed of 3 types.
  23. * CodedOutput, BufferedOutput and DeferredOutput.
  24. *
  25. * @author David Yu
  26. * @created Nov 13, 2009
  27. */
  28. public class CompareOutputsTest extends AbstractTest
  29. {
  30. static final Baz negativeBaz = new Baz(-567, "negativeBaz", -202020202);
  31. static final Bar negativeBar = new Bar(-12, "negativeBar", negativeBaz, Bar.Status.STARTED,
  32. ByteString.copyFromUtf8("a1"), true, -130.031f, -1000.0001d, -101010101);
  33. static final Baz baz = new Baz(567, "baz", 202020202);
  34. static final Bar bar = new Bar(890, "bar", baz, Bar.Status.STARTED,
  35. ByteString.copyFromUtf8("b2"), true, 150.051f, 2000.0002d, 303030303);
  36. // a total of 1000 bytes
  37. public static final Foo foo = SerializableObjects.newFoo(
  38. new Integer[]{90210,-90210, 0},
  39. new String[]{"foo", "123456789012345678901234567890123456789012"},
  40. new Bar[]{bar, negativeBar},
  41. new EnumSample[]{EnumSample.TYPE0, EnumSample.TYPE2},
  42. new ByteString[]{
  43. ByteString.copyFromUtf8("ef"),
  44. ByteString.copyFromUtf8("gh"),
  45. // two 350-byte bytestring.
  46. ByteString.copyFromUtf8("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"),
  47. ByteString.copyFromUtf8("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890")},
  48. new Boolean[]{true, false},
  49. new Float[]{1234.4321f, -1234.4321f, 0f},
  50. new Double[]{12345678.87654321d, -12345678.87654321d, 0d},
  51. new Long[]{7060504030201l, -7060504030201l, 0l});
  52. public void testFooProtobuf() throws Exception
  53. {
  54. Foo fooCompare = foo;
  55. byte[] computed = toByteArrayComputedProtobuf(fooCompare, fooCompare.cachedSchema());
  56. byte[] buffered = toByteArrayBufferedProtobuf(fooCompare, fooCompare.cachedSchema());
  57. byte[] streamed = toByteArrayStreamedProtobuf(fooCompare, fooCompare.cachedSchema());
  58. assertTrue(computed.length == buffered.length);
  59. assertTrue(computed.length == streamed.length);
  60. String strComputed = new String(computed, "UTF-8");
  61. assertEquals(strComputed, new String(buffered, "UTF-8"));
  62. assertEquals(strComputed, new String(streamed, "UTF-8"));
  63. }
  64. public void testFooProtostuff() throws Exception
  65. {
  66. Foo fooCompare = foo;
  67. byte[] computed = toByteArrayComputedProtostuff(fooCompare, fooCompare.cachedSchema());
  68. byte[] buffered = toByteArrayBufferedProtostuff(fooCompare, fooCompare.cachedSchema());
  69. byte[] streamed = toByteArrayStreamedProtostuff(fooCompare, fooCompare.cachedSchema());
  70. assertTrue(computed.length == buffered.length);
  71. assertTrue(computed.length == streamed.length);
  72. String strComputed = new String(computed, "UTF-8");
  73. assertEquals(strComputed, new String(buffered, "UTF-8"));
  74. assertEquals(strComputed, new String(streamed, "UTF-8"));
  75. }
  76. public void testBenchmark() throws Exception
  77. {
  78. if(!"false".equals(System.getProperty("benchmark.skip")))
  79. return;
  80. String dir = System.getProperty("benchmark.output_dir");
  81. PrintStream out = dir==null ? System.out :
  82. new PrintStream(new FileOutputStream(new File(new File(dir),
  83. "protostuff-core-"+System.currentTimeMillis()+".txt"), true));
  84. int warmups = Integer.getInteger("benchmark.warmups", 200000);
  85. int loops = Integer.getInteger("benchmark.loops", 2000000);
  86. String title = "protostuff-core serialization benchmark for " + loops + " runs";
  87. out.println(title);
  88. out.println();
  89. start(foo, SERIALIZERS, out, warmups, loops);
  90. if(System.out!=out)
  91. out.close();
  92. }
  93. public static void main(String[] args) throws Exception
  94. {
  95. String dir = System.getProperty("benchmark.output_dir");
  96. PrintStream out = dir==null ? System.out :
  97. new PrintStream(new FileOutputStream(new File(new File(dir),
  98. "protostuff-core-"+System.currentTimeMillis()+".txt"), true));
  99. int warmups = Integer.getInteger("benchmark.warmups", 200000);
  100. int loops = Integer.getInteger("benchmark.loops", 2000000);
  101. String title = "protostuff-core serialization benchmark for " + loops + " runs";
  102. out.println(title);
  103. out.println();
  104. start(foo, SERIALIZERS, out, warmups, loops);
  105. if(System.out!=out)
  106. out.close();
  107. }
  108. public static <T extends Message<T>> void start(T message, Serializer[] serializers,
  109. PrintStream out, int warmups, int loops) throws Exception
  110. {
  111. for(Serializer s : serializers)
  112. ser(message, s, out, s.getName(), warmups, loops);
  113. }
  114. static <T extends Message<T>> void ser(T message, Serializer serializer, PrintStream out,
  115. String name, int warmups, int loops) throws Exception
  116. {
  117. int len = serializer.serialize(message).length;
  118. for(int i=0; i<warmups; i++)
  119. serializer.serialize(message);
  120. long start = System.currentTimeMillis();
  121. for(int i=0; i<loops; i++)
  122. serializer.serialize(message);
  123. long finish = System.currentTimeMillis();
  124. long elapsed = finish - start;
  125. out.println(elapsed + " ms elapsed with " + len + " bytes for " + name);
  126. }
  127. static <T> byte[] toByteArrayComputedProtobuf(T message, Schema<T> schema)
  128. {
  129. return CodedOutput.toByteArray(message, schema, false);
  130. }
  131. static <T> byte[] toByteArrayBufferedProtobuf(T message, Schema<T> schema)
  132. {
  133. final ProtobufOutput output = new ProtobufOutput(new LinkedBuffer(BUF_SIZE));
  134. try
  135. {
  136. schema.writeTo(output, message);
  137. }
  138. catch (IOException e)
  139. {
  140. throw new RuntimeException("Serializing to a byte array threw an IOException " +
  141. "(should never happen).", e);
  142. }
  143. return output.toByteArray();
  144. }
  145. static <T> byte[] toByteArrayStreamedProtobuf(T message, Schema<T> schema)
  146. {
  147. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  148. final CodedOutput output = new CodedOutput(out, new byte[BUF_SIZE], false);
  149. try
  150. {
  151. schema.writeTo(output, message);
  152. output.flush();
  153. }
  154. catch (IOException e)
  155. {
  156. throw new RuntimeException("Serializing to a byte array threw an IOException " +
  157. "(should never happen).", e);
  158. }
  159. return out.toByteArray();
  160. }
  161. static <T> byte[] toByteArrayComputedProtostuff(T message, Schema<T> schema)
  162. {
  163. return CodedOutput.toByteArray(message, schema, true);
  164. }
  165. static <T> byte[] toByteArrayBufferedProtostuff(T message, Schema<T> schema)
  166. {
  167. final ProtostuffOutput output = new ProtostuffOutput(new LinkedBuffer(BUF_SIZE));
  168. try
  169. {
  170. schema.writeTo(output, message);
  171. }
  172. catch (IOException e)
  173. {
  174. throw new RuntimeException("Serializing to a byte array threw an IOException " +
  175. "(should never happen).", e);
  176. }
  177. return output.toByteArray();
  178. }
  179. static <T> byte[] toByteArrayStreamedProtostuff(T message, Schema<T> schema)
  180. {
  181. /*final ByteArrayOutputStream out = new ByteArrayOutputStream();
  182. try
  183. {
  184. ProtostuffIOUtil.writeTo(out, message, schema, new LinkedBuffer(BUF_SIZE));
  185. }
  186. catch (IOException e)
  187. {
  188. throw new RuntimeException("Serializing to a byte array threw an IOException " +
  189. "(should never happen).", e);
  190. }
  191. return out.toByteArray();*/
  192. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  193. final LinkedBuffer buffer = new LinkedBuffer(BUF_SIZE);
  194. final ProtostuffOutput output = new ProtostuffOutput(buffer, out);
  195. try
  196. {
  197. schema.writeTo(output, message);
  198. LinkedBuffer.writeTo(out, buffer);
  199. }
  200. catch (IOException e)
  201. {
  202. throw new RuntimeException("Serializing to a byte array threw an IOException " +
  203. "(should never happen).", e);
  204. }
  205. return out.toByteArray();
  206. }
  207. static final int BUF_SIZE = 256;
  208. public interface Serializer
  209. {
  210. public <T extends Message<T>> byte[] serialize(T message);
  211. public String getName();
  212. }
  213. static final Serializer PROTOBUF_COMPUTED_OUTPUT = new Serializer()
  214. {
  215. public <T extends Message<T>> byte[] serialize(T message)
  216. {
  217. return toByteArrayComputedProtobuf(message, message.cachedSchema());
  218. }
  219. public String getName()
  220. {
  221. return "protobuf-computed-output";
  222. }
  223. };
  224. static final Serializer PROTOBUF_BUFFERED_OUTPUT = new Serializer()
  225. {
  226. public <T extends Message<T>> byte[] serialize(T message)
  227. {
  228. return toByteArrayBufferedProtobuf(message, message.cachedSchema());
  229. }
  230. public String getName()
  231. {
  232. return "protobuf-buffered-output";
  233. }
  234. };
  235. static final Serializer PROTOBUF_STREAMED_OUTPUT = new Serializer()
  236. {
  237. public <T extends Message<T>> byte[] serialize(T message)
  238. {
  239. return toByteArrayStreamedProtobuf(message, message.cachedSchema());
  240. }
  241. public String getName()
  242. {
  243. return "protobuf-streamed-output";
  244. }
  245. };
  246. static final Serializer PROTOSTUFF_COMPUTED_OUTPUT = new Serializer()
  247. {
  248. public <T extends Message<T>> byte[] serialize(T message)
  249. {
  250. return toByteArrayComputedProtostuff(message, message.cachedSchema());
  251. }
  252. public String getName()
  253. {
  254. return "protostuff-computed-output";
  255. }
  256. };
  257. static final Serializer PROTOSTUFF_BUFFERED_OUTPUT = new Serializer()
  258. {
  259. public <T extends Message<T>> byte[] serialize(T message)
  260. {
  261. return toByteArrayBufferedProtostuff(message, message.cachedSchema());
  262. }
  263. public String getName()
  264. {
  265. return "protostuff-buffered-output";
  266. }
  267. };
  268. static final Serializer PROTOSTUFF_STREAMED_OUTPUT = new Serializer()
  269. {
  270. public <T extends Message<T>> byte[] serialize(T message)
  271. {
  272. return toByteArrayStreamedProtostuff(message, message.cachedSchema());
  273. }
  274. public String getName()
  275. {
  276. return "protostuff-streamed-output";
  277. }
  278. };
  279. static final Serializer[] SERIALIZERS = new Serializer[]{
  280. PROTOBUF_COMPUTED_OUTPUT,
  281. PROTOBUF_BUFFERED_OUTPUT,
  282. PROTOBUF_STREAMED_OUTPUT,
  283. PROTOSTUFF_COMPUTED_OUTPUT,
  284. PROTOSTUFF_BUFFERED_OUTPUT,
  285. PROTOSTUFF_STREAMED_OUTPUT
  286. };
  287. }