gpt4 book ai didi

c# - Azure 存储队列性能不佳 - 只有 200 RPS

转载 作者:太空宇宙 更新时间:2023-11-03 15:09:19 24 4
gpt4 key购买 nike

我使用 Asp.Net Core Web 应用程序 (.Net Core) 模板创建了一个 dotnet core Web api,该模板为我们提供了 api/values Controller ,并且在帖子中我使用 Windows Azure 存储库 v8 向 azure 存储队列发送消息.0。 https://www.nuget.org/packages/WindowsAzure.Storage/8.0.0

目前,当我执行单个请求时,队列大约需要一个平均时间。完成方法 AddMessageAsync() 需要 140 毫秒,但是当我对每秒 200 个请求进行负载测试时,相同的方法平均需要 800 毫秒才能完成。根据azure存储队列,它应该能够每秒处理2000个请求,但我每秒无法处理200个请求。

如果有人提供一些信息来解释为什么 Web 应用程序 api 未按预期执行,我将不胜感激。

请参阅下面我的代码示例

Startup.cs-ConfigureServices()

// Add QueueAccessLayer.
services.AddSingleton<IQueueAccessLayer, QueueAccessLayer>();

电子邮件 Controller .cs

[Route("api/[controller]")]
public class EmailController : Controller
{
private IQueueAccessLayer _queue;

public EmailController(IQueueAccessLayer queue)
{
_queue = queue;
}

// POST api/values
[HttpPost]
public async Task<IActionResult> Post([FromBody]string value)
{
var emailMessage = "Message Id - " + Guid.NewGuid();
await _queue.SendMessage(emailMessage);
return new EmptyResult();
}
}

QueueAccessLayer.cs

public class QueueAccessLayer : IQueueAccessLayer
{
private CloudQueueClient _queueClient;
private CloudStorageAccount _storageAccount;

private CloudQueue _emailQueue;
private ILogger<QueueAccessLayer> _logger;


public QueueAccessLayer(ILogger<QueueAccessLayer> logger)
{
_storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=test1;AccountKey=#####;");
_queueClient = _storageAccount.CreateCloudQueueClient();
_emailQueue = _queueClient.GetQueueReference("emailqueue");
_emailQueue.CreateIfNotExistsAsync().Wait();

_logger = logger;
}

public async Task<bool> SendMessage(string msg)
{
Stopwatch watch = new Stopwatch();
watch.Start();
CloudQueueMessage message = new CloudQueueMessage(msg);
await _emailQueue.AddMessageAsync(message);
watch.Stop();

_logger.LogInformation(msg + " - " + watch.ElapsedMilliseconds + "ms");
return true;
}
}

public interface IQueueAccessLayer
{
Task<bool> SendMessage(string msg);
}

最佳答案

确保在应用程序启动时设置以下内容:

  1. ServicePointManager.Expect100Continue = false;
  2. ServicePointManager.UseNagleAlgorithm = false;

Expect100Continue 设置为 false - 将减少发送请求时到服务器端的往返次数。

UseNagleAlgorithm 设置为 false - 将关闭 Nagle 优化并显着提高性能。

有一个很棒的博客解释了这一点:Nagle’s Algorithm is Not Friendly towards Small Requests .

Nagling is a TCP optimization on the sender and it is designed to reduce network congestion by coalescing small send requests into larger TCP segments... This is achieved by holding back small segments either until TCP has enough data to transmit a full sized segment or until all outstanding data has been acknowledged by the receiver... The test is run as a worker role accessing our storage account in the same geo location. It inserts messages which are 480 bytes in length. The results show that the average latency improves by more than 600% with Nagling turned off

关于c# - Azure 存储队列性能不佳 - 只有 200 RPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41883860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com