title: How to measure performance of awaiting asynchronous operations?
body: <p>I have a Windows Service that reads from multiple <code>MessageQueue</code> instances. Those messagequeues all run their own <code>Task</code> 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.</p>
  
  <h1>The Console application</h1>
  
  <p>In my test environment, I have 10 queues. <code>GetQueues()</code> returns 10 different <code>MessageQueue</code> instances.</p>
  
  <pre><code>static void Main(string[] args)
  {
      var isAsync = Console.ReadLine() == "Y";
      foreach (var queue in queueManager.GetQueues())
      {
          var temp = queue;
          Task.Run(() =&gt; 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&lt;Message&gt;(queue.BeginReceive(), queue.EndReceive);
  
          if (isAsync)
              await ProcessMessageAsync(message);
          else
              ProcessMessage(message);
      }
  }
  </code></pre>
  
  <h2>Async message processing</h2>
  
  <p>Uses await on <code>Task.Delay()</code>, so should release current <code>Thread</code></p>
  
  <pre><code>static async Task ProcessMessageAsync(Message message)
  {
      await Task.Delay(1000);
      BurnCpu();
  }
  </code></pre>
  
  <h2>Sync message processing</h2>
  
  <p>waits on <code>Task.Delay()</code>, so shouldn't release current <code>Thread</code></p>
  
  <pre><code>static void ProcessMessage(Message message)
  {
      Task.Delay(1000).Wait();
      BurnCpu();
  }
  </code></pre>
  
  <p>In the end, results are equal. Am I missing something here?</p>
  
  <h3>Edit 1</h3>
  
  <p>I'm measuring overall time using <code>stopWatch.ElapsedMilliseconds</code>. I Fill all queues using <code>FillAllQueuesWithMessages()</code> with 10, 100, 10000 or more messages.</p>
  
  <h3>Edit 2</h3>
  
  <p><code>ReceiveMessagesForQueue()</code> returns <code>Task</code> instead of <code>void</code> now.</p>
  
  <h3>Edit 3 (fix)</h3>
  
  <p>This test does show me performance improvement now. I had to make <code>BurnCpu()</code> take more time. While <code>Task.Delay()</code> is being awaited, <code>BurnCPU()</code> can use the released thread to process.</p>