天蓝色队列性能

sha*_*aft 2 c# performance azure

对于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();
Run Code Online (Sandbox Code Playgroud)

知道如何才能获得更好的性能吗?

编辑:

根据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();
Run Code Online (Sandbox Code Playgroud)

Sandrino建议的使用app.config文件配置ServicePointManager的解决方案具有以下优点:ServicePointManager在应用程序启动时初始化,因此您不必担心时间依赖性.

San*_*tia 10

几天前我回答了一个类似的问题:如何使用azure存储表每秒更多地插入10个插件.

为了在表存储中添加1000个项目,花费了3分钟,并且根据我在答案中描述的更改,它降至4秒(250个请求/秒).最后,表存储和存储队列并没有那么不同.后端是相同的,数据只是以不同的方式存储.表存储和队列都通过REST API公开,因此如果您改进处理请求的方式,您将获得更好的性能.

最重要的变化:

  • expect100Continue:假的
  • useNagleAlgorithm:false(你已经这样做了)
  • 并行请求与 connectionManagement/maxconnection