对于 Windows azure 队列,每个存储的可扩展性目标应该是大约 500 条消息/秒 ( http://msdn.microsoft.com/en-us/library/windowsazure/hh697709.aspx )。我有以下简单的程序,它只是将一些消息写入队列。该程序需要 10 秒才能完成(4 条消息/秒)。我正在从虚拟机内部(在西欧)运行该程序,并且我的存储帐户也位于西欧。我没有为我的存储设置异地复制。我的连接字符串设置为使用 http 协议。
// http://blogs.msdn.com/b/windowsazurestorage/archive/2010/06/25/nagle-s-algorithm-is-not-friendly-towards-small-requests.aspx
ServicePointManager.UseNagleAlgorithm = false;
CloudStorageAccount storageAccount=CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]);
var cloudQueueClient = storageAccount.CreateCloudQueueClient();
var queue = cloudQueueClient.GetQueueReference(Guid.NewGuid().ToString());
queue.CreateIfNotExist();
var w = new Stopwatch();
w.Start();
for (int i = 0; i < 50;i++ )
{
Console.WriteLine("nr {0}",i);
queue.AddMessage(new CloudQueueMessage("hello "+i));
}
w.Stop();
Console.WriteLine("elapsed: {0}", w.ElapsedMilliseconds);
queue.Delete();
知道如何获得更好的性能吗?
编辑:
根据 Sandrino Di Mattia 的回答,我重新分析了我最初发布的代码,发现它不够完整,无法重现错误。事实上,我在调用 ServicePointManager.UseNagleAlgorithm = false; 之前创建了一个队列。重现我的问题的代码看起来更像这样:
CloudStorageAccount storageAccount=CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]);
var cloudQueueClient = storageAccount.CreateCloudQueueClient();
var queue = cloudQueueClient.GetQueueReference(Guid.NewGuid().ToString());
//ServicePointManager.UseNagleAlgorithm = false; // If you change the nagle algorithm here, the performance will be okay.
queue.CreateIfNotExist();
ServicePointManager.UseNagleAlgorithm = false; // TOO LATE, the queue is already created without 'nagle'
var w = new Stopwatch();
w.Start();
for (int i = 0; i < 50;i++ )
{
Console.WriteLine("nr {0}",i);
queue.AddMessage(new CloudQueueMessage("hello "+i));
}
w.Stop();
Console.WriteLine("elapsed: {0}", w.ElapsedMilliseconds);
queue.Delete();
Sandrino 建议的使用 app.config 文件配置 ServicePointManager 的解决方案的优点是 ServicePointManager 在应用程序启动时被初始化,因此您不必担心时间依赖性。