|
15 | 15 | // |
16 | 16 | package org.msgpack.jackson.dataformat.benchmark; |
17 | 17 |
|
| 18 | +import com.fasterxml.jackson.core.JsonGenerator; |
18 | 19 | import com.fasterxml.jackson.core.JsonProcessingException; |
19 | 20 | import com.fasterxml.jackson.core.type.TypeReference; |
20 | 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
21 | 22 | import org.junit.Test; |
22 | | -import org.msgpack.jackson.dataformat.MessagePackDataformatTestBase; |
23 | 23 | import org.msgpack.jackson.dataformat.MessagePackFactory; |
24 | 24 |
|
| 25 | +import java.io.File; |
| 26 | +import java.io.FileOutputStream; |
| 27 | +import java.io.OutputStream; |
25 | 28 | import java.util.ArrayList; |
26 | 29 | import java.util.List; |
27 | 30 |
|
28 | 31 | public class MessagePackDataformatHugeDataBenchmarkTest |
29 | | - extends MessagePackDataformatTestBase |
30 | 32 | { |
31 | | - private static final int ELM_NUM = 1000000; |
32 | | - private static final int SAMPLING_COUNT = 4; |
| 33 | + private static final int ELM_NUM = 100000; |
| 34 | + private static final int COUNT = 6; |
| 35 | + private static final int WARMUP_COUNT = 4; |
33 | 36 | private final ObjectMapper origObjectMapper = new ObjectMapper(); |
34 | 37 | private final ObjectMapper msgpackObjectMapper = new ObjectMapper(new MessagePackFactory()); |
35 | 38 | private static final List<Object> value; |
@@ -66,34 +69,68 @@ public class MessagePackDataformatHugeDataBenchmarkTest |
66 | 69 | packedByMsgPack = bytes; |
67 | 70 | } |
68 | 71 |
|
| 72 | + public MessagePackDataformatHugeDataBenchmarkTest() |
| 73 | + { |
| 74 | + origObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); |
| 75 | + msgpackObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); |
| 76 | + } |
| 77 | + |
69 | 78 | @Test |
70 | 79 | public void testBenchmark() |
71 | 80 | throws Exception |
72 | 81 | { |
73 | | - double[] durationOfSerializeWithJson = new double[SAMPLING_COUNT]; |
74 | | - double[] durationOfSerializeWithMsgPack = new double[SAMPLING_COUNT]; |
75 | | - double[] durationOfDeserializeWithJson = new double[SAMPLING_COUNT]; |
76 | | - double[] durationOfDeserializeWithMsgPack = new double[SAMPLING_COUNT]; |
77 | | - for (int si = 0; si < SAMPLING_COUNT; si++) { |
78 | | - long currentTimeMillis = System.currentTimeMillis(); |
79 | | - origObjectMapper.writeValueAsBytes(value); |
80 | | - durationOfSerializeWithJson[si] = System.currentTimeMillis() - currentTimeMillis; |
| 82 | + Benchmarker benchmarker = new Benchmarker(); |
| 83 | + |
| 84 | + File tempFileJackson = File.createTempFile("msgpack-jackson-", "-huge-jackson"); |
| 85 | + tempFileJackson.deleteOnExit(); |
| 86 | + final OutputStream outputStreamJackson = new FileOutputStream(tempFileJackson); |
| 87 | + |
| 88 | + File tempFileMsgpack = File.createTempFile("msgpack-jackson-", "-huge-msgpack"); |
| 89 | + tempFileMsgpack.deleteOnExit(); |
| 90 | + final OutputStream outputStreamMsgpack = new FileOutputStream(tempFileMsgpack); |
81 | 91 |
|
82 | | - currentTimeMillis = System.currentTimeMillis(); |
83 | | - msgpackObjectMapper.writeValueAsBytes(value); |
84 | | - durationOfSerializeWithMsgPack[si] = System.currentTimeMillis() - currentTimeMillis; |
| 92 | + benchmarker.addBenchmark(new Benchmarker.Benchmarkable("serialize(huge) with JSON") { |
| 93 | + @Override |
| 94 | + public void run() |
| 95 | + throws Exception |
| 96 | + { |
| 97 | + origObjectMapper.writeValue(outputStreamJackson, value); |
| 98 | + } |
| 99 | + }); |
85 | 100 |
|
86 | | - currentTimeMillis = System.currentTimeMillis(); |
87 | | - origObjectMapper.readValue(packedByOriginal, new TypeReference<List<Object>>() {}); |
88 | | - durationOfDeserializeWithJson[si] = System.currentTimeMillis() - currentTimeMillis; |
| 101 | + benchmarker.addBenchmark(new Benchmarker.Benchmarkable("serialize(huge) with MessagePack") { |
| 102 | + @Override |
| 103 | + public void run() |
| 104 | + throws Exception |
| 105 | + { |
| 106 | + msgpackObjectMapper.writeValue(outputStreamMsgpack, value); |
| 107 | + } |
| 108 | + }); |
89 | 109 |
|
90 | | - currentTimeMillis = System.currentTimeMillis(); |
91 | | - msgpackObjectMapper.readValue(packedByMsgPack, new TypeReference<List<Object>>() {}); |
92 | | - durationOfDeserializeWithMsgPack[si] = System.currentTimeMillis() - currentTimeMillis; |
| 110 | + benchmarker.addBenchmark(new Benchmarker.Benchmarkable("deserialize(huge) with JSON") { |
| 111 | + @Override |
| 112 | + public void run() |
| 113 | + throws Exception |
| 114 | + { |
| 115 | + origObjectMapper.readValue(packedByOriginal, new TypeReference<List<Object>>() {}); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + benchmarker.addBenchmark(new Benchmarker.Benchmarkable("deserialize(huge) with MessagePack") { |
| 120 | + @Override |
| 121 | + public void run() |
| 122 | + throws Exception |
| 123 | + { |
| 124 | + msgpackObjectMapper.readValue(packedByMsgPack, new TypeReference<List<Object>>() {}); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + try { |
| 129 | + benchmarker.run(COUNT, WARMUP_COUNT); |
| 130 | + } |
| 131 | + finally { |
| 132 | + outputStreamJackson.close(); |
| 133 | + outputStreamMsgpack.close(); |
93 | 134 | } |
94 | | - printStat("serialize(huge) with JSON", durationOfSerializeWithJson); |
95 | | - printStat("serialize(huge) with MessagePack", durationOfSerializeWithMsgPack); |
96 | | - printStat("deserialize(huge) with JSON", durationOfDeserializeWithJson); |
97 | | - printStat("deserialize(huge) with MessagePack", durationOfDeserializeWithMsgPack); |
98 | 135 | } |
99 | 136 | } |
0 commit comments