|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.pekko.stream |
| 19 | + |
| 20 | +import com.typesafe.config.ConfigFactory |
| 21 | +import org.apache.pekko.NotUsed |
| 22 | +import org.apache.pekko.actor.ActorSystem |
| 23 | +import org.apache.pekko.remote.artery.{ BenchTestSource, LatchSink } |
| 24 | +import org.apache.pekko.stream.scaladsl._ |
| 25 | +import org.apache.pekko.stream.testkit.scaladsl.StreamTestKit |
| 26 | +import org.openjdk.jmh.annotations._ |
| 27 | + |
| 28 | +import java.util.concurrent.{ CountDownLatch, TimeUnit } |
| 29 | +import scala.concurrent.Await |
| 30 | +import scala.concurrent.duration._ |
| 31 | + |
| 32 | +object BroadcastHubBenchmark { |
| 33 | + final val OperationsPerInvocation = 100000 |
| 34 | +} |
| 35 | + |
| 36 | +@State(Scope.Benchmark) |
| 37 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 38 | +@BenchmarkMode(Array(Mode.Throughput)) |
| 39 | +class BroadcastHubBenchmark { |
| 40 | + import BroadcastHubBenchmark._ |
| 41 | + |
| 42 | + val config = ConfigFactory.parseString(""" |
| 43 | + pekko.actor.default-dispatcher { |
| 44 | + executor = "fork-join-executor" |
| 45 | + fork-join-executor { |
| 46 | + parallelism-factor = 1 |
| 47 | + } |
| 48 | + } |
| 49 | + """) |
| 50 | + |
| 51 | + implicit val system: ActorSystem = ActorSystem("BroadcastHubBenchmark", config) |
| 52 | + import system.dispatcher |
| 53 | + |
| 54 | + var testSource: Source[java.lang.Integer, NotUsed] = _ |
| 55 | + |
| 56 | + @Param(Array("64", "256")) |
| 57 | + var parallelism = 0 |
| 58 | + |
| 59 | + @Setup |
| 60 | + def setup(): Unit = { |
| 61 | + // eager init of materializer |
| 62 | + SystemMaterializer(system).materializer |
| 63 | + testSource = Source.fromGraph(new BenchTestSource(OperationsPerInvocation)) |
| 64 | + } |
| 65 | + |
| 66 | + @TearDown |
| 67 | + def shutdown(): Unit = { |
| 68 | + Await.result(system.terminate(), 5.seconds) |
| 69 | + } |
| 70 | + |
| 71 | + @Benchmark |
| 72 | + @OperationsPerInvocation(OperationsPerInvocation) |
| 73 | + def broadcast(): Unit = { |
| 74 | + val latch = new CountDownLatch(parallelism) |
| 75 | + val broadcastSink = |
| 76 | + BroadcastHub.sink[java.lang.Integer](bufferSize = parallelism, startAfterNrOfConsumers = parallelism) |
| 77 | + val sink = new LatchSink(OperationsPerInvocation, latch) |
| 78 | + val source = testSource.runWith(broadcastSink) |
| 79 | + var idx = 0 |
| 80 | + while (idx < parallelism) { |
| 81 | + source.runWith(sink) |
| 82 | + idx += 1 |
| 83 | + } |
| 84 | + awaitLatch(latch) |
| 85 | + } |
| 86 | + |
| 87 | + private def awaitLatch(latch: CountDownLatch): Unit = { |
| 88 | + if (!latch.await(30, TimeUnit.SECONDS)) { |
| 89 | + StreamTestKit.printDebugDump(SystemMaterializer(system).materializer.supervisor) |
| 90 | + throw new RuntimeException("Latch didn't complete in time") |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments