你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 .NET 的 Microsoft Azure 负载测试管理客户端库

Microsoft Azure 负载测试是一项完全托管的负载测试服务,可用于生成大规模负载。 该服务可以模拟应用程序的流量,且无需其托管位置。 开发人员、测试人员和质量保证 (QA) 工程师可以使用该服务来优化应用程序性能、可缩放性或容量。

此库支持管理 Microsoft Azure 负载测试资源。

此库遵循 新的 Azure SDK 准则,并提供许多核心功能:

- Support MSAL.NET, Azure.Identity is out of box for supporting MSAL.NET.
- Support [OpenTelemetry](https://opentelemetry.io/) for distributed tracing.
- HTTP pipeline with custom policies.
- Better error-handling.
- Support uniform telemetry across all languages.

入门

安装包

使用 NuGet 安装适用于 .NET 的 Microsoft Azure 负载测试管理库:

dotnet add package Azure.ResourceManager.LoadTesting

先决条件

对客户端进行身份验证

若要创建经过身份验证的客户端并开始与 Microsoft Azure 资源交互,请参阅 此处的快速入门指南

关键概念

在此处找到用于 .NET 的 Microsoft Azure SDK的关键概念。

文档

文档可帮助你了解如何使用此包:

示例

创建新的 Azure 负载测试资源

在创建 Azure 负载测试资源之前,需要有一个资源组。

ArmClient armClient = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();
// With the collection, we can create a new resource group with an specific name
string rgName = "sample-rg";
AzureLocation location = AzureLocation.WestUS2;
ArmOperation<ResourceGroupResource> resourceGroupLro = await rgCollection.CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(location));
ResourceGroupResource resourceGroup = lro.Value;

创建 Azure 负载测试资源。

LoadTestingResourceCollection loadTestingCollection = _resourceGroup.GetLoadTestingResources();
string loadTestResourceName = "sample-loadtest";
LoadTestingResourceData inputPayload = new LoadTestingResourceData(AzureLocation.WestUS2);
ArmOperation<LoadTestingResource> loadTestingLro = await loadTestingCollection.CreateOrUpdateAsync(WaitUntil.Completed, loadTestResourceName, inputPayload);

LoadTestingResource resource = loadTestingLro.Value;

创建配置了 CMK 加密的 Azure 负载测试资源。

LoadTestingResourceCollection loadTestingCollection = _resourceGroup.GetLoadTestingResources();
string loadTestResourceName = "sample-loadtest";
LoadTestingResourceData inputPayload = new LoadTestingResourceData(AzureLocation.WestUS2);

// Managed identity properties
ResourceIdentifier identityId = new ResourceIdentifier("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.managedidentity/userassignedidentities/identity1");
inputPayload.Identity = new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssignedUserAssigned);
inputPayload.Identity.UserAssignedIdentities.Add(identityId, new UserAssignedIdentity());

// CMK encryption properties
inputPayload.Encryption = new LoadTestingCmkEncryptionProperties();
inputPayload.Encryption.KeyUri = new Uri("https://sample-kv.vault.azure.net/keys/cmkkey/2d1ccd5c50234ea2a0858fe148b69cde");
inputPayload.Encryption.Identity = new LoadTestingCmkIdentity();
inputPayload.Encryption.Identity.IdentityType = LoadTestingCmkIdentityType.UserAssigned;
inputPayload.Encryption.Identity.ResourceId = identityId;

ArmOperation<LoadTestingResource> loadTestingLro = await loadTestingCollection.CreateOrUpdateAsync(WaitUntil.Completed, loadTestResourceName, inputPayload);

LoadTestingResource resource = loadTestingLro.Value;

获取 Azure 负载测试资源的详细信息

LoadTestingResourceCollection loadTestingCollection = _resourceGroup.GetLoadTestingResources();

string loadTestResourceName = "sample-loadtest";
Response<LoadTestingResource> loadTestingResponse = await loadTestingCollection.GetAsync(loadTestResourceName);

LoadTestingResource resource = loadTestingResponse.Value;

更新 Azure 负载测试资源

更新 Azure 负载测试资源,以使用系统分配的托管标识配置 CMK 加密。

LoadTestingResourceCollection loadTestingCollection = _resourceGroup.GetLoadTestingResources();
string loadTestResourceName = "sample-loadtest";
Response<LoadTestingResource> loadTestingResponse = await loadTestingCollection.GetAsync(loadTestResourceName);
LoadTestingResource resource = loadTestingResponse.Value;

ResourceIdentifier identityId = new ResourceIdentifier("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.managedidentity/userassignedidentities/identity1");
LoadTestingResourcePatch resourcePatchPayload = new LoadTestingResourcePatch {
    Encryption = new LoadTestingCmkEncryptionProperties
    {
        Identity = new LoadTestingCmkIdentity
        {
            // make sure that system-assigned managed identity is enabled on this resource and the identity has been granted required permissions to access the key.
            IdentityType = LoadTestingCmkIdentityType.SystemAssigned,
            ResourceId = null
        },
        KeyUri = new Uri("https://sample-kv.vault.azure.net/keys/cmkkey/2d1ccd5c50234ea2a0858fe148b69cde")
    }
};

ArmOperation<LoadTestingResource> loadTestingLro = await resource.UpdateAsync(WaitUntil.Completed, resourcePatchPayload);

LoadTestingResource updatedResource = loadTestingLro.Value;

更新 Azure 负载测试资源以更新用户分配的托管标识。

LoadTestingResourceCollection loadTestingCollection = _resourceGroup.GetLoadTestingResources();
string loadTestResourceName = "sample-loadtest";
Response<LoadTestingResource> loadTestingResponse = await loadTestingCollection.GetAsync(loadTestResourceName);
LoadTestingResource resource = loadTestingResponse.Value;

ResourceIdentifier identityId1 = new ResourceIdentifier("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.managedidentity/userassignedidentities/identity1");
ResourceIdentifier identityId2 = new ResourceIdentifier("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.managedidentity/userassignedidentities/identity2");

LoadTestingResourcePatch resourcePatchPayload = new LoadTestingResourcePatch();
resourcePatchPayload.Identity = new ManagedServiceIdentity(ManagedServiceIdentityType.UserAssigned);
// removes user-assigned identity with resourceId <identityId1> (if already assigned to the load testing resource)
resourcePatchPayload.Identity.UserAssignedIdentities.Add(identityId1, null);
resourcePatchPayload.Identity.UserAssignedIdentities.Add(identityId2, new UserAssignedIdentity());

ArmOperation<LoadTestingResource> loadTestingLro = await resource.UpdateAsync(WaitUntil.Completed, resourcePatchPayload);
LoadTestingResource updatedResource = loadTestingLro.Value;

删除 Azure 负载测试资源

LoadTestingResourceCollection loadTestingCollection = _resourceGroup.GetLoadTestingResources();
string loadTestResourceName = "sample-loadtest";
Response<LoadTestingResource> loadTestingResponse = await loadTestingCollection.GetAsync(loadTestResourceName);
LoadTestingResource resource = loadTestingResponse.Value;

ArmOperation loadTestDeleteResponse = await resource.DeleteAsync(WaitUntil.Completed);

配额操作

获取负载测试配额集合。

LoadTestingQuotaCollection QuotaCollection = _subscription.GetAllLoadTestingQuota(AzureLocation.WestUS2);
// Use the quotaCollection for all the quota operations.

获取特定配额存储桶的配额值。

LoadTestingQuotaCollection QuotaCollection = _subscription.GetAllLoadTestingQuota(AzureLocation.WestUS2);

// Get the quota values for a particular quota bucket
Response<LoadTestingQuotaResource> quotaResponse = await QuotaCollection.GetAsync("maxConcurrentTestRuns");
LoadTestingQuotaResource quotaBucket = quotaResponse.Value;

获取所有配额存储桶的配额值。

LoadTestingQuotaCollection QuotaCollection = _subscription.GetAllLoadTestingQuota(AzureLocation.WestUS2);

// Get the quota values for a all quota buckets
List<LoadTestingQuotaResource> quotaBuckets = await QuotaCollection.GetAllAsync().ToEnumerableAsync();

检查配额可用性。

LoadTestingQuotaCollection QuotaCollection = _subscription.GetAllLoadTestingQuota(AzureLocation.WestUS2);

Response<LoadTestingQuotaResource> quotaResponse = await QuotaCollection.GetAsync("maxConcurrentTestRuns");
LoadTestingQuotaResource quotaResource = quotaResponse.Value;

LoadTestingQuotaBucketDimensions dimensions = new LoadTestingQuotaBucketDimensions("<subscription-id>", AzureLocation.WestUS2);
LoadTestingQuotaBucketContent quotaAvailabilityPayload = new LoadTestingQuotaBucketContent(
    quotaResponse.Value.Data.Id,
    quotaResource.Data.Name,
    quotaResource.Data.ResourceType,
    null,
    quotaResource.Data.Usage,
    quotaResource.Data.Limit,
    50, // new quota value
    dimensions);

Response<LoadTestingQuotaAvailabilityResult> checkAvailabilityResult = await quotaResponse.Value.CheckLoadTestingQuotaAvailabilityAsync(quotaAvailabilityPayload);
// IsAvailable property indicates whether the requested quota is available.
Console.WriteLine(checkAvailabilityResult.Value.IsAvailable);

有关使用 .NET 管理库的代码示例,请参阅以下位置

疑难解答

后续步骤

有关 Microsoft Azure SDK 的详细信息,请参阅 此网站

供稿

有关参与此存储库的详细信息,请参阅 参与指南

本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 https://cla.microsoft.com

提交拉取请求时,CLA 机器人会自动确定是否需要提供 CLA 并适当修饰 PR, (例如标签、注释) 。 按照机器人提供的说明操作。 只需使用 CLA 在所有存储库中执行此操作一次。

此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,如有任何其他问题或评论,请联系 opencode@microsoft.com