I am always a big fan of Restsharp, and have been using it for long time now. Especially recently as most of the projects I am working on involve Rest APIs.
The implementation is straight forward to make a call to an API and it supports Async calls as well.
Couple of days ago, I found myself calling a Rest API with the normal HttpClient
and it crossed my mind about making a small comparison regarding the performance for both of them.
The test is simple as I have a Self host Web API (Owin) that includes one Get
method[test] which returns a number from a static
variable, this number increases by one every time a call is made to the API method.
public static int x = -1;
[Route("test")]
[HttpGet]
public IHttpActionResult Get()
{
x = x + 1;
return Ok(x);
}
API will be called from another console application within the same solution for both of the clients (Restsharp
, HttpClient
) asynchronously for number of times decided in the beginning of the console, the different time will be calculated by stopwatch
object and print the result on the screen. Remember there is an initial request to the API in order to remove the delays occurred when you call the API for the first time.
Where is the Test !!
Ok, let's start the test with only 10 requests for each client and see the result.
As you see, restsharp took a bit longer but I thought that’s not a big difference as it is in milliseconds. At the end of the result, you will see that each time the API responses, the value getting stored in a list and when the test completes, the last item will be displayed in order to make sure that the result is correct. Let's make the test bigger by 500 requests.
Seems the gap is getting bigger now !! Double the time is something big when it comes to performance. What about we make the final round for 5000 requests!
Again, the last round has proven that Restsharp is almost taking double the time. Well to be honest, I was expecting a slight difference in the performance as most the libraries built over the native classes usually takes more time than the original ones for the price of functionalities and features they offer but double the time is something that should be a concern.
Please note that both of the tests are done on 1 PC without any involvement of network factor as the API running from the same machine which runs the Test console APP to guarantee that network factor is not involved in the comparison between the calls for both of the clients. Also, all the calls were done asynchronously.
Conclusion
To be honest, I am still confused. I would love to do more tests whenever I get more time on both of the clients and if you have any ideas which would change the result, please let me know in the comments and I will take it into consideration in my next test.
You can find the complete solution in this link.