Android Volley vs Retrofit | Who is Better?

 



Retrofit and Volley both are the REST client libraries. Before starting the comparison let me first introduce these libraries: –

Volley-: Volley is a networking library it offers great features like  prioritization, ordered requests and of course caching, making multiple requests at the same time, asynchronous requests, synchronous requests;

Retrofit -: Retrofit is a REST client for Android, through which you can make easy to use interfaces which can turn any Android app into a powerful one. Retrofit can perform Async and sync requests with automatic JSON parsing without any effort

Volley or Retrofit?

I won’t lie to you, I prefer Volley over Retrofit. I do however have my reasons for this preference; I know the code for Volley well, it suits my coding/project structure style and you get image handling and lots of other things for free. Retrofit on the other hand (feel free to correct me on this) is more about code abstraction on top of a HTTP client which for Retrofit is OkHttp. Retrofit aims to make it easier to consume RESTful web services where as the goal of Volley is to handle all your networking needs for Android specifically.

  • Volley will handle avoiding duplicate calls, not sure if Retrofit will.
  • Volley provides network image handling support.
  • Volley can integrate with most popular HTTP clients out there that include OkHttp where as Retrofit (as far as I know) relies on OkHttp.
  • Retrofit remains up to date. Volley may be relying on a library bundled with your OS, hence updating the network client isn’t an option.
  • Retrofit makes it much easier to configure HTTP intercepts (if you want to do something before or after an HTTP call).
  • Volley can provide fine grain control over caching strategy, but its harder to configure caching than Retrofit.
  • Retrofit relies on OkHttp, which relies on Okio which effectively makes this library huge compared to a basic configuration of Volley. If size is an issue, Volley is a better bet.

Methodology

  • Since this is about measuring the “speed” of either library. I only measure the time is takes for an asynchronous call to return the raw response.
  • I selected 8 RSS feeds from a local news site measure the average amount of time it took for each library to fetch all 8 RSS feeds from a clean install. In order to account of network latency and other real world factors, I ran each tests 10 times and averaged the results.
  • I measured each library twice to account for any network issues that may have been taking place the first time I ran the 10 tests.
  • All tests were run on the Android Studio 2.0 x86 emulator running API 23 on a Nexus 5.
  • For Volley, the tests were run using OkHttp 3, Apache 4.4.12 and Android platform API (for API 9+ this is URLConnection, for older versions it may be Apache HttpClient 2.0+)
  • POST was testing by creating a stubbed service on my localhost. Each call time was averaged.

The result

  • Retrofit took on average 41ms to fetch the 8 API responses.
  • Retrofit cache responses took an average of 21ms.
  • Retrofit POST with 3 fields took 14ms on average.
  • Volley-native took an average of 50ms.
  • Volley-native cache responses took an average of 12ms.
  • Volley-native POST with 3 fields took 18ms on average.
  • Volley-Apache4 took an average of 48ms.
  • Volley-Apache4 cache responses took an average of 13ms.
  • Volley-Apache4 POST with 3 fields took 15ms on average.
  • Volley-OkHttp3 took an average of 40ms.
  • Volley-OkHttp3 cache responses took an average of 14ms.
  • Volley-OkHttp3 POST with 3 fields took 15ms on average.

Notes:

  • Retrofit cache responses are a lot slower though presumably because by default Retrofit/OkHttp uses disk cache where as Volley probably uses memory cache alongside disk cache.
  • It’s worth pointing out that Volley’s average response times for GET varied greatly compared to Retrofit, some times completing all 8 request with an average time of ~200ms. I’m not sure how to account for this. When I measure each responses individually, some responses would be faster for Volley; others faster for Retrofit but on cumulative average, Retrofit would be faster. Retrofit’s responses were far more consistent, varying only by a max of 10 milliseconds at worst.
  • I wondered of the RSS feed supported SPDY which would account for OkHttp performing better, however as best I can tell, this is not the case.
  • Retrofits performance advantage all but disappeared when using OkHttp with Volley. Volleys faster memory cache probably gives it the upper-hand here.
  • Who is noticing a 10 millisecond difference in the cumulative average of API calls anyways? I can understand that there may be some-very-select apps where this 10 millisecond difference is a big deal, but it’s not even one refresh frame!
  • POST responses varied greatly for everyone (±5ms variance) Volley just tended towards higher numbers. Again, not sure what can explain the variance, I tried to control the environment as strictly as was reasonable.
  • Forking and customising Volley for your needs is a lot easier than doing so with Retrofit.
  • You probably should use Glide for image handling, this effectively negates any advantage Volley has in this area.



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.