Why is response time(for Rest Call) slower in Android when compared to PC?
Asked Answered
L

2

17

I am making a rest api call from Android device and was really surprised looking at the difference of speeds when compared to PC. Below is the image from a rest tool on PC.enter image description here

I tried few libraries like Retrofit, Volley and also regular Async task to make the same rest call from android device and noticed following response times.

(Response times with Retrofit library (Time includes converting json data to java objects)).
Test 1: 8372 Ms
Test 2: 7715 Ms
Test 3: 7686 Ms
Test 4: 10128 Ms
Test 5: 7876 Ms

(Response times with Volley. No conversion to Java Objects from Json Data )
Test 1: 6721 MS
Test 2: 6610 MS
Test 3: 6287 MS
Test 4: 6118 MS
Test 5: 6118 MS

I took System.currentTimeMillis() before making a call and after getting the response and subtracted those values to get above response time in Android program.

It would be really helpful if some one can show me how to reduce the response time in android.

FYI: I am on Wifi and using same network both for my PC and Android device.

Here is the Retrofit Android code

RestAdapter adapter = new RestAdapter.Builder()
                                .setEndpoint(ENDPOINT)
                                .build();

    WeatherApi weatherApi = adapter.create(WeatherApi.class);
    startTime = System.currentTimeMillis();
    weatherApi.getWeather(location.getLatitude(),location.getLongitude(),new Callback<WeatherInfo>() {

        @Override
        public void success(WeatherInfo arg0, Response arg1) {
            endTime = System.currentTimeMillis();
            Log.d("TAG",endTime-startTime + ":Millisecs");
            setWeatherInfo(arg0);
            if(weatherDialog != null && weatherDialog.isShowing())
            weatherDialog.dismiss();
        }
Lactescent answered 21/5, 2015 at 15:51 Comment(10)
Are you sure that you aren't actually measuring the time after processing the result of the call instead of the time to get a response from the server? Maybe posting your code to show where you are making the timings would be helpfulZsazsa
@EdGeorge updated with codeLactescent
Between the start and endtime, there is the process of converting the JSON response to a WeatherInfo object. The times you have given will contain that additional conversion time. How large is the body you get back and can you post the WeatherInfo POJO class you are using?Zsazsa
@EdGeorge WeatherInfo POJO is complicated class but I don't think converting data to Java classes is consuming lot of time. I tried this with volley which don't convert json data to Java objects but the results are quite similar. Please find updated results with VolleyLactescent
It does seem to take a lot of time to process - anything more than a second seems excessive and in one case you had an additional processing time of nearly 4 seconds. Have you tested this on multiple devices?Zsazsa
@EdGeorge Test results you see here are independent of each other. Do not compare those test cases. You are right that time reduced by a second is huge but still the reponse on mobile is almost 10 times higher than the PC. I would like to know the reason for that and any ideas for improving the speed.Lactescent
Use Traceview and determine where your time is being spent.Pirog
no these times are not with debuggingLactescent
A mobile CPU is an awful lot less powerful than a desktop CPU. This will account for some of the difference, especially when using a secure connection, but probably not for all of it. As @Pirog mentioned, TraceView is a good place to see where the time is going.Camphene
Try running a test using org.apache.http.client.HttpClient That is probably a better indicator. I also notice that the content type response on pc is html. Maybe set the content type to application/json when testing on pc.Cute
I
4

Retrofit is agnostic to the HTTP client it uses, so it tries to figure out what is the best available HTTP client it can use to perform the request.

By default Retrofit uses GSON as a converter (that too can be customized). Considering that GSON uses reflection to map JSON elements to your class attributes, the complexity of the WeatherInfo class may very well have a performance penalty on the conversion step.

Since the differences in your case are not quite reasonable, my best guess is that GZIP is disabled. You can use RestAdapter.setLogLevel(LogLevel.FULL) and check the Accept-Encoding header to see if GZIP is enabled or not. If that is not the case then post more relevant code so we can give more insights.

A quick way which I'm sure you'll notice a huge difference is if you include okhttp and okhttp-urlconnection - when those are available in the classpath Retrofit will use okhttp as the client by default, which supports GZIP without any further configuration.

You can do so by adding the following dependencies to your build.gradle file:

compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'

Note that compression also has to be supported on the server-side, and I'm not sure if you've posted the complete output of the response headers, but it seems to be missing something like Content-Encoding: gzip, if that is the case you should look into enabling this feature on your web server as well.

Infiltrate answered 31/5, 2015 at 0:21 Comment(0)
B
1

My guess is your android system is either overloaded or under a debugger or dalvik vm compilation is in effect there .

Just out of the time like 6 seconds it is understood that there is some phisical limitation , like CPU load or SDHC memory access.

Also if you use emulator to debug, this maybe the reason .

Baluchi answered 31/5, 2015 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.