PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jackson-src-1.9.4/src/perf/perf/TestDeserPerf.java

#
Java | 243 lines | 152 code | 36 blank | 55 comment | 13 complexity | 89a3ec35d7f315859fee93b1de959358 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. package perf;
  2. import org.codehaus.jackson.*;
  3. import org.codehaus.jackson.map.*;
  4. import org.codehaus.jackson.map.type.TypeFactory;
  5. import org.codehaus.jackson.smile.SmileFactory;
  6. import org.codehaus.jackson.smile.SmileGenerator;
  7. import org.codehaus.jackson.type.JavaType;
  8. /**
  9. * Micro-benchmark for comparing performance of bean deserialization
  10. */
  11. public final class TestDeserPerf
  12. {
  13. /*
  14. /**********************************************************
  15. /* Actual test
  16. /**********************************************************
  17. */
  18. private final int REPS;
  19. private TestDeserPerf() {
  20. // Let's try to guestimate suitable size
  21. REPS = 25000;
  22. }
  23. private MediaItem buildItem()
  24. {
  25. MediaItem.Content content = new MediaItem.Content();
  26. content.setPlayer(MediaItem.Player.JAVA);
  27. content.setUri("http://javaone.com/keynote.mpg");
  28. content.setTitle("Javaone Keynote");
  29. content.setWidth(640);
  30. content.setHeight(480);
  31. content.setFormat("video/mpeg4");
  32. content.setDuration(18000000L);
  33. content.setSize(58982400L);
  34. content.setBitrate(262144);
  35. content.setCopyright("None");
  36. content.addPerson("Bill Gates");
  37. content.addPerson("Steve Jobs");
  38. MediaItem item = new MediaItem(content);
  39. item.addPhoto(new MediaItem.Photo("http://javaone.com/keynote_large.jpg", "Javaone Keynote", 1024, 768, MediaItem.Size.LARGE));
  40. item.addPhoto(new MediaItem.Photo("http://javaone.com/keynote_small.jpg", "Javaone Keynote", 320, 240, MediaItem.Size.SMALL));
  41. return item;
  42. }
  43. public void test()
  44. throws Exception
  45. {
  46. int sum = 0;
  47. final MediaItem item = buildItem();
  48. JsonFactory jsonF =
  49. // new org.codehaus.jackson.smile.SmileFactory();
  50. new JsonFactory()
  51. ;
  52. final ObjectMapper jsonMapper = new ObjectMapper(jsonF);
  53. // jsonMapper.configure(SerializationConfig.Feature.USE_STATIC_TYPING, true);
  54. final SmileFactory smileFactory = new SmileFactory();
  55. final ObjectMapper smileMapper = new ObjectMapper(smileFactory);
  56. smileFactory.configure(SmileGenerator.Feature.CHECK_SHARED_NAMES, true);
  57. // smileFactory.configure(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES, true);
  58. smileFactory.configure(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES, false);
  59. // Use Jackson?
  60. byte[] json = jsonMapper.writeValueAsBytes(item);
  61. // or another lib?
  62. // byte[] json = com.alibaba.fastjson.JSON.toJSONString(item, com.alibaba.fastjson.serializer.SerializerFeature.WriteEnumUsingToString).getBytes("UTF-8");
  63. System.out.println("Warmed up: data size is "+json.length+" bytes; "+REPS+" reps -> "
  64. +((REPS * json.length) >> 10)+" kB per iteration");
  65. System.out.println();
  66. byte[] smile = smileMapper.writeValueAsBytes(item);
  67. System.out.println(" smile size: "+smile.length+" bytes");
  68. /*
  69. byte[] bson;
  70. final ObjectMapper bsonMapper = new ObjectMapper(new de.undercouch.bson4jackson.BsonFactory());
  71. {
  72. bson = bsonMapper.writeValueAsBytes(item);
  73. System.out.println(" BSON size: "+bson.length+" bytes");
  74. }
  75. */
  76. { // verify equality
  77. System.out.println("Will verify state of Smile...");
  78. MediaItem result = smileMapper.readValue(smile, 0, smile.length, MediaItem.class);
  79. String jsonFromSmile = jsonMapper.writeValueAsString(result);
  80. String jsonFromItem = jsonMapper.writeValueAsString(item);
  81. if (!jsonFromSmile.equals(jsonFromItem)) {
  82. int ix = 0;
  83. for (int max = Math.min(jsonFromSmile.length(), jsonFromItem.length()); ix < max; ++ix) {
  84. if (jsonFromSmile.charAt(ix) != jsonFromItem.charAt(ix)) {
  85. break;
  86. }
  87. }
  88. System.err.println("Source JSON: ");
  89. System.err.println(jsonFromItem);
  90. System.err.println("------------");
  91. System.err.println("Smile JSON: ");
  92. System.err.println(jsonFromSmile);
  93. System.err.println("------------");
  94. for (int i = 0; i < ix; ++i) {
  95. System.err.print('=');
  96. }
  97. System.err.println("^");
  98. System.err.println("------------");
  99. throw new Error("No smile today -- data corruption!");
  100. }
  101. System.out.println("Verification successful: Smile ok!");
  102. }
  103. // for debugging:
  104. System.err.println("JSON = "+jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(item));
  105. int round = 0;
  106. while (true) {
  107. // try { Thread.sleep(100L); } catch (InterruptedException ie) { }
  108. // int round = 2;
  109. long curr = System.currentTimeMillis();
  110. String msg;
  111. round = (++round % 4);
  112. //if (true) round = 3;
  113. if (round < 2) round += 2;
  114. boolean lf = (round == 0);
  115. switch (round) {
  116. case 0:
  117. msg = "Deserialize, manual, JSON";
  118. sum += testDeser(jsonMapper.getJsonFactory(), json, REPS);
  119. break;
  120. case 1:
  121. msg = "Deserialize, manual/FAST, JSON";
  122. sum += testDeserFaster(jsonMapper.getJsonFactory(), json, REPS);
  123. break;
  124. /*
  125. case 2:
  126. msg = "Deserialize, bind, JSON";
  127. sum += testDeser(jsonMapper, json, REPS);
  128. break;
  129. case 2:
  130. msg = "Deserialize, smile";
  131. sum += testDeser(smileMapper, smile, REPS * 2);
  132. break;
  133. */
  134. case 2:
  135. msg = "Deserialize, manual, Smile";
  136. sum += testDeser(smileMapper.getJsonFactory(), smile, REPS);
  137. break;
  138. case 3:
  139. msg = "Deserialize, manual/FAST, Smile";
  140. sum += testDeserFaster(smileMapper.getJsonFactory(), smile, REPS);
  141. break;
  142. /*
  143. case 2:
  144. msg = "Deserialize, fast-json";
  145. sum += testFastJson(json, REPS);
  146. break;
  147. */
  148. default:
  149. throw new Error("Internal error");
  150. }
  151. curr = System.currentTimeMillis() - curr;
  152. if (lf) {
  153. System.out.println();
  154. }
  155. System.out.println("Test '"+msg+"' -> "+curr+" msecs ("
  156. +(sum & 0xFF)+").");
  157. }
  158. }
  159. protected int testDeser(ObjectMapper mapper, byte[] input, int reps)
  160. throws Exception
  161. {
  162. JavaType type = TypeFactory.defaultInstance().constructType(MediaItem.class);
  163. MediaItem item = null;
  164. for (int i = 0; i < reps; ++i) {
  165. item = mapper.readValue(input, 0, input.length, type);
  166. }
  167. return item.hashCode(); // just to get some non-optimizable number
  168. }
  169. protected int testDeser(JsonFactory jf, byte[] input, int reps)
  170. throws Exception
  171. {
  172. MediaItem item = null;
  173. for (int i = 0; i < reps; ++i) {
  174. JsonParser jp = jf.createJsonParser(input);
  175. item = MediaItem.deserialize(jp);
  176. jp.close();
  177. }
  178. return item.hashCode(); // just to get some non-optimizable number
  179. }
  180. protected int testDeserFaster(JsonFactory jf, byte[] input, int reps)
  181. throws Exception
  182. {
  183. MediaItem item = null;
  184. for (int i = 0; i < reps; ++i) {
  185. JsonParser jp = jf.createJsonParser(input);
  186. item = MediaItem.deserializeFaster(jp);
  187. jp.close();
  188. }
  189. return item.hashCode(); // just to get some non-optimizable number
  190. }
  191. /*
  192. protected int testFastJson(byte[] input, int reps)
  193. throws Exception
  194. {
  195. MediaItem item = null;
  196. for (int i = 0; i < reps; ++i) {
  197. item = com.alibaba.fastjson.JSON.parseObject(input, MediaItem.class);
  198. }
  199. return item.hashCode(); // just to get some non-optimizable number
  200. }
  201. */
  202. public static void main(String[] args) throws Exception
  203. {
  204. new TestDeserPerf().test();
  205. }
  206. }