@@ -3,6 +3,7 @@ package org.msgpack.core
33import java .io .ByteArrayOutputStream
44
55import org .msgpack .core .buffer .{OutputStreamBufferOutput , ArrayBufferInput }
6+ import xerial .core .io .IOUtil
67
78import scala .util .Random
89
@@ -11,7 +12,7 @@ import scala.util.Random
1112 */
1213class 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