我正在测试 CosmosDb。我发现初始连接通常需要很多秒。我编写了一个小型 .net core 2.2 控制台应用程序来演示该问题。
static async System.Threading.Tasks.Task Main(string[] args)
{
Console.WriteLine("Hello World!");
string url = "https://docdb.documents.azure.com:443/";
string key = "myKey";
DocumentClient docClient = new DocumentClient(new Uri(url), key);
Stopwatch sw = new Stopwatch();
while (true)
{
sw.Start();
var res = await docClient.ReadDocumentAsync(UriFactory.CreateDocumentUri("ct", "ops", "xxx"),
new RequestOptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey("test") });
sw.Stop();
Console.WriteLine($"Query took {sw.ElapsedMilliseconds}ms");
sw.Reset();
await Task.Delay(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的典型结果:
Hello World!
Query took 48530ms
Query took 36ms
Query took 26ms
Query took 15ms
Run Code Online (Sandbox Code Playgroud)
第一个请求需要 48 秒!我尝试将 docClient 构造修改为:
DocumentClient docClient = new DocumentClient(new Uri(url), key,new
ConnectionPolicy {ConnectionMode = ConnectionMode.Direct,ConnectionProtocol
= Protocol.Tcp });
Run Code Online (Sandbox Code Playgroud)
要查看是否更好一些典型结果:
Hello World!
Query took 20536ms
Query took 104ms
Query took 37ms
Query took 71ms
Query took 13ms
Query took 88ms
Query took 14ms
Run Code Online (Sandbox Code Playgroud)
第一次查询还有 20 秒。
我的数据库是
130Mb
Avg Throughput /s* 3.58 RU/s
Run Code Online (Sandbox Code Playgroud)
我有
400 RU's
Run Code Online (Sandbox Code Playgroud)
有什么办法可以减轻第一次连接的延迟吗?
我关于 Cosmosdb 的文档是:
{
"id": "xxx",
"fid": "test",
"_rid": "VX8TAPKGDqNeWwEAAAAAAA==",
"_self": "dbs/VX8TAA==/colls/VX8TAPKGDqM=/docs/VX8TAPKGDqNeWwEAAAAAAA==/",
"_etag": "\"0000d4a2-0000-1100-0000-5ce801ef0000\"",
"_attachments": "attachments/",
"_ts": 1558708719
}
Run Code Online (Sandbox Code Playgroud)
这是预期的,请参阅https://learn.microsoft.com/en-us/azure/cosmos-db/performance-tips#networking中的第 2 点
默认情况下,第一个请求具有较高的延迟,因为它必须获取地址路由表。为了避免在第一个请求时出现这种启动延迟,您应该在初始化期间调用 OpenAsync() 一次,如下所示。
static async System.Threading.Tasks.Task Main(string[] args)
{
Console.WriteLine("Hello World!");
string url = "https://docdb.documents.azure.com:443/";
string key = "myKey";
DocumentClient docClient = new DocumentClient(new Uri(url), key);
await docClient.OpenAsync();
Stopwatch sw = new Stopwatch();
while (true)
{
sw.Start();
var res = await docClient.ReadDocumentAsync(UriFactory.CreateDocumentUri("ct", "ops", "xxx"),
new RequestOptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey("test") });
sw.Stop();
Console.WriteLine($"Query took {sw.ElapsedMilliseconds}ms");
sw.Reset();
await Task.Delay(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1582 次 |
最近记录: |