Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure performance of awaiting asynchronous operations?

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.

like image 430
annemartijn Avatar asked Apr 29 '14 08:04

annemartijn


People also ask

How would you measure the performance of async operations?

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.

How does async-await improve performance?

Your Ultimate async / await Tutorial in C# Increase the performance and responsiveness of your application, particularly when you have long-running operations that do not require to block the execution. In this case, you can perform other work while waiting for the result from the long running task.

Does asynchronous programming improve performance?

For applications with many tasks, programmers can consider using async programming. It allows one or more tasks to progress independently, rather than sequentially. The user benefits from increased responsiveness and improved overall performance.

Is async-await slower?

I found out that running async-await can be much slower in some scenarios. But if I click on the 'both' button, the 'await' version is ~3-4 times slower than the promises version.


2 Answers

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

like image 177
i3arnon Avatar answered Sep 20 '22 18:09

i3arnon


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.

like image 27
zmbq Avatar answered Sep 21 '22 18:09

zmbq