Raghul Sugathan
Raghul Sugathan

Reputation: 370

How to get API Request/ Response time using Retrofit 2

I am using Retrofit 2.1.0 for API parsing in my android application. I need the time taken by retrofit to parse the API.

How to obtain the Request/ response time using Retrofit 2.

Below is the Retrofit Rest Client Call I am using for API parsing.

public static class ServiceGenerated {
    static OkHttpClient httpClient = new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request.Builder ongoing = chain.request().newBuilder();
                    return chain.proceed(ongoing.build());
                }
            })
            .build();


    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(RetrofitUtils.API_BASE_URL)
                    .client(httpClient)
                    .addConverterFactory(GsonConverterFactory.create());


    public static <S> S createService(Class<S> serviceClass) {
        Retrofit retrofit = builder.client(httpClient).build();
        return retrofit.create(serviceClass);
    }

}

Upvotes: 11

Views: 17978

Answers (4)

Mkhytar Mkhoian
Mkhytar Mkhoian

Reputation: 66

You need to look at EventListener from okhttp lib https://square.github.io/okhttp/features/events/

Upvotes: 1

Shivam Bhardwaj
Shivam Bhardwaj

Reputation: 171

It's easy you can find the receivedResponseAtMillis() and the sendResponseAtMillis() in the raw() part of the response and then you can calculate the difference

response.raw().receivedResponseAtMillis()
response.raw().sendResponseAtMillis()

Upvotes: 17

Rakesh
Rakesh

Reputation: 4334

You can calculate the total round trip time by substracting the timestamp when response is received and timestamp when the request is sent. These two methods from the Response object will give you these two values

  1. Response.sentRequestAtMillis()
  2. Response.receivedResponseAtMillis()

        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        long tx = response.sentRequestAtMillis();
        long rx = response.receivedResponseAtMillis();
        System.out.println("response time : "+(rx - tx)+" ms");
    

Upvotes: 27

darwin
darwin

Reputation: 1594

Try this

   if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient.addInterceptor(httpLoggingInterceptor);
    }

also add

compile 'com.squareup.okhttp3:logging-interceptor:3.2.0' 

to your app module gradle file

Sample response:

Date: Fri, 12 Aug 2016 07:33:23 GMT
OkHttp-Sent-Millis: 1470987074283
OkHttp-Received-Millis: 1470987074422

Upvotes: 5

Related Questions