Skip to content

Commit 0dabac5

Browse files
committed
Benchmark the performance more precisely
1 parent af98ae9 commit 0dabac5

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import org.msgpack.core.buffer.{ChannelBufferOutput, OutputStreamBufferOutput}
2222
import org.msgpack.value.ValueFactory
2323
import xerial.core.io.IOUtil
2424

25-
import scala.compat.Platform
2625
import scala.util.Random
2726

2827
/**
@@ -238,22 +237,29 @@ class MessagePackerTest
238237
}
239238

240239
"pack a lot of String within expected time" in {
241-
val count = 500000
240+
val count = 20000
242241

243-
def measureDuration(outputStream: java.io.OutputStream) : Long = {
242+
def measureDuration(outputStream: java.io.OutputStream) = {
244243
val packer = MessagePack.newDefaultPacker(outputStream)
245-
val start = Platform.currentTime
246-
for (i <- 0 to count) {
244+
var i = 0
245+
while (i < count) {
247246
packer.packString("0123456789ABCDEF")
247+
i += 1
248248
}
249249
packer.close
250-
Platform.currentTime - start
251250
}
252251

253-
val baseDuration = measureDuration(new ByteArrayOutputStream())
254-
val (_, fileOutput) = createTempFileWithOutputStream
255-
val targetDuration = measureDuration(fileOutput)
256-
targetDuration shouldBe <= (baseDuration * 3)
252+
val t = time("packString into OutputStream", repeat = 10) {
253+
block("byte-array-output-stream") {
254+
measureDuration(new ByteArrayOutputStream())
255+
}
256+
257+
block("file-output-stream") {
258+
val (_, fileOutput) = createTempFileWithOutputStream
259+
measureDuration(fileOutput)
260+
}
261+
}
262+
t("file-output-stream").averageWithoutMinMax shouldBe < (t("byte-array-output-stream").averageWithoutMinMax * 4)
257263
}
258264
}
259265

0 commit comments

Comments
 (0)