使用 Azure Cosmos DB 执行性能和规模测试Performance and scale testing with Azure Cosmos DB

性能和规模测试是应用程序开发过程中的关键步骤。Performance and scale testing is a key step in application development. 对许多应用程序而言,数据库层对整体性能和可伸缩性有重大影响。For many applications, the database tier has a significant impact on overall performance and scalability. 因此,它是性能测试的关键组成部分。Therefore, it's a critical component of performance testing. Azure Cosmos DB 专为弹性缩放和可预测性能而构建。Azure Cosmos DB is purpose-built for elastic scale and predictable performance. 这些功能使它非常适合需要高性能数据库层的应用程序。These capabilities make it a great fit for applications that need a high-performance database tier.

要对 Azure Cosmos DB 工作负载实施性能测试的开发人员可将本文作为参考。This article is a reference for developers implementing performance test suites for their Azure Cosmos DB workloads. 此外,本文还可用于评估 Azure Cosmos DB 是否适用于高性能应用程序方案。It also can be used to evaluate Azure Cosmos DB for high-performance application scenarios. 本文重点演示隔离的数据库性能测试,但也提供适用于生产应用程序的最佳实践。It focuses primarily on isolated performance testing of the database, but also includes best practices for production applications.

阅读本文后,可以回答以下问题:After reading this article, you'll be able to answer the following questions:

  • 在哪里可以找到可用于 Azure Cosmos DB 性能测试的示例 .NET 客户端应用程序?Where can I find a sample .NET client application for performance testing of Azure Cosmos DB?
  • 如何从客户端应用程序实现 Azure Cosmos DB 的高吞吐量级别?How do I achieve high throughput levels with Azure Cosmos DB from my client application?

若要开始处理代码,请从 Azure Cosmos DB 性能测试示例下载项目。To get started with code, download the project from Azure Cosmos DB performance testing sample.

Note

此应用程序的目标是演示如何使用少量的客户端计算机从 Azure Cosmos DB 中获得更佳性能。The goal of this application is to demonstrate how to get the best performance from Azure Cosmos DB with a small number of client machines. 此示例的目标不是实现 Azure Cosmos DB 的峰值吞吐量容量(可以不受限制地缩放)。The goal of the sample is not to achieve the peak throughput capacity of Azure Cosmos DB (which can scale without any limits).

如果正在寻找用于提高 Azure Cosmos DB 性能的客户端配置选项,请参阅 Azure Cosmos DB 性能提示If you're looking for client-side configuration options to improve Azure Cosmos DB performance, see Azure Cosmos DB performance tips.

运行性能测试应用程序Run the performance testing application

最快的入门方法是根据下列步骤编译并运行 .NET 示例。The quickest way to get started is to compile and run the .NET sample, as described in the following steps. 也可以查看源代码,然后在自己的客户端应用程序中实施类似的配置。You can also review the source code and implement similar configurations on your own client applications.

步骤 1:Azure Cosmos DB 性能测试示例下载项目,或创建 GitHub 存储库分支。Step 1: Download the project from Azure Cosmos DB performance testing sample, or fork the GitHub repository.

步骤 2: 在 App.config 中修改 EndpointUrl、AuthorizationKey、CollectionThroughput 和 DocumentTemplate(可选)的设置。Step 2: Modify the settings for EndpointUrl, AuthorizationKey, CollectionThroughput, and DocumentTemplate (optional) in App.config.

Note

为集合预配高吞吐量之前,请参阅定价页估算每个集合的成本。Before you provision collections with high throughput, refer to the Pricing page to estimate the costs per collection. Azure Cosmos DB 按小时对存储和吞吐量单独计费。Azure Cosmos DB bills storage and throughput independently on an hourly basis. 可在测试完成后,删除或降低 Azure Cosmos DB 集合的吞吐量以节省成本。You can save costs by deleting or lowering the throughput of your Azure Cosmos DB collections after testing.

步骤 3: 从命令行编译并运行控制台应用。Step 3: Compile and run the console app from the command line. 应会看到如下输出:You should see output like the following:

C:\Users\cosmosdb\Desktop\Benchmark>DocumentDBBenchmark.exe
Summary:
---------------------------------------------------------------------
Endpoint: https://arramacquerymetrics.documents.azure.cn:443/
Collection : db.data at 100000 request units per second
Document Template*: Player.json
Degree of parallelism*: -1
---------------------------------------------------------------------

DocumentDBBenchmark starting...
Found collection data with 100000 RU/s
Starting Inserts with 100 tasks
Inserted 4503 docs @ 4491 writes/s, 47070 RU/s (122B max monthly 1KB reads)
Inserted 17910 docs @ 8862 writes/s, 92878 RU/s (241B max monthly 1KB reads)
Inserted 32339 docs @ 10531 writes/s, 110366 RU/s (286B max monthly 1KB reads)
Inserted 47848 docs @ 11675 writes/s, 122357 RU/s (317B max monthly 1KB reads)
Inserted 58857 docs @ 11545 writes/s, 120992 RU/s (314B max monthly 1KB reads)
Inserted 69547 docs @ 11378 writes/s, 119237 RU/s (309B max monthly 1KB reads)
Inserted 80687 docs @ 11345 writes/s, 118896 RU/s (308B max monthly 1KB reads)
Inserted 91455 docs @ 11272 writes/s, 118131 RU/s (306B max monthly 1KB reads)
Inserted 102129 docs @ 11208 writes/s, 117461 RU/s (304B max monthly 1KB reads)
Inserted 112444 docs @ 11120 writes/s, 116538 RU/s (302B max monthly 1KB reads)
Inserted 122927 docs @ 11063 writes/s, 115936 RU/s (301B max monthly 1KB reads)
Inserted 133157 docs @ 10993 writes/s, 115208 RU/s (299B max monthly 1KB reads)
Inserted 144078 docs @ 10988 writes/s, 115159 RU/s (298B max monthly 1KB reads)
Inserted 155415 docs @ 11013 writes/s, 115415 RU/s (299B max monthly 1KB reads)
Inserted 166126 docs @ 10992 writes/s, 115198 RU/s (299B max monthly 1KB reads)
Inserted 173051 docs @ 10739 writes/s, 112544 RU/s (292B max monthly 1KB reads)
Inserted 180169 docs @ 10527 writes/s, 110324 RU/s (286B max monthly 1KB reads)
Inserted 192469 docs @ 10616 writes/s, 111256 RU/s (288B max monthly 1KB reads)
Inserted 199107 docs @ 10406 writes/s, 109054 RU/s (283B max monthly 1KB reads)
Inserted 200000 docs @ 9930 writes/s, 104065 RU/s (270B max monthly 1KB reads)

Summary:
---------------------------------------------------------------------
Inserted 200000 docs @ 9928 writes/s, 104063 RU/s (270B max monthly 1KB reads)
---------------------------------------------------------------------
DocumentDBBenchmark completed successfully.
Press any key to exit...

步骤 4(如有必要): 工具报告的吞吐量(RU/秒)应该等于或大于为某个集合或一组集合预配的吞吐量。Step 4 (if necessary): The throughput reported (RU/s) from the tool should be the same or higher than the provisioned throughput of the collection or a set of collections. 如果情况并非如此,以较小的增量提高 DegreeOfParallelism 可帮助达到该限制。If it's not, increasing the DegreeOfParallelism in small increments might help you reach the limit. 如果客户端应用的吞吐量达到持平状态,请在其他客户端计算机上启动多个应用实例。If the throughput from your client app plateaus, start multiple instances of the app on additional client machines. 如需此步骤的帮助,请通过 Azure 支持创建支持票证。If you need help with this step, file a support ticket from the Azure Support.

应用处于运行状态后,可以尝试不同的编制索引策略一致性级别,以了解它们对吞吐量和延迟的影响。After you have the app running, you can try different indexing policies and consistency levels to understand their impact on throughput and latency. 用户也可以查看源代码,并在自己的测试套件或生产应用程序中实施类似的配置。You can also review the source code and implement similar configurations to your own test suites or production applications.

后续步骤Next steps

本文介绍了如何使用 .NET 控制台应用对 Azure Cosmos DB 执行性能和缩放测试。In this article, we looked at how you can perform performance and scale testing with Azure Cosmos DB by using a .NET console app. 有关详细信息,请参阅以下文章:For more information, see the following articles: