Async / performance expectation

I am working on optimizing the performance of a program that makes extensive use of the async / await function. Generally speaking, it downloads thousands of json documents via HTTP in parallel, parses them, and generates some response using this data. We are experiencing some performance issues, when we process many requests at the same time (for example, loading 1000 jsons), we can see that a simple HTTP request can take several minutes.

I wrote a small console application to test it with a simplified example:

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < 100000; i++)
        {
            Task.Run(IoBoundWork);
        }

        Console.ReadKey();
    }

    private static async Task IoBoundWork()
    {
        var sw = Stopwatch.StartNew();

        await Task.Delay(1000);

        Console.WriteLine(sw.Elapsed);
    }
}

And I see a similar behavior here:

enter image description here

The question is why β€œwaiting for Task.Delay (1000)” ultimately takes 23 seconds.

+6
2

Task.Delay , 100 000 , . Console.WriteLine, . , , .

Console.WriteLine, . , , , - . Console.WriteLine 1,16 , :

using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        ThreadPool.SetMinThreads(50000, 50000);
        var tasks = Enumerable.Repeat(0, 100000)
            .Select(_ => Task.Run(IoBoundWork))
            .ToArray();
        Task.WaitAll(tasks);
        var maxTime = tasks.Max(t => t.Result);
        Console.WriteLine($"Max: {maxTime}");
    }

    private static async Task<double> IoBoundWork()
    {
        var sw = Stopwatch.StartNew();
        await Task.Delay(1000);
        return sw.Elapsed.TotalSeconds;
    }
}

IoBoundWork . :

  • ( - "" , )
  • ( , CPU )
  • -, ( , , , , , ..).
  • - , Console.WriteLine
  • IO (await foo.WriteAsync(...) ..)

Task.Delay(1000) . , , , , Task.Yield Task.Delay. , "", - 100 000 200 000.

. , 100 000 , , -. , async/await , " 100 000 , , ".

+13

- , async-await - .

async-await - . , HTTP- ..

async-await , , .

HTTP- , , .

0

All Articles