Skip to content

Instantly share code, notes, and snippets.

@sameei
Created June 14, 2018 13:20
Show Gist options
  • Save sameei/cdb28c0792d7fc7f3ff84531c27d2c89 to your computer and use it in GitHub Desktop.
Save sameei/cdb28c0792d7fc7f3ff84531c27d2c89 to your computer and use it in GitHub Desktop.
A Simple Benchmark; Broadcast Messages to Akka Actors and Wait for Response.
name := "akka-bench"
version := "0.1-SNAPSHOT"
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.5.13"
scalaVersion := "2.12.6"
fork := true
import akka.actor._
import scala.concurrent._
import duration._
import com.typesafe.config._
object Main {
def main(args: Array[String]): Unit = {
val config = ConfigFactory.load()
val system = ActorSystem("bench", config)
println("Creating ...")
val beforeCreate = now
for { i <- 1 to 2000000 } {
system.actorOf(worker(i),s"W-${i}")
}
val afterCreate = now
println(s"Creating: ${afterCreate - beforeCreate}")
system.actorOf(broadcaster(1, 1, 2000000, 1000), "BRD")
Await.result(system.whenTerminated, Duration.Inf)
}
case object Hi
case class Count(howMuch: Int)
case object Done
def now = System.currentTimeMillis
def worker(num: Int) = Props { new Worker(s"W-${num}", num) }
class Worker(name: String, num: Int) extends Actor {
var internal: Int = 0
override def receive: Receive = {
case Hi => sender ! Hi
case Count(howMuch) =>
for ( i <- 0 to howMuch ) internal += 1
sender ! Done
}
}
def broadcaster(from: Int, step: Int, to: Int, work: Int) = Props { new Broadcaster(from, step, to, work) }
class Broadcaster(from: Int, step: Int, to: Int, work: Int) extends Actor {
val count: Int = ((to - from) / step)+ 1
var done: Int = 0
var beforeSend = 0l
var afterSend = 0l
var firstDone = 0l
var lastDone = 0l
override def preStart(): Unit = {
println(s"Broadcaster, Ref: ${self}, From: ${from}, Step: ${step}, To: ${to}")
beforeSend = now
from.to(to).by(step).foreach { i =>
context.system.actorSelection(s"/user/W-${i}") ! Count(work)
}
afterSend = now
println(s"Send ${count} Messages via ActorSelection in ${afterSend - beforeSend}")
}
override def postStop(): Unit = {
println("="*100)
dump
println("="*100)
println("STOP")
context.system.terminate
}
override def receive : Receive = {
case Done if done == count =>
context stop self
case Done if done == 0 =>
firstDone = now
lastDone = firstDone
done += 1
println("First Done")
case Done =>
lastDone = now
done += 1
if (done % 1000 == 0) { println("="*100); dump }
if (done == count) self ! Done
}
def dump = {
println(s"Done: ${done}/${count}")
println(s"Sending: ${afterSend - beforeSend}")
println(s"Delay from AfterSend to FirstDone: ${firstDone - afterSend}")
println(s"Receiveing: ${lastDone - firstDone}")
println(s"Overall: ${lastDone - beforeSend}")
}
}
}
[info] Creating ...
[info] Creating: 7050
[info] Broadcaster, Ref: Actor[akka://bench/user/BRD#-109225259], From: 1, Step: 200, To: 2000000
[info] Send 10000 Messages via ActorSelection in 92
[info] Done: 10000/10000
[info] Sending: 92
[info] Delay from AfterSend to FirstDone: 0
[info] Receiveing: 17
[info] Overall: 109
===================================================================================================
[info] Creating ...
[info] Creating: 7450
[info] Broadcaster, Ref: Actor[akka://bench/user/BRD#-109225259], From: 1, Step: 10, To: 2000000
[info] Send 10000 Messages via ActorSelection in 695
[info] Done: 200000/200000
[info] Sending: 695
[info] Delay from AfterSend to FirstDone: 0
[info] Receiveing: 156
[info] Overall: 851
===================================================================================================
[info] Creating ...
[info] Creating: 7659
[info] Broadcaster, Ref: Actor[akka://bench/user/BRD#-109225259], From: 1, Step: 1, To: 2000000
[info] Send 10000 Messages via ActorSelection in 5307
[info] Done: 2000000/2000000
[info] Sending: 5307
[info] Delay from AfterSend to FirstDone: 0
[info] Receiveing: 1202
[info] Overall: 6509
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment