目前我将像这样放入 azure 表存储中:
public static void AzurePut(string Key, byte[] Value)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
var keyhash = MyTable.CalculateMD5Hash(Key);
var tc = MyTable.GetBinFromHash(keyhash, AzureTables.TableCount);
CloudTable table = tableClient.GetTableReference("table" + tc);
var entity = new AzureEntity();
entity.Key = keyhash;
entity.SetValue(Value);
TableOperation insertOperation = TableOperation.InsertOrReplace(entity);
table.Execute(insertOperation);
}
我做了很多看跌期权,但它们很慢。当我打开 fiddler 时,它们的速度快了 40 倍。检查原因后发现,fiddler 重用了带有 connection: keep-alive header 的连接。有没有办法使用表存储 API 来做到这一点?
最佳答案
简短: 将其添加到应用程序的启动代码中:
ServicePointManager.DefaultConnectionLimit = 100; // Default 2
ServicePointManager.UseNagleAlgorithm = false; // Default true
说明
您不必添加任何 Keep-Alive header ,它们已经存在。看看HttpWebRequestFactory
(第 86 行):
#if WINDOWS_DESKTOP && !WINDOWS_PHONE
request.KeepAlive = true;
// Disable the Expect 100-Continue
request.ServicePoint.Expect100Continue = false;
#endif
return request;
}
最重要的是HttpWebRequest默认情况下使用 HTTP 1.1 makes connection persistent by default
您可以使用TcpView查看连接正在被重用。
Fiddler 之所以如此之快,主要是因为它在重用连接、批处理和缓冲请求方面非常聪明,尤其是当您的应用程序发出大量并行请求时。
默认情况下,ServicePointManager.DefaultConnectionLimit
为 2,这意味着您同时只能有 2 个待处理请求。想象一下,您有 8 个线程尝试发出请求,其中 2 个线程可以同时处于事件状态,其余的则在等待。提高限制大大提高了并发请求。
另一个问题是默认情况下启用 ServicePointManager.UseNagleAlgorithm
。由于大多数 Azure 表请求都相对较小(HTTP 消息大小 < 1460 字节),因此不需要对它们进行缓冲。查看对此的更长解释 at Microsoft Azure Storage Team Blog (Nagle’s Algorithm is Not Friendly towards Small Requests)
关于azure - windows azure 表存储,如何启用保持事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26946893/