Last active
December 15, 2017 13:59
-
-
Save tg44/57664b2a434a1e5f4d571116bd6b87fe to your computer and use it in GitHub Desktop.
ResponseTimeLoggerActor
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
object ResponseTimeLoggerActor { | |
case object LogMeasures | |
} | |
trait ResponseTimeLoggerActor { | |
x: Actor => | |
implicit val executionContext: ExecutionContextExecutor | |
val logger: Logger | |
val autoSchedule = true | |
val autoScheduleTime = 5 seconds | |
var min: Long = 0 | |
var max: Long = 0 | |
var count : Long = 0 | |
var averageResponse: Double = 0.0 | |
var firstMeasure: Long = 0 | |
abstract override def aroundReceive(receive: Receive, msg: Any): Unit = { | |
val now = System.currentTimeMillis() | |
if(firstMeasure == 0 && autoSchedule) { | |
firstMeasure = now | |
x.context.system.scheduler.scheduleOnce(autoScheduleTime, self, LogMeasures) | |
} | |
msg match { | |
case LogMeasures => | |
logger.info(s"Actor response times ($firstMeasure - $now): Min: $min, Max: $max, count: $count, avgResponseTime: $averageResponse") | |
min = 0 | |
max = 0 | |
count = 0 | |
averageResponse = 0 | |
firstMeasure = now | |
x.context.system.scheduler.scheduleOnce(autoScheduleTime, self, LogMeasures) | |
case _ => | |
} | |
receive.apply(msg) | |
val runtime = System.currentTimeMillis() - now | |
averageResponse = (averageResponse * count + runtime) / (count + 1) | |
count = count + 1 | |
min = Math.min(min, runtime) | |
max = Math.max(max, runtime) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment