Performance Comparison MessageSender’s Send vs SendAsync Methods

Introduction

Let's take advantage of the previous article, “Send a Brokered Message to Azure Service Bus” to leverage the same working program to compare send and sendAsync methods. We will try to send messages for a duration of one minute and then determine the actual number of messages that were sent within that minute.

Step 1: Synchronous Send

The code snippet to send the same object using the synchronous send method to the Azure Service Bus for one minute is as outlined below:

  1. private void SendMessageBySend(MessageSender sender, Employee employee)  
  2. {  
  3.    var stopwatch = new Stopwatch();  
  4.    stopwatch.Start();  
  5.    int count = 0;  
  6.    while (stopwatch.Elapsed < TimeSpan.FromSeconds(60))  
  7.    {  
  8.       count++;  
  9.       BrokeredMessage brokeredMessage = new BrokeredMessage(employee);  
  10.       sender.Send(brokeredMessage);  
  11.       brokeredMessage.Dispose();  
  12.       Console.WriteLine(string.Format("Number of messages sent - {0}, Time elapsed (in milliseconds) - {1}",
          count.ToString(), stopwatch.ElapsedMilliseconds.ToString()));  
  13.    }  
  14.    stopwatch.Stop();  
  15.    Console.WriteLine("Total number of message sent using send()  
  16.    method. Total -> " + count);  
  17.    Console.ReadLine();  

the the total number of messages sent within that minute is as shown in the following:



Step 2: Asynchronous Send

The code snippet to send the same object using the asynchronous send method to the Azure Service Bus for one minute is as outlined below:

  1. private void SendMessageBySendAsync(MessageSender sender, Employee employee)  
  2. {  
  3.    var stopwatch = new Stopwatch();  
  4.    stopwatch.Start();  
  5.    int count = 0;  
  6.    while (stopwatch.Elapsed < TimeSpan.FromSeconds(60))  
  7.    {  
  8.       count++;  
  9.       BrokeredMessage brokeredMessage = new BrokeredMessage(employee);  
  10.       sender.SendAsync(brokeredMessage);  
  11.       brokeredMessage.Dispose();  
  12.       Console.WriteLine(string.Format("Number of messages sent - {0}, Time elapsed (in milliseconds) - {1}",
          count.ToString(), stopwatch.ElapsedMilliseconds.ToString()));  
  13.    }  
  14.    stopwatch.Stop();  
  15.    Console.WriteLine("Total number of message sent using sendAsync() method. Total ->  " + count);  
  16.    Console.ReadLine();  

The total number of messages sent within that minute is as shown in the following:


Conclusion

This is a minor comparison between synchronous and asynchronous processing. In situations where high volume occurs, utilizing asynchronous processing is preferred. A code sample has been attached. Note: in the event that you experience an out of memory exception, simply reduce the duration and execute it again.

Happy coding!

Up Next
Ebook Download
View all
Learn
View all
NA 33277.9k

 Hands-On Architect provides solution to business problems. Proficient in architecture, design, development, maintenance and support pr... Know more

https://www.c-sharpcorner.com/members/kalaiarasu-mani