Search code examples
retrofit2okhttp

Unexpected behavior while measuring download time using okhttp eventListener on Android


This question is a continuation of https://github.com/square/okhttp/issues/6991

I hope everyone is fine. I'm using okhttp version 4.3.1. I'm trying to measure the download time for a file. For that I have used the following code:

val okHttpClientBuilder = OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.MINUTES) // connect timeout
                .writeTimeout(5, TimeUnit.MINUTES) // write timeout
                .readTimeout(5, TimeUnit.MINUTES)

                .eventListener(object : EventListener() {
                    override fun callStart(call: Call) {
                        val callTimeEnd = System.currentTimeMillis()
                        Log.v(TAG, "callTimeEnd: $callTimeEnd")
                    }

                    override fun callEnd(call: Call) {
                        val callTimeStart = System.currentTimeMillis()
                        Log.v(TAG, "callTimeEnd: $callTimeEnd")
                    }
                })

It logged the following timestamps:

callStart: 1641622023750

callEnd: 1641622031159

and callEnd - callStart = ~7.4seconds. My device (Android 9.0) has downloading speed of 5 Mega bits per second. File size is 25 Mega Bytes. Ideally, downloading time should be ~40 seconds. Am I missing something or do the library is doing something under the hood?

Moreover, another device (Android 8.0) with downloading speed of 20 Mega bits per second is downloading the same file in ~ 10 seconds i.e., taking longer time than Android 9.0 device. Any pointer will be appreciated.


Solution

  • The issue was with the network to which the devices were connected. I had applied rate limits on WAN ports, but the server was connected to a router through ethernet and clients were connected via Wifi, therefore, the file was being served to clients using local network which operates on a high bandwidth i.e., router was operating on a 150 Mbps, slow mobile was 65 Mbps, and fast was 144 Mbps which turns out the time to download for slow and fast was ~5 seconds and ~3 seconds respectively. I moved the server to WAN port and it provided the desired results i.e., slow device rate limit set to 5 Mbps and it downloaded the 25 Mega bytes file in ~43 seconds.