Blog

OkHTTP vs. HttpURLConnection Performance: Comparing Android HTTP Libraries

While diving into the landscape of Android’s HTTP libraries, I concluded that OkHTTP was my favorite, due to its full features set and developer friendly API. However, the performance regarding transfer speeds that each library is capable of achieving is more or as important as its features. It doesn’t make sense to prioritize application maintenance simplicity over application performance, especially when we know that delays in serving content cost you money.

Learn here how to integrate HttpURLConnection

Therefore, I decided to evaluate the transfer speeds OkHTTP achieves and compare them against the results of Android’s default library, HttpURLConnection. This should be interesting to share with you!

Test Environment

For the intended comparison, I used 2 setups:

  • Android emulator, with a Nexus 5X API 28 x86 image, running on a 13-inch MacBook Pro 2017, with an i5 2.3GHz CPU and 8GB of RAM
  • OnePlus 5, with Android 9.0

Both the OnePlus 5 and MacBook Pro are associated to a 5GHz Wi-Fi network, connected to the Internet via a 1Gbps connection.

Let’s Measure It!

To measure the performance of each HTTP library, I created a simple test application, that after app launch, perform a download of a small file (1.1MB) or a big file (47MB) from an Ookla’s speedtest.net server. For that purpose, and given that I am located in Portugal, I selected a server also located in Portugal.

The following tables present the achieved transfer times. Notice that the presented results correspond to the average of 30 measurement samples and all times are in milliseconds.

Table 1 - Small File (times in milliseconds)
  OkHTTP  HttpURLConnection 
Emulator 406 405
OnePlus 116  111

 

Table 1 refers to the transfer times of a small 1.1MB file. Results show that OkHTTP achieves identical performance when compared with HttpURLConnection on the emulator.

Table 2 - Big File (times in milliseconds)
  OkHTTP  HttpURLConnection 
Emulator 7002 7510
OnePlus 1594 1680

 

Table 2 refers to the transfer times of a big 47MB file. Results show that OkHTTP achieves slightly better performance when compared with HttpURLConnection for transfers of larger files, by transferring the same file almost 100ms faster on a smartphone and 500ms faster in the emulator.

The results are somewhat expected given that both libraries actually depend on the performance of TCP, the underlying protocol of HTTP/1.1 and HTTP/2. Nevertheless, OkHTTP is capable of achieving a small performance boost for large files.

Full Code Used for Testing

The full code used for the comparison is available in Github, feel free to adapt it to your specific needs.

Final Thoughts

Not only is OkHTTP feature-rich, but it is also capable of providing a small performance boost when compared with HttpURLConnection. More and more reasons to choose OkHTTP for your next Android project - or even for your next major project update.

Tagged Android, HTTP Libs