pekko/akka-bench-jmh/src/main/scala/akka/stream/MapAsyncBenchmark.scala

97 lines
2.4 KiB
Scala
Raw Normal View History

/*
* Copyright (C) 2014-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.stream
2019-09-05 16:08:37 +02:00
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import akka.NotUsed
import akka.actor.ActorSystem
2019-09-05 16:08:37 +02:00
import akka.remote.artery.BenchTestSource
import akka.remote.artery.LatchSink
import akka.stream.scaladsl._
import akka.stream.testkit.scaladsl.StreamTestKit
import com.typesafe.config.ConfigFactory
import org.openjdk.jmh.annotations._
import scala.concurrent.duration._
2019-09-05 16:08:37 +02:00
import scala.concurrent.Await
import scala.concurrent.Future
object MapAsyncBenchmark {
final val OperationsPerInvocation = 100000
}
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
@BenchmarkMode(Array(Mode.Throughput))
class MapAsyncBenchmark {
import MapAsyncBenchmark._
2019-03-11 10:38:24 +01:00
val config = ConfigFactory.parseString("""
akka.actor.default-dispatcher {
executor = "fork-join-executor"
fork-join-executor {
parallelism-factor = 1
}
}
2019-03-11 10:38:24 +01:00
""")
implicit val system: ActorSystem = ActorSystem("MapAsyncBenchmark", config)
import system.dispatcher
var testSource: Source[java.lang.Integer, NotUsed] = _
@Param(Array("1", "4"))
var parallelism = 0
@Param(Array("false", "true"))
var spawn = false
@Setup
def setup(): Unit = {
// eager init of materializer
SystemMaterializer(system).materializer
testSource = Source.fromGraph(new BenchTestSource(OperationsPerInvocation))
}
@TearDown
def shutdown(): Unit = {
Await.result(system.terminate(), 5.seconds)
}
@Benchmark
@OperationsPerInvocation(OperationsPerInvocation)
def mapAsync(): Unit = {
val latch = new CountDownLatch(1)
testSource
.mapAsync(parallelism)(elem => if (spawn) Future(elem) else Future.successful(elem))
.runWith(new LatchSink(OperationsPerInvocation, latch))
awaitLatch(latch)
}
@Benchmark
@OperationsPerInvocation(OperationsPerInvocation)
def mapAsyncUnordered(): Unit = {
val latch = new CountDownLatch(1)
testSource
.mapAsyncUnordered(parallelism)(elem => if (spawn) Future(elem) else Future.successful(elem))
.runWith(new LatchSink(OperationsPerInvocation, latch))
awaitLatch(latch)
}
private def awaitLatch(latch: CountDownLatch): Unit = {
if (!latch.await(30, TimeUnit.SECONDS)) {
2019-09-05 16:08:37 +02:00
StreamTestKit.printDebugDump(SystemMaterializer(system).materializer.supervisor)
throw new RuntimeException("Latch didn't complete in time")
}
}
}