Skip to content

Commit 14adad2

Browse files
committed
msgpack#127 Add a performance test for OutputStremBufferOutput.reset
1 parent 478b928 commit 14adad2

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

msgpack-core/src/main/java/org/msgpack/core/MessagePackFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public MessagePackFactory(MessagePack.Config config) {
2727
*/
2828
public static final MessagePackFactory DEFAULT = new MessagePackFactory(MessagePack.DEFAULT_CONFIG);
2929

30+
31+
public static MessagePacker newDefaultPacker(OutputStream out) {
32+
return DEFAULT.newPacker(out);
33+
}
34+
35+
3036
/**
3137
* Create an MessagePacker that outputs the packed data to the specified stream
3238
* @param out

msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.msgpack.core
33
import java.io.ByteArrayOutputStream
44

55
import org.msgpack.core.buffer.{OutputStreamBufferOutput, ArrayBufferInput}
6+
import xerial.core.io.IOUtil
67

78
import scala.util.Random
89

@@ -11,7 +12,7 @@ import scala.util.Random
1112
*/
1213
class MessagePackerTest extends MessagePackSpec {
1314

14-
val mf = MessagePackFactory.DEFAULT;
15+
val mf = MessagePackFactory.DEFAULT
1516

1617
def verifyIntSeq(answer:Array[Int], packed:Array[Byte]) {
1718
val unpacker = mf.newUnpacker(packed)
@@ -50,5 +51,41 @@ class MessagePackerTest extends MessagePackSpec {
5051
verifyIntSeq(intSeq3, b3.toByteArray)
5152
}
5253

54+
"improve the performance via reset method" taggedAs("reset") in {
55+
56+
57+
val N = 1000
58+
val t = time("packer", repeat = 10) {
59+
block("no-buffer-reset") {
60+
val out = new ByteArrayOutputStream
61+
IOUtil.withResource(MessagePackFactory.newDefaultPacker(out)) { packer =>
62+
for (i <- 0 until N) {
63+
val outputStream = new ByteArrayOutputStream()
64+
packer.reset(new OutputStreamBufferOutput(outputStream))
65+
packer.packInt(0)
66+
packer.flush()
67+
}
68+
}
69+
}
70+
71+
block("buffer-reset") {
72+
val out = new ByteArrayOutputStream
73+
IOUtil.withResource(MessagePackFactory.newDefaultPacker(out)) { packer =>
74+
val bufferOut = new OutputStreamBufferOutput(new ByteArrayOutputStream())
75+
for (i <- 0 until N) {
76+
val outputStream = new ByteArrayOutputStream()
77+
bufferOut.reset(outputStream)
78+
packer.reset(bufferOut)
79+
packer.packInt(0)
80+
packer.flush()
81+
}
82+
}
83+
}
84+
}
85+
86+
t("buffer-reset").averageWithoutMinMax should be <= t("no-buffer-reset").averageWithoutMinMax
87+
88+
}
89+
5390
}
5491
}

0 commit comments

Comments
 (0)