Azure DocumentDB性能问题
从本地计算机上的C#代码运行DocumentDB查询时,一个简单的DocumentDB查询平均需要0.5秒。另一个例子是,获取对文档集合的引用平均需要0.7秒。这是意料之中的事吗?下面是我用来检查集合是否存在的代码,这很简单,但是有什么方法可以改善糟糕的性能吗Azure DocumentDB性能问题,azure,azure-cosmosdb,Azure,Azure Cosmosdb,从本地计算机上的C#代码运行DocumentDB查询时,一个简单的DocumentDB查询平均需要0.5秒。另一个例子是,获取对文档集合的引用平均需要0.7秒。这是意料之中的事吗?下面是我用来检查集合是否存在的代码,这很简单,但是有什么方法可以改善糟糕的性能吗 // Create a new instance of the DocumentClient var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
// Create a new instance of the DocumentClient
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
// Get the database with the id=FamilyRegistry
var database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault();
var stopWatch = new Stopwatch();
stopWatch.Start();
// Get the document collection with the id=FamilyCollection
var documentCollection = client.CreateDocumentCollectionQuery("dbs/"
+ database.Id).Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault();
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
var ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
var elapsedTime = String.Format("{0:00} seconds, {1:00} milliseconds",
ts.Seconds,
ts.Milliseconds );
Console.WriteLine("Time taken to get a document collection: " + elapsedTime);
Console.ReadKey();
本地计算机上的平均输出:
Time taken to get a document collection: 0 seconds, 752 milliseconds
在我的另一段代码中,我正在做20个小文档更新,每个JSON大小约为400字节,总共需要12秒。我只在我的开发环境中运行,但我期望有更好的性能。简而言之,使用DocumentDB可以在~9毫秒内完成端到端的操作。我将在下面介绍所需的更改,以及它们为什么/如何影响结果 在DocumentDB中,第一个查询总是花费更长的时间,因为它会执行一些设置工作(获取DocumentDB分区的物理地址)。接下来的两个请求需要更长的时间来预热连接池。随后的查询将与您的网络一样快(由于SSD存储,DocumentDB中的读取延迟非常低) 例如,如果您修改上面的代码以进行测量,例如10个读数,而不仅仅是第一个读数,如下图所示:
using (DocumentClient client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey))
{
long totalRequests = 10;
var database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault();
Stopwatch watch = new Stopwatch();
for (int i = 0; i < totalRequests; i++)
{
watch.Start();
var documentCollection = client.CreateDocumentCollectionQuery("dbs/"+ database.Id)
.Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault();
Console.WriteLine("Finished read {0} in {1}ms ", i, watch.ElapsedMilliseconds);
watch.Reset();
}
}
Console.ReadKey();
接下来,我从默认的网关切换到Direct/TCP连接,以将延迟从两个跃点提高到一个,即将初始化代码更改为:
using (DocumentClient client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey, new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp }))
现在,按ID查找集合的操作在23毫秒内完成:
Finished read 0 in 197ms
Finished read 1 in 117ms
Finished read 2 in 23ms
Finished read 3 in 23ms
Finished read 4 in 25ms
Finished read 5 in 23ms
Finished read 6 in 31ms
Finished read 7 in 23ms
Finished read 8 in 23ms
Finished read 9 in 23ms
当您从同样在同一Azure DC中运行的Azure VM或工作者角色运行相同的结果时,情况如何?同样的操作在大约9毫秒内完成
Finished read 0 in 140ms
Finished read 1 in 10ms
Finished read 2 in 8ms
Finished read 3 in 9ms
Finished read 4 in 9ms
Finished read 5 in 9ms
Finished read 6 in 9ms
Finished read 7 in 9ms
Finished read 8 in 10ms
Finished read 9 in 8ms
Finished read 9 in 9ms
因此,总结一下:
- 对于性能度量,请考虑一些度量示例,以说明DocumentDB客户端的启动/初始化
- 请使用TCP/直接连接以实现最低延迟
- 如果可能,请在同一Azure区域内运行
- 如果您遵循这些步骤,就可以获得优异的性能,并且可以使用DocumentDB获得最佳性能
Finished read 0 in 140ms
Finished read 1 in 10ms
Finished read 2 in 8ms
Finished read 3 in 9ms
Finished read 4 in 9ms
Finished read 5 in 9ms
Finished read 6 in 9ms
Finished read 7 in 9ms
Finished read 8 in 10ms
Finished read 9 in 8ms
Finished read 9 in 9ms