Created
June 28, 2013 16:15
-
-
Save edofic/5885932 to your computer and use it in GitHub Desktop.
akka speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import concurrent.Await | |
import concurrent.duration._ | |
import akka.util.Timeout | |
import akka.pattern.ask | |
import concurrent.Future | |
import concurrent.ExecutionContext.Implicits.global | |
def time(n:Int)(block: => Unit) = { | |
var i = 0 | |
val start = System.currentTimeMillis() | |
while(i<n){ | |
block | |
i+=1 | |
} | |
val took = System.currentTimeMillis() - start | |
val per = took.toFloat / n | |
(took, per, 1/per) | |
} | |
implicit val timeout = Timeout(3.seconds) | |
def get(msg: Any) = Await.result(echo ? msg, 3.seconds) | |
def get(n: Int, msg: Any) = Await.result(Future.sequence(Seq.fill(n)( echo ? msg )), 10.seconds) | |
class Counter(limit: Int) extends Actor { | |
var time = 0l | |
var last: Any = null | |
var count = 0 | |
def receive = { | |
case msg => | |
if(msg == last){ | |
count += 1 | |
if(count > limit){ | |
val cur = System.currentTimeMillis() | |
val took = cur - time | |
val thru = count.toFloat / took | |
println(s"Processed $count in $took; throughput: $thru") | |
count = 0 | |
time = System.currentTimeMillis() | |
} | |
} else { | |
last = msg | |
time = System.currentTimeMillis() | |
} | |
} | |
} | |
class Echo extends Actor { | |
def receive = { | |
case msg => sender ! msg | |
} | |
} | |
def send(actor: ActorRef, n: Int, msg: Any){ | |
var i = 0 | |
while(i<n){ | |
actor ! msg | |
i+=1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment