Skip to content

Commit 4d2594e

Browse files
committed
Use ObjectMapper#writeValue() instead of writeValueAsBytes() in benchmark
1 parent 3d9e168 commit 4d2594e

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
//
1616
package org.msgpack.jackson.dataformat.benchmark;
1717

18+
import com.fasterxml.jackson.core.JsonGenerator;
1819
import com.fasterxml.jackson.core.JsonProcessingException;
1920
import com.fasterxml.jackson.core.type.TypeReference;
2021
import com.fasterxml.jackson.databind.ObjectMapper;
2122
import org.junit.Test;
2223
import org.msgpack.jackson.dataformat.MessagePackFactory;
2324

25+
import java.io.File;
26+
import java.io.FileOutputStream;
27+
import java.io.OutputStream;
2428
import java.util.ArrayList;
2529
import java.util.List;
2630

@@ -65,18 +69,32 @@ public class MessagePackDataformatHugeDataBenchmarkTest
6569
packedByMsgPack = bytes;
6670
}
6771

72+
public MessagePackDataformatHugeDataBenchmarkTest()
73+
{
74+
origObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
75+
msgpackObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
76+
}
77+
6878
@Test
6979
public void testBenchmark()
7080
throws Exception
7181
{
7282
Benchmarker benchmarker = new Benchmarker();
7383

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);
91+
7492
benchmarker.addBenchmark(new Benchmarker.Benchmarkable("serialize(huge) with JSON") {
7593
@Override
7694
public void run()
7795
throws Exception
7896
{
79-
origObjectMapper.writeValueAsBytes(value);
97+
origObjectMapper.writeValue(outputStreamJackson, value);
8098
}
8199
});
82100

@@ -85,7 +103,7 @@ public void run()
85103
public void run()
86104
throws Exception
87105
{
88-
msgpackObjectMapper.writeValueAsBytes(value);
106+
msgpackObjectMapper.writeValue(outputStreamMsgpack, value);
89107
}
90108
});
91109

@@ -107,6 +125,12 @@ public void run()
107125
}
108126
});
109127

110-
benchmarker.run(COUNT, WARMUP_COUNT);
128+
try {
129+
benchmarker.run(COUNT, WARMUP_COUNT);
130+
}
131+
finally {
132+
outputStreamJackson.close();
133+
outputStreamMsgpack.close();
134+
}
111135
}
112136
}

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
//
1616
package org.msgpack.jackson.dataformat.benchmark;
1717

18+
import com.fasterxml.jackson.core.JsonGenerator;
1819
import com.fasterxml.jackson.core.JsonProcessingException;
1920
import com.fasterxml.jackson.databind.ObjectMapper;
2021
import org.junit.Test;
2122
import org.msgpack.jackson.dataformat.MessagePackFactory;
2223
import static org.msgpack.jackson.dataformat.MessagePackDataformatTestBase.NormalPojo;
2324
import static org.msgpack.jackson.dataformat.MessagePackDataformatTestBase.Suit;
2425

26+
import java.io.File;
27+
import java.io.FileOutputStream;
28+
import java.io.OutputStream;
2529
import java.math.BigInteger;
2630
import java.util.ArrayList;
2731
import java.util.List;
@@ -88,20 +92,34 @@ public class MessagePackDataformatPojoBenchmarkTest
8892
}
8993
}
9094

95+
public MessagePackDataformatPojoBenchmarkTest()
96+
{
97+
origObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
98+
msgpackObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
99+
}
100+
91101
@Test
92102
public void testBenchmark()
93103
throws Exception
94104
{
95105
Benchmarker benchmarker = new Benchmarker();
96106

107+
File tempFileJackson = File.createTempFile("msgpack-jackson-", "-huge-jackson");
108+
tempFileJackson.deleteOnExit();
109+
final OutputStream outputStreamJackson = new FileOutputStream(tempFileJackson);
110+
111+
File tempFileMsgpack = File.createTempFile("msgpack-jackson-", "-huge-msgpack");
112+
tempFileMsgpack.deleteOnExit();
113+
final OutputStream outputStreamMsgpack = new FileOutputStream(tempFileMsgpack);
114+
97115
benchmarker.addBenchmark(new Benchmarker.Benchmarkable("serialize(pojo) with JSON") {
98116
@Override
99117
public void run()
100118
throws Exception
101119
{
102120
for (int j = 0; j < LOOP_FACTOR; j++) {
103121
for (int i = 0; i < LOOP_MAX; i++) {
104-
origObjectMapper.writeValueAsBytes(pojos.get(i));
122+
origObjectMapper.writeValue(outputStreamJackson, pojos.get(i));
105123
}
106124
}
107125
}
@@ -114,7 +132,7 @@ public void run()
114132
{
115133
for (int j = 0; j < LOOP_FACTOR; j++) {
116134
for (int i = 0; i < LOOP_MAX; i++) {
117-
msgpackObjectMapper.writeValueAsBytes(pojos.get(i));
135+
msgpackObjectMapper.writeValue(outputStreamMsgpack, pojos.get(i));
118136
}
119137
}
120138
}
@@ -146,6 +164,12 @@ public void run()
146164
}
147165
});
148166

149-
benchmarker.run(COUNT, WARMUP_COUNT);
167+
try {
168+
benchmarker.run(COUNT, WARMUP_COUNT);
169+
}
170+
finally {
171+
outputStreamJackson.close();
172+
outputStreamMsgpack.close();
173+
}
150174
}
151175
}

0 commit comments

Comments
 (0)