|
18 | 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
19 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
20 | 20 | import org.junit.Test; |
21 | | -import org.msgpack.jackson.dataformat.MessagePackDataformatTestBase; |
22 | 21 | import org.msgpack.jackson.dataformat.MessagePackFactory; |
| 22 | +import static org.msgpack.jackson.dataformat.MessagePackDataformatTestBase.NormalPojo; |
| 23 | +import static org.msgpack.jackson.dataformat.MessagePackDataformatTestBase.Suit; |
23 | 24 |
|
24 | 25 | import java.math.BigInteger; |
25 | 26 | import java.util.ArrayList; |
26 | 27 | import java.util.List; |
27 | 28 |
|
28 | 29 | public class MessagePackDataformatPojoBenchmarkTest |
29 | | - extends MessagePackDataformatTestBase |
30 | 30 | { |
31 | | - private static final int LOOP_MAX = 1000; |
32 | | - private static final int LOOP_FACTOR = 50; |
33 | | - private static final int SAMPLING_COUNT = 4; |
| 31 | + private static final int LOOP_MAX = 600; |
| 32 | + private static final int LOOP_FACTOR = 40; |
| 33 | + private static final int COUNT = 6; |
| 34 | + private static final int WARMUP_COUNT = 4; |
34 | 35 | private static final List<NormalPojo> pojos = new ArrayList<NormalPojo>(LOOP_MAX); |
35 | 36 | private static final List<byte[]> pojosSerWithOrig = new ArrayList<byte[]>(LOOP_MAX); |
36 | 37 | private static final List<byte[]> pojosSerWithMsgPack = new ArrayList<byte[]>(LOOP_MAX); |
@@ -91,46 +92,60 @@ public class MessagePackDataformatPojoBenchmarkTest |
91 | 92 | public void testBenchmark() |
92 | 93 | throws Exception |
93 | 94 | { |
94 | | - double[] durationOfSerializeWithJson = new double[SAMPLING_COUNT]; |
95 | | - double[] durationOfSerializeWithMsgPack = new double[SAMPLING_COUNT]; |
96 | | - double[] durationOfDeserializeWithJson = new double[SAMPLING_COUNT]; |
97 | | - double[] durationOfDeserializeWithMsgPack = new double[SAMPLING_COUNT]; |
98 | | - for (int si = 0; si < SAMPLING_COUNT; si++) { |
99 | | - long currentTimeMillis = System.currentTimeMillis(); |
100 | | - for (int j = 0; j < LOOP_FACTOR; j++) { |
101 | | - for (int i = 0; i < LOOP_MAX; i++) { |
102 | | - origObjectMapper.writeValueAsBytes(pojos.get(i)); |
| 95 | + Benchmarker benchmarker = new Benchmarker(); |
| 96 | + |
| 97 | + benchmarker.addBenchmark(new Benchmarker.Benchmarkable("serialize(pojo) with JSON") { |
| 98 | + @Override |
| 99 | + public void run() |
| 100 | + throws Exception |
| 101 | + { |
| 102 | + for (int j = 0; j < LOOP_FACTOR; j++) { |
| 103 | + for (int i = 0; i < LOOP_MAX; i++) { |
| 104 | + origObjectMapper.writeValueAsBytes(pojos.get(i)); |
| 105 | + } |
103 | 106 | } |
104 | 107 | } |
105 | | - durationOfSerializeWithJson[si] = System.currentTimeMillis() - currentTimeMillis; |
| 108 | + }); |
106 | 109 |
|
107 | | - currentTimeMillis = System.currentTimeMillis(); |
108 | | - for (int j = 0; j < LOOP_FACTOR; j++) { |
109 | | - for (int i = 0; i < LOOP_MAX; i++) { |
110 | | - msgpackObjectMapper.writeValueAsBytes(pojos.get(i)); |
| 110 | + benchmarker.addBenchmark(new Benchmarker.Benchmarkable("serialize(pojo) with MessagePack") { |
| 111 | + @Override |
| 112 | + public void run() |
| 113 | + throws Exception |
| 114 | + { |
| 115 | + for (int j = 0; j < LOOP_FACTOR; j++) { |
| 116 | + for (int i = 0; i < LOOP_MAX; i++) { |
| 117 | + msgpackObjectMapper.writeValueAsBytes(pojos.get(i)); |
| 118 | + } |
111 | 119 | } |
112 | 120 | } |
113 | | - durationOfSerializeWithMsgPack[si] = System.currentTimeMillis() - currentTimeMillis; |
| 121 | + }); |
114 | 122 |
|
115 | | - currentTimeMillis = System.currentTimeMillis(); |
116 | | - for (int j = 0; j < LOOP_FACTOR; j++) { |
117 | | - for (int i = 0; i < LOOP_MAX; i++) { |
118 | | - origObjectMapper.readValue(pojosSerWithOrig.get(i), NormalPojo.class); |
| 123 | + benchmarker.addBenchmark(new Benchmarker.Benchmarkable("deserialize(pojo) with JSON") { |
| 124 | + @Override |
| 125 | + public void run() |
| 126 | + throws Exception |
| 127 | + { |
| 128 | + for (int j = 0; j < LOOP_FACTOR; j++) { |
| 129 | + for (int i = 0; i < LOOP_MAX; i++) { |
| 130 | + origObjectMapper.readValue(pojosSerWithOrig.get(i), NormalPojo.class); |
| 131 | + } |
119 | 132 | } |
120 | 133 | } |
121 | | - durationOfDeserializeWithJson[si] = System.currentTimeMillis() - currentTimeMillis; |
| 134 | + }); |
122 | 135 |
|
123 | | - currentTimeMillis = System.currentTimeMillis(); |
124 | | - for (int j = 0; j < LOOP_FACTOR; j++) { |
125 | | - for (int i = 0; i < LOOP_MAX; i++) { |
126 | | - msgpackObjectMapper.readValue(pojosSerWithMsgPack.get(i), NormalPojo.class); |
| 136 | + benchmarker.addBenchmark(new Benchmarker.Benchmarkable("deserialize(pojo) with MessagePack") { |
| 137 | + @Override |
| 138 | + public void run() |
| 139 | + throws Exception |
| 140 | + { |
| 141 | + for (int j = 0; j < LOOP_FACTOR; j++) { |
| 142 | + for (int i = 0; i < LOOP_MAX; i++) { |
| 143 | + msgpackObjectMapper.readValue(pojosSerWithMsgPack.get(i), NormalPojo.class); |
| 144 | + } |
127 | 145 | } |
128 | 146 | } |
129 | | - durationOfDeserializeWithMsgPack[si] = System.currentTimeMillis() - currentTimeMillis; |
130 | | - } |
131 | | - printStat("serialize(pojo) with JSON", durationOfSerializeWithJson); |
132 | | - printStat("serialize(pojo) with MessagePack", durationOfSerializeWithMsgPack); |
133 | | - printStat("deserialize(pojo) with JSON", durationOfDeserializeWithJson); |
134 | | - printStat("deserialize(pojo) with MessagePack", durationOfDeserializeWithMsgPack); |
| 147 | + }); |
| 148 | + |
| 149 | + benchmarker.run(COUNT, WARMUP_COUNT); |
135 | 150 | } |
136 | 151 | } |
0 commit comments