I am building a workrole which is fully leverage Azure storage queue. I have two requirements, I need to insert a queue as fast as it can, and also have to load data from another queue as fast as it can.
To meet my insert performance, I am using async to insert queue with the following configuration:
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.DefaultConnectionLimit = int.MaxValue;
ServicePointManager.Expect100Continue = false;
with async method AddMessageAsync. And then use GetMessages to load message in the queue. After experiment, message insert did very well (around 1500/s), but the GetMessages method hangs.
I can use the sync method AddMessage to insert message, it leads to a better balance between add and get. But 150/s insertion not meet my requirement, I'd like to at least trip it so I switched to async way.
And I can understand that async approach raises lots of extra threads and connections. But there is any way to balance this situation? My understand after experiment is that if you use async method in a process, it's no way to use sync method anymore. Is that true?
Sample code is listed below:
CloudQueue asyncQueue = null; private void PerfButton_Click(object sender, RoutedEventArgs e) { Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = GetStorageAccount(); CloudQueueClient client = storageAccount.CreateCloudQueueClient(); CloudQueue queueAsync = client.GetQueueReference("async"); queueAsync.DeleteIfExists(); queueAsync.CreateIfNotExists(); this.asyncQueue = queueAsync; int count = 0; Stopwatch watch = new Stopwatch(); watch.Start(); BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += worker_DoWork; worker.RunWorkerAsync(); do { var message = new CloudQueueMessage("Hello Queue"); var results = queueAsync.GetMessages(32); count += results.Count(); if (count % 100 == 0) { Debug.WriteLine(string.Format("It takes {0} seconds to load {1} objects", watch.ElapsedMilliseconds / 1000, count)); } } while (true); } void worker_DoWork(object sender, DoWorkEventArgs e)
{
do
{
var message = new CloudQueueMessage("Hello Queue");
this.asyncQueue.AddMessageAsync(message);
}
while (true);
}