From a2199878013cb5f36c8f7fab30f084fd02af10a7 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Mon, 12 Jul 2021 11:53:08 +0200 Subject: [PATCH] benchmark: ByteString append and builder (#30313) --- .../util/ByteString_append_Benchmark.scala | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 akka-bench-jmh/src/main/scala/akka/util/ByteString_append_Benchmark.scala diff --git a/akka-bench-jmh/src/main/scala/akka/util/ByteString_append_Benchmark.scala b/akka-bench-jmh/src/main/scala/akka/util/ByteString_append_Benchmark.scala new file mode 100644 index 0000000000..978f0b36d8 --- /dev/null +++ b/akka-bench-jmh/src/main/scala/akka/util/ByteString_append_Benchmark.scala @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2021 Lightbend Inc. + */ + +package akka.util + +import java.util.concurrent.TimeUnit + +import org.openjdk.jmh.annotations._ +import org.openjdk.jmh.infra.Blackhole + +@State(Scope.Benchmark) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@BenchmarkMode(Array(Mode.Throughput)) +@Fork(2) +@Warmup(iterations = 4) +@Measurement(iterations = 10) +class ByteString_append_Benchmark { + + private val bs = ByteString(Array.ofDim[Byte](10)) + + @Benchmark + @OperationsPerInvocation(10000) + def appendThree(bh: Blackhole): Unit = { + var result = ByteString.empty + var i = 0 + while (i < 10000) { + result = result ++ bs + if (i % 3 == 0) { + bh.consume(result) + result = ByteString.empty + } + i += 1 + } + bh.consume(result) + } + + @Benchmark + @OperationsPerInvocation(10000) + def appendMany(bh: Blackhole): Unit = { + var result = ByteString.empty + var i = 0 + while (i < 10000) { + result = result ++ bs + i += 1 + } + bh.consume(result) + } + @Benchmark + @OperationsPerInvocation(10000) + def builderOne(bh: Blackhole): Unit = { + val builder = ByteString.newBuilder + var i = 0 + while (i < 10000) { + builder ++= bs + bh.consume(builder.result()) + builder.clear() + i += 1 + } + } + + @Benchmark + @OperationsPerInvocation(10000) + def builderThree(bh: Blackhole): Unit = { + val builder = ByteString.newBuilder + var i = 0 + while (i < 10000) { + builder ++= bs + if (i % 3 == 0) { + bh.consume(builder.result()) + builder.clear() + } + i += 1 + } + bh.consume(builder.result()) + } + + @Benchmark + @OperationsPerInvocation(10000) + def builderFive(bh: Blackhole): Unit = { + val builder = ByteString.newBuilder + var i = 0 + while (i < 10000) { + builder ++= bs + if (i % 5 == 0) { + bh.consume(builder.result()) + builder.clear() + } + i += 1 + } + bh.consume(builder.result()) + } + + @Benchmark + @OperationsPerInvocation(10000) + def builderMany(bh: Blackhole): Unit = { + val builder = ByteString.newBuilder + var i = 0 + while (i < 10000) { + builder ++= bs + i += 1 + } + bh.consume(builder.result()) + } +}