| 1 | import akka.actor.typed.scaladsl.Behaviors
|
|---|
| 2 | import akka.actor.typed.scaladsl.LoggerOps
|
|---|
| 3 | import akka.actor.typed.{ ActorRef, ActorSystem, Behavior }
|
|---|
| 4 | import java.util.concurrent.atomic.AtomicInteger
|
|---|
| 5 | import akka.event.Logging
|
|---|
| 6 | import akka.actor.typed.DispatcherSelector
|
|---|
| 7 | import java.util.concurrent.Semaphore
|
|---|
| 8 |
|
|---|
| 9 | object DynamicActor {
|
|---|
| 10 | final case class Dummy( cnt: Int )
|
|---|
| 11 |
|
|---|
| 12 | def apply(): Behavior[Dummy] = Behaviors.receive { (context, message) =>
|
|---|
| 13 | if ( message.cnt >= DynamicMain.times ) {
|
|---|
| 14 | if ( DynamicMain.trials.get() > 0 ) { // ignore trial 1
|
|---|
| 15 | println( s"${(System.nanoTime() - DynamicMain.startTime) / DynamicMain.times}" )
|
|---|
| 16 | // println( s"${DynamicMain.times} ${(System.nanoTime() - DynamicMain.startTime) * 1E-9}s" )
|
|---|
| 17 | } // if
|
|---|
| 18 | if ( DynamicMain.trials.incrementAndGet() == DynamicMain.NumTrials + 1 ) {
|
|---|
| 19 | DynamicMain.system ! DynamicMain.Stop()
|
|---|
| 20 | } else {
|
|---|
| 21 | DynamicMain.startTime = System.nanoTime() // reset for next trial
|
|---|
| 22 | context.self ! Dummy( 0 )
|
|---|
| 23 | } // if
|
|---|
| 24 | Behaviors.same
|
|---|
| 25 | } else {
|
|---|
| 26 | DynamicMain.system ! DynamicMain.Start( message.cnt + 1 )
|
|---|
| 27 | Behaviors.stopped
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | object DynamicMain {
|
|---|
| 33 | sealed trait MainMessages
|
|---|
| 34 | final case class Start( cnt: Int ) extends MainMessages
|
|---|
| 35 | final case class Stop() extends MainMessages
|
|---|
| 36 |
|
|---|
| 37 | val trials = new AtomicInteger(0)
|
|---|
| 38 | val sem = new Semaphore(0)
|
|---|
| 39 |
|
|---|
| 40 | var actor_cnt = 0;
|
|---|
| 41 | var system : ActorSystem[MainMessages] = null
|
|---|
| 42 | var startTime = System.nanoTime()
|
|---|
| 43 | var times = 2_000_000; var NumTrials = 5
|
|---|
| 44 |
|
|---|
| 45 | def apply(): Behavior[MainMessages] = Behaviors.setup { context =>
|
|---|
| 46 | Behaviors.receiveMessage { message =>
|
|---|
| 47 | message match {
|
|---|
| 48 | case Start( cnt: Int ) =>
|
|---|
| 49 | val actor = context.spawn(DynamicActor(), "actor_" + actor_cnt , DispatcherSelector.fromConfig("akka.dispatcher"))
|
|---|
| 50 | actor_cnt += 1
|
|---|
| 51 | actor ! DynamicActor.Dummy( cnt )
|
|---|
| 52 | Behaviors.same
|
|---|
| 53 | case Stop() =>
|
|---|
| 54 | sem.release()
|
|---|
| 55 | Behaviors.stopped
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | def main( args: Array[String] ): Unit = {
|
|---|
| 61 | def usage() = {
|
|---|
| 62 | println( "Usage: [ times (> 0) ]" );
|
|---|
| 63 | System.exit( 0 );
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | args.length match {
|
|---|
| 67 | case 2 =>
|
|---|
| 68 | if ( args(1) != "d" ) { NumTrials = args(1).toInt }
|
|---|
| 69 | if ( args(0) != "d" ) { times = args(0).toInt }
|
|---|
| 70 | if ( times < 1 ) usage()
|
|---|
| 71 | case 1 =>
|
|---|
| 72 | if ( args(0) != "d" ) { times = args(0).toInt }
|
|---|
| 73 | if ( times < 1 ) usage()
|
|---|
| 74 | case 0 =>
|
|---|
| 75 | case _ =>
|
|---|
| 76 | usage()
|
|---|
| 77 | } // match
|
|---|
| 78 |
|
|---|
| 79 | system = ActorSystem( DynamicMain(), "Dynamic" )
|
|---|
| 80 | system ! Start( 0 )
|
|---|
| 81 | sem.acquire()
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // Local Variables: //
|
|---|
| 86 | // tab-width: 4 //
|
|---|
| 87 | // mode: java //
|
|---|
| 88 | // compile-command: "sbt --warn -J-Xmx32g \"run\"" //
|
|---|
| 89 | // End: //
|
|---|