当从我本地计算机上的C#代码的DocumentDB查询运行时,一个简单的DocumentDB查询平均需要大约0.5秒.另一个例子,获取对文档集合的引用平均需要大约0.7秒.这是预期的吗?下面是我检查集合是
// 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个小文档更新,每个大小约为400字节的JSON大小,总共需要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();
我在Redmond的桌面上运行以下结果,对抗Azure West US数据中心,即大约50毫秒.这些数字可能因网络连接和客户端与Azure DC托管DocumentDB的距离而有所不同:
Finished read 0 in 217ms Finished read 1 in 46ms Finished read 2 in 51ms Finished read 3 in 47ms Finished read 4 in 46ms Finished read 5 in 93ms Finished read 6 in 48ms Finished read 7 in 45ms Finished read 8 in 45ms Finished read 9 in 51ms
接下来,我从默认的Gateway切换到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 / Direct连接以获得最低延迟.>如果可能,请在同一Azure区域内运行.>如果您按照这些步骤操作,您可以获得出色的性能,并且您将能够使用DocumentDB获得最佳性能数据.