How to measure performance of awaiting asynchronous operations?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 08:39:06

问题


I have a Windows Service that reads from multiple MessageQueue instances. Those messagequeues all run their own Task for reading messages. Normally, after reading a message, the work of an I/O database is done. I've found articles claiming it's a good idea to use async on I/O operations, because it would free up threads. I'm trying to simulate the performance boost of using async I/O opertations in a Console application.

The Console application

In my test environment, I have 10 queues. GetQueues() returns 10 different MessageQueue instances.

static void Main(string[] args)
{
    var isAsync = Console.ReadLine() == "Y";
    foreach (var queue in queueManager.GetQueues())
    {
        var temp = queue;
        Task.Run(() => ReceiveMessagesForQueue(temp, isAsync));
    }

    while (true)
    {
        FillAllQueuesWithMessages();
        ResetAndStartStopWatch();
        while(!AllMessagesRead())
        {
            Thread.Sleep(10);
        }
        Console.WriteLine("All messages read in {0}ms", stopWatch.ElapsedMilliseconds);
    }
}

static async Task ReceiveMessagesForQueue(MessageQueue queue, bool isAsync)
{
    while (true)
    {
        var message = await Task.Factory.FromAsync<Message>(queue.BeginReceive(), queue.EndReceive);

        if (isAsync)
            await ProcessMessageAsync(message);
        else
            ProcessMessage(message);
    }
}

Async message processing

Uses await on Task.Delay(), so should release current Thread

static async Task ProcessMessageAsync(Message message)
{
    await Task.Delay(1000);
    BurnCpu();
}

Sync message processing

waits on Task.Delay(), so shouldn't release current Thread

static void ProcessMessage(Message message)
{
    Task.Delay(1000).Wait();
    BurnCpu();
}

In the end, results are equal. Am I missing something here?

Edit 1

I'm measuring overall time using stopWatch.ElapsedMilliseconds. I Fill all queues using FillAllQueuesWithMessages() with 10, 100, 10000 or more messages.

Edit 2

ReceiveMessagesForQueue() returns Task instead of void now.

Edit 3 (fix)

This test does show me performance improvement now. I had to make BurnCpu() take more time. While Task.Delay() is being awaited, BurnCPU() can use the released thread to process.


回答1:


Using async-await doesn't speed up the time it takes to execute a single operation, it just means that you don't have a thread waiting doing nothing.

In your case Task.Delay will take a second no matter what but here:

Task.Delay(1000).Wait();

You have a thread that sits and waits for the second to end while here:

await Task.Delay(1000);

You don't. You are still asynchronously waiting (hence, await) but no thread is being used which means better scalability.

In async-await you get the performance boost because your app can do the same with less threads, or do more with the same threads. To measure that you need to have a lot of async operations concurrently. Only then will you notice that the async option utilizes CPU resources better than the synchronous one.


More info about freeing threads here There Is No Thread




回答2:


You're still running each task in its own thread from the thread pool - as you're using the default task scheduler. If you want to see performance imporvement, you'll need to make sure several tasks are performed on the same thread.

Also, with 20 parallel tasks, you're probably not going to see any difference. Try it with 2,000 tasks.



来源:https://stackoverflow.com/questions/23359829/how-to-measure-performance-of-awaiting-asynchronous-operations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!