How to calculate total response time of an HTTP request using AsyncHttpClient library

548 Views Asked by At

I am looking into using AsyncHttpClient (AHC) to invoke multiple HTTP requests in parallel and calculate total response time as well as TCP connection time, DNS resolution time, time to first byte etc. of each of these async requests.

AsyncHttpClient is the only Java client I came across which emits events for TCP connection time, DNS resolution time etc.

My question is: What is the correct way to measure the start time for the HTTP request so that I can calculate different performance metrics based on that.

Is "onRequestSend" correct event to consider for start time. I am looking for an event which indicates the start of the HTTP lifecycle phase i.e creating a socket on client side to open a connection.

Documentation: https://www.javadoc.io/doc/org.asynchttpclient/async-http-client/latest/org/asynchttpclient/AsyncHandler.html

2

There are 2 best solutions below

0
Antoniossss On

I would say it is like

long t0=System.currentTimeMilis();

doYourHttpCallAndProvideCallback(response->{
    long total=System.currentTimeMIlis()-t0;
    souf("It took %dms to do the request\n",total);
})
2
tornadoradon On

you can use Kotlin standard library:

    val mark = TimeSource.Monotonic.markNow() // Returned `TimeMark` is inline class
    val elapsedDuration = mark.elapsedNow()

Look at the official documentation for more details.