source: doc/theses/colby_parsons_MMath/benchmarks/actors/akka/SendStatic/AkkaSendStatic.scala

Last change on this file was f945fa7, checked in by Peter A. Buhr <pabuhr@…>, 13 hours ago

fix spelling mistake in directory name

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