6

I have made a console application which basically performs large amount of requests towards my server. 10-15000 requests per user.

So I've written this code which uses .NET's HTTP Client library:

public async Task<string> DoRequest(string token)
{
var request = new HttpRequestMessage(HttpMethod.Post, "mysite.com");
string requestXML = "my xml request goes her...";
request.Content = new StringContent(requestXML, Encoding.UTF8, "text/xml");
var response = await httpClient.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}

So now I'm trying to perform as much as HTTP requests that I can per 1 second... I don't know whats the upper limit of this so I've used parallel for loop to speed this:

Parallel.For(0,list.Count(),items=>{
DoRequest(item.Token);
});

But I'm not very happy with the speed of the code and how the requests are being made...

Is there any way that I can speed things up n do maybe up to 10-100 requests per 1 second ? Or what is the maximum limit ?

Can someone help me out ?

P.S. guys I created only one instance of httpclient class because I read that it's a good practice to make only one since each time a new object of this class is made the connection gets closed, which is not what we want no ?

asked Mar 21, 2017 at 18:11
3
  • 3
    Your test might not be particularly useful. For example, HttpClient is restricted to the number of connections to the same server at any one time (though you can change that with ServicePointManager.DefaultConnectionLimit) Commented Mar 21, 2017 at 18:14
  • @DavidG can you show me exactly how to do this and where? In this same method or ? Commented Mar 21, 2017 at 18:15
  • 1
    ServicePointManager.DefaultConnectionLimit is a property @User987 just set it. Commented Mar 21, 2017 at 18:34

1 Answer 1

6

Is there any way that I can speed things up n do maybe up to 10-100 requests per 1 second?

That's an unanswerable question.

Or what is the maximum limit?

As David noted, you need to set ServicePointManager.DefaultConnectionLimit. This should suffice as the first line in your Main method:

ServicePointManager.DefaultConnectionLimit = int.MaxValue;

I've used parallel for loop to speed this

That's the incorrect tool for this job. You do want concurrency, but not parallelism. Asynchronous concurrency is expressed using Task.WhenAll:

var tasks = list.Select(item => DoRequest(item.Token));
await Task.WhenAll(tasks);

I created only one instance of httpclient class because I read that it's a good practice to make only one since each time a new object of this class is made the connection gets closed, which is not what we want no?

That is correct.

answered Mar 21, 2017 at 19:38
7
  • Stephen, and how would I actually load the results of Task.WhenAll(tasks) ? Inside each of task theres an XML response. How would I load them into and XMLDocument then ? Commented Mar 21, 2017 at 21:07
  • @User987 Chain continuation tasks to them. Have the continuation read the XML and do whatever you want with it after that. Commented Mar 22, 2017 at 11:43
  • @User987: var results = await Task.WhenAll(tasks); will get you a results variable which is an array of XML responses. Commented Mar 22, 2017 at 13:27
  • @Stephen Cleary How would you do this but limit the number of concurrent connections? Commented Mar 25, 2017 at 0:11
  • You can use DefaultConnectionLimit or SemaphoreSlim (there are several examples on SO). Commented Mar 25, 2017 at 16:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.