如何衡量等待异步操作的性能? [英] How to measure performance of awaiting asynchronous operations?

查看:200
本文介绍了如何衡量等待异步操作的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,从多个读的MessageQueue 实例。这些messagequeues所有经营自己的工作阅读信息。通常情况下,阅读的邮件后,一个I / O数据库的工作已经完成。我发现的文章,声称它使用的I / O操作异步个好主意,因为它会释放线程。我试图以模拟控制台应用程序中使用异步I / O opertations的性能提升。

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.

在我的测试环境,我有10个队列。 GetQueues()返回1​​0个不同的的MessageQueue 实例。

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);
    }
}

异步消息处理

用途等待关于 Task.Delay(),所以应该释放电流

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

同步消息处理

在等待 Task.Delay(),所以不应该释放电流

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?

我用的测量总时间 stopWatch.ElapsedMilliseconds 。我用填充所有队列 FillAllQueuesWithMessages() 10,100,10000或更多的消息。

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

ReceiveMessagesForQueue()收益工作而不是无效现在。

此测试确实现在向我展示的性能提升。我不得不做出 BurnCpu()需要更多的时间。而 Task.Delay()正在等待, BurnCPU()可以使用发布的线程来处理。

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.

推荐答案

使用异步等待不加快它需要执行一次操作的时候,它只是意味着你没有一个线程等待无所事事

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.

在你的情况 Task.Delay 将进行第二次不管是什么,但这里:

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.

异步等待你得到的性能提升,因为您的应用程序可以做同样的用更少的线程,或用相同的线程做多。为了测量,你需要有很多的的异步操作同时进行。只有这样,你注意到异步选项使用 CPU 的资源比同步的好。

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

这篇关于如何衡量等待异步操作的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆