Is there a simple way to get the response time for a request to a url (other than keeping track of time in the code separately)?
import dispatch._, Defaults._
import scala.util.{Failure, Success}
val svc = dispatch.url(url)
val response: Future[com.ning.http.client.Response] = Http(svc > (x => x))
response onComplete {
case Success(content) => {
println(s"SUCCESS: ${content.getStatusCode()} for $url")
//how long did this take??
}
case Failure(t) => {
println(s"ERROR: timeout/failure for $url")
}
}
This is a different approach for computing the elapsed time, might need a bit more customization (like handling of failure,
NonFatalmost probably), but I think this is a bit more idiomatic. It works only withFutures though.First create a class for the result with time (could be replaced with a tuple too) and one for the failure with time.
Then create a method that wraps the future to execute and handles the case when it fails (might use
Promiseif you do not like theFuture'smapandrecoverWithmethods (as they require an implicitExecutionContext)).Here is an example how to use it: