Skip to content

Instantly share code, notes, and snippets.

@edofic
Created June 28, 2013 16:15
Show Gist options
  • Save edofic/5885932 to your computer and use it in GitHub Desktop.
Save edofic/5885932 to your computer and use it in GitHub Desktop.
akka speed
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