Click here to Skip to main content
15,881,852 members
Articles / All Topics

Restsharp Vs HttpClient Performance Benchmark

Rate me:
Please Sign up or sign in to vote.
4.31/5 (7 votes)
20 Oct 2016CPOL3 min read 44.9K   2   6
Restsharp Vs HttpClient Performance Benchmark

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.

C#
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.

Screenshot (31).png

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.

Screenshot (28).png

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!

Screenshot (30)

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.

Image 4 Image 5

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Arab Emirates United Arab Emirates
Genius is one percent inspiration and ninety-nine percent perspiration.

Comments and Discussions

 
QuestionPossibly this is the reason? Pin
Member 1158520023-Oct-17 2:39
Member 1158520023-Oct-17 2:39 
QuestionThis test is probably seriously flawed somewhere Pin
tyzh10-Jun-17 5:04
tyzh10-Jun-17 5:04 
SuggestionTests PinPopular
Graeme_Grant20-Oct-16 16:10
mvaGraeme_Grant20-Oct-16 16:10 
GeneralRe: Tests Pin
Amgad Fahmi20-Oct-16 21:05
Amgad Fahmi20-Oct-16 21:05 
That's great Grant, thanks for taking the time to read the post.
Well I see urs are performing pretty good, also adding the factor of one time initialization is good but it covers some scenarios not all as in my case there will be calls made from other REST API (API to API) which is stateless environment and every time a request is made a new instance have to be there so yeah.
But I agree that case should have been covered in the article to cover the different scenario. Thanks again
Genius is one percent inspiration and ninety-nine percent perspiration.

GeneralRe: Tests Pin
Graeme_Grant21-Oct-16 13:19
mvaGraeme_Grant21-Oct-16 13:19 
GeneralRe: Tests Pin
Amgad Fahmi22-Oct-16 0:31
Amgad Fahmi22-Oct-16 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.