JakeWharton / hugo

Annotation-triggered method call logging for your debug builds.
Apache License 2.0
7.92k stars 797 forks source link

Unable to log execution time (including response time) for method making an API request #118

Closed rohan2817 closed 8 years ago

rohan2817 commented 8 years ago

I was trying to log method execution time for a method which makes an API request using Retrofit. I wanted it to include the response time taken by the request into method execution time. Is there any workaround for this ?

@DebugLog
private void makeAPIRequest(){
apiService.getSomething().enqueue(new Callback<APIResponse>() {
          @Override
          public void onResponse(Response<APIResponse> response, Retrofit retrofit) {
                           //Do something
          }

          @Override
          public void onFailure(Throwable t) {
                           //Do something
           }
     });
}
JakeWharton commented 8 years ago

Hugo logs only the time it takes for a method to execute. Because Retrofit uses background threads to asynchronously handle the HTTP call the enclosing method returns very quickly. For anything like this, not just Retrofit, you need to handle measuring timing yourself or use some other means. Retrofit, for example, can use OkHttp which has a logging-interceptor that can log the time between request and response.

MerlinYu commented 7 years ago

@JakeWharton I want to calculate the retrofit request time and I find there are two headers exist: "OkHttp-Sent-Millis", "OkHttp-Received-Millis" I want to use it ,but sometimes it's gone .Do you have some idears?

shivam05bhardwaj commented 5 years ago

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()