I started caveman profiling network request code and found out that some APIs that should be about 3s took around 8s instead. After scattering a lot of println() around I reached to the point where between:
mOkHttpClient.newCall(request).enqueue(okcallback);
and
public void onResponse(Call call, okhttp3.Response response) {
System.out.println("GGGN in 1");
7 seconds elapsed in between, suggesting okhttp3 is the culprit. After reading some other answers I stumbled upon the Events wiki page which contains an EventListener example to debug events in the http pipeline. Here is the output:
18:36:54.389 GGGH 0007 https://someurl/
18:36:54.390 GGGH 0007 0,000 callStart
18:36:54.390 GGGN 5
18:36:54.392 GGGH 0007 0,002 connectionAcquired
18:36:54.392 GGGH 0007 0,002 requestHeadersStart
18:36:54.392 GGGH 0007 0,003 requestHeadersEnd
18:36:54.392 GGGH 0007 0,003 requestBodyStart
18:36:54.393 GGGH 0007 0,003 requestBodyEnd
18:36:54.393 GGGH 0007 0,003 responseHeadersStart
18:37:01.680 GGGH 0007 7,291 responseHeadersEnd
18:37:01.681 GGGH 0007 7,291 responseBodyStart
18:37:01.682 GGGN in 1
18:37:01.682 GGGN in 2
18:37:01.683 GGGN in 3
18:37:01.683 GGGQuery 2
18:37:01.688 GGGQuery 3
18:37:01.689 GGGN in 4
18:37:01.689 GGG callback 4
18:37:01.690 GGG callback 5
18:37:01.715 GGG callback 6
18:37:01.715 GGGH 0007 7,325 responseBodyEnd
18:37:01.715 GGGH 0007 7,326 connectionReleased
18:37:01.715 GGGH 0007 7,326 callEnd
It seems that the client spends around 7s between responseHeadersStart and responseHeadersEnd. However, when I run the same query from a mac on the same network, I get 2.03s for the request.
Why is the difference so big, and can I do something to make the mobile go faster? I tried the suggestions from other questions like this, but I'm already doing all mentioned there.
I was trying this on a low end Xiaomi Redmi3S, but then I repeated the tests on a Xiaomi Mi6, a faster device, and ironically the test there runs 1s slower! Mobile APK is compiled in release mode with proguard, so I build, run, then fetch logs with grep to avoid adding any slowdown to the devices. OkHttp3 version is 3.12.1. Any ideas?