While diving into the landscape of Android HTTP libraries as a task to make Bolina more developer-friendly, 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.
Therefore, I decided to test and compare Android HTTP Libraries, starting by evaluating the transfer speeds OkHttp achieves and compare them with the results of Android's default library, HttpURLConnection. This should be interesting to share with you, so here you have some insights on OkHttp vs. HttpURLConnection.
For the intended comparison, I used two 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 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 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.
The full code used for the comparison is available in Github, feel free to adapt it to your specific needs.
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.