Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine the number of concurrent requests from an async httpclient during execution?

I have a simple console application which looks like this:

    private static StringBuilder sb = new StringBuilder();
    private static HttpClient client = new HttpClient();

    private static async Task<HttpStatusCode> AccessTheWebAsync()
    {            
        HttpResponseMessage response = await client.GetAsync("http://www.google.com").ConfigureAwait(false);
        return response.StatusCode;
    }

    static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        List<Task> tasks = new List<Task>();
        for (int i = 0; i < 10; i++)
            tasks.Add(AccessTheWebAsync());
        Task.WaitAll(tasks.ToArray());

        foreach (Task<HttpStatusCode> t in tasks)
            sb.Append((int)t.Result).Append(Environment.NewLine);
        Console.WriteLine(sb.ToString());            
        sw.Stop();
        Console.WriteLine("Run Completed, Time elapsed: {0}", sw.Elapsed);
        Console.ReadLine();
    }

Here I initiate 10 async web requests and collect the response codes when the requests are completed and list them.

  1. Question: Is it possible to use VS2012 to debug the app in such a way where I can determine the number of concurrent web requests that are happening at any given point during execution?

Reason being is I found out there's something you can change on the App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.net>
    <connectionManagement>
      <add address = "*" maxconnection = "10" />
    </connectionManagement>
  </system.net>
</configuration>

But, I've not a good way to determine if this is actually working or not.

  1. Do servers set this limit?

  2. Does the OS set this limit?

  3. Does this app config and/or .NET set this limit?

like image 521
user17753 Avatar asked Oct 22 '12 14:10

user17753


1 Answers

Is it possible to use VS2012 to debug the app in such a way where I can determine the number of concurrent web requests that are happening at any given point during execution?

Yes, surely. Use Parallel Tasks and Parallel Stacks windows, they are very good.

  1. Do servers set this limit?

  2. Does the OS set this limit?

  3. Does this app config and/or .NET set this limit?

The setting specifies the maximum number of connections to a network host and can be set on the machine or application level. A server could set a connection limit for the clients as well but that's not related to maxconnection attribute.

In your example the actual maximum number of connections can also be limited by thread pool settings. Have a look at this KB entry for more details.

like image 74
Serge Belov Avatar answered Oct 05 '22 02:10

Serge Belov