Skip to content

Commit 3d9e168

Browse files
committed
Improve MessagePackDataformat(Pojo|HugeData)BenchmarkTest
1 parent 53d1248 commit 3d9e168

File tree

4 files changed

+184
-74
lines changed

4 files changed

+184
-74
lines changed

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatTestBase.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2020
import com.fasterxml.jackson.annotation.JsonProperty;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
22-
import org.apache.commons.math3.stat.StatUtils;
23-
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
2422
import org.junit.After;
2523
import org.junit.Before;
2624

@@ -99,17 +97,6 @@ public void teardown()
9997
}
10098
}
10199

102-
protected void printStat(String label, double[] values)
103-
{
104-
StandardDeviation standardDeviation = new StandardDeviation();
105-
System.out.println(label + ":");
106-
System.out.println(String.format(" mean : %.2f", StatUtils.mean(values)));
107-
System.out.println(String.format(" min : %.2f", StatUtils.min(values)));
108-
System.out.println(String.format(" max : %.2f", StatUtils.max(values)));
109-
System.out.println(String.format(" stdev: %.2f", standardDeviation.evaluate(values)));
110-
System.out.println("");
111-
}
112-
113100
public enum Suit
114101
{
115102
SPADE, HEART, DIAMOND, CLUB;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// MessagePack for Java
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+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.jackson.dataformat.benchmark;
17+
18+
import org.apache.commons.math3.stat.StatUtils;
19+
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
public class Benchmarker
25+
{
26+
private final List<Benchmarkable> benchmarkableList = new ArrayList<Benchmarkable>();
27+
28+
public abstract static class Benchmarkable
29+
{
30+
private final String label;
31+
32+
protected Benchmarkable(String label)
33+
{
34+
this.label = label;
35+
}
36+
37+
public abstract void run() throws Exception;
38+
}
39+
40+
public void addBenchmark(Benchmarkable benchmark)
41+
{
42+
benchmarkableList.add(benchmark);
43+
}
44+
45+
private static class Tuple<F, S>
46+
{
47+
F first;
48+
S second;
49+
50+
public Tuple(F first, S second)
51+
{
52+
this.first = first;
53+
this.second = second;
54+
}
55+
}
56+
57+
public void run(int count, int warmupCount)
58+
throws Exception
59+
{
60+
List<Tuple<String, double[]>> benchmarksResults = new ArrayList<Tuple<String, double[]>>(benchmarkableList.size());
61+
62+
for (Benchmarkable benchmark : benchmarkableList) {
63+
double[] durations = new double[count];
64+
65+
for (int i = 0; i < count + warmupCount; i++) {
66+
if (i >= warmupCount) {
67+
System.gc();
68+
}
69+
70+
long currentTimeNanos = System.nanoTime();
71+
benchmark.run();
72+
73+
if (i >= warmupCount) {
74+
durations[i - warmupCount] = (System.nanoTime() - currentTimeNanos) / 1000000.0;
75+
}
76+
}
77+
benchmarksResults.add(new Tuple<String, double[]>(benchmark.label, durations));
78+
}
79+
80+
for (Tuple<String, double[]> benchmarkResult : benchmarksResults) {
81+
printStat(benchmarkResult.first, benchmarkResult.second);
82+
}
83+
}
84+
85+
private void printStat(String label, double[] values)
86+
{
87+
StandardDeviation standardDeviation = new StandardDeviation();
88+
System.out.println(label + ":");
89+
System.out.println(String.format(" mean : %8.3f", StatUtils.mean(values)));
90+
System.out.println(String.format(" min : %8.3f", StatUtils.min(values)));
91+
System.out.println(String.format(" max : %8.3f", StatUtils.max(values)));
92+
System.out.println(String.format(" stdev: %8.3f", standardDeviation.evaluate(values)));
93+
System.out.println("");
94+
}
95+
}

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatHugeDataBenchmarkTest.java

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@
1919
import com.fasterxml.jackson.core.type.TypeReference;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import org.junit.Test;
22-
import org.msgpack.jackson.dataformat.MessagePackDataformatTestBase;
2322
import org.msgpack.jackson.dataformat.MessagePackFactory;
2423

2524
import java.util.ArrayList;
2625
import java.util.List;
2726

2827
public class MessagePackDataformatHugeDataBenchmarkTest
29-
extends MessagePackDataformatTestBase
3028
{
31-
private static final int ELM_NUM = 1000000;
32-
private static final int SAMPLING_COUNT = 4;
29+
private static final int ELM_NUM = 100000;
30+
private static final int COUNT = 6;
31+
private static final int WARMUP_COUNT = 4;
3332
private final ObjectMapper origObjectMapper = new ObjectMapper();
3433
private final ObjectMapper msgpackObjectMapper = new ObjectMapper(new MessagePackFactory());
3534
private static final List<Object> value;
@@ -70,30 +69,44 @@ public class MessagePackDataformatHugeDataBenchmarkTest
7069
public void testBenchmark()
7170
throws Exception
7271
{
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;
72+
Benchmarker benchmarker = new Benchmarker();
8173

82-
currentTimeMillis = System.currentTimeMillis();
83-
msgpackObjectMapper.writeValueAsBytes(value);
84-
durationOfSerializeWithMsgPack[si] = System.currentTimeMillis() - currentTimeMillis;
74+
benchmarker.addBenchmark(new Benchmarker.Benchmarkable("serialize(huge) with JSON") {
75+
@Override
76+
public void run()
77+
throws Exception
78+
{
79+
origObjectMapper.writeValueAsBytes(value);
80+
}
81+
});
8582

86-
currentTimeMillis = System.currentTimeMillis();
87-
origObjectMapper.readValue(packedByOriginal, new TypeReference<List<Object>>() {});
88-
durationOfDeserializeWithJson[si] = System.currentTimeMillis() - currentTimeMillis;
83+
benchmarker.addBenchmark(new Benchmarker.Benchmarkable("serialize(huge) with MessagePack") {
84+
@Override
85+
public void run()
86+
throws Exception
87+
{
88+
msgpackObjectMapper.writeValueAsBytes(value);
89+
}
90+
});
8991

90-
currentTimeMillis = System.currentTimeMillis();
91-
msgpackObjectMapper.readValue(packedByMsgPack, new TypeReference<List<Object>>() {});
92-
durationOfDeserializeWithMsgPack[si] = System.currentTimeMillis() - currentTimeMillis;
93-
}
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);
92+
benchmarker.addBenchmark(new Benchmarker.Benchmarkable("deserialize(huge) with JSON") {
93+
@Override
94+
public void run()
95+
throws Exception
96+
{
97+
origObjectMapper.readValue(packedByOriginal, new TypeReference<List<Object>>() {});
98+
}
99+
});
100+
101+
benchmarker.addBenchmark(new Benchmarker.Benchmarkable("deserialize(huge) with MessagePack") {
102+
@Override
103+
public void run()
104+
throws Exception
105+
{
106+
msgpackObjectMapper.readValue(packedByMsgPack, new TypeReference<List<Object>>() {});
107+
}
108+
});
109+
110+
benchmarker.run(COUNT, WARMUP_COUNT);
98111
}
99112
}

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatPojoBenchmarkTest.java

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
import com.fasterxml.jackson.core.JsonProcessingException;
1919
import com.fasterxml.jackson.databind.ObjectMapper;
2020
import org.junit.Test;
21-
import org.msgpack.jackson.dataformat.MessagePackDataformatTestBase;
2221
import org.msgpack.jackson.dataformat.MessagePackFactory;
22+
import static org.msgpack.jackson.dataformat.MessagePackDataformatTestBase.NormalPojo;
23+
import static org.msgpack.jackson.dataformat.MessagePackDataformatTestBase.Suit;
2324

2425
import java.math.BigInteger;
2526
import java.util.ArrayList;
2627
import java.util.List;
2728

2829
public class MessagePackDataformatPojoBenchmarkTest
29-
extends MessagePackDataformatTestBase
3030
{
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;
3435
private static final List<NormalPojo> pojos = new ArrayList<NormalPojo>(LOOP_MAX);
3536
private static final List<byte[]> pojosSerWithOrig = new ArrayList<byte[]>(LOOP_MAX);
3637
private static final List<byte[]> pojosSerWithMsgPack = new ArrayList<byte[]>(LOOP_MAX);
@@ -91,46 +92,60 @@ public class MessagePackDataformatPojoBenchmarkTest
9192
public void testBenchmark()
9293
throws Exception
9394
{
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+
}
103106
}
104107
}
105-
durationOfSerializeWithJson[si] = System.currentTimeMillis() - currentTimeMillis;
108+
});
106109

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+
}
111119
}
112120
}
113-
durationOfSerializeWithMsgPack[si] = System.currentTimeMillis() - currentTimeMillis;
121+
});
114122

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+
}
119132
}
120133
}
121-
durationOfDeserializeWithJson[si] = System.currentTimeMillis() - currentTimeMillis;
134+
});
122135

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+
}
127145
}
128146
}
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);
135150
}
136151
}

0 commit comments

Comments
 (0)