共用方式為


適用于 Python 的 Azure 負載測試用戶端程式庫 - 1.0.0 版

Azure 負載測試為使用者提供 Python 中的用戶端程式庫,讓使用者可以透過此程式庫與 Azure 負載測試服務原生互動。 Azure 負載測試是完全受控的負載測試服務,可讓您產生大規模的負載。 不論應用程式的裝載位置為何,服務都會模擬應用程式的流量。 開發人員、測試人員和品質保證 (QA) 工程師可以使用它來優化應用程式效能、延展性或容量。

文件

有各種檔可協助您開始使用

開始使用

安裝套件

python -m pip install azure-developer-loadtesting

必要條件

  • 需要 Python 3.7 或更新版本才能使用此套件。
  • 您需要 Azure 訂 用帳戶才能使用此套件。
  • 現有的 Azure 開發人員 LoadTesting 實例。

使用 Azure Active Directory 認證建立

若要使用 Azure Active Directory (AAD) 權杖認證,請提供從 azure-identity 程式庫取得所需認證類型的實例。

若要向 AAD 進行驗證,您必須先安裝pipazure-identity

設定之後,您可以從 azure.identity 選擇要使用的認證類型。

例如,透過 Azure CLI az login 命令登入, 而 DefaultAzureCredential 會以該使用者身分驗證。

使用傳回的權杖認證來驗證用戶端。

建立用戶端

Azure Developer LoadTesting SDK 有 2 個主要用戶端的子客戶端 (LoadTestingClient) 來與服務「系統管理」和「test_run」互動。

from azure.developer.loadtesting import LoadTestAdministrationClient

# for managing authentication and authorization
# can be installed from pypi, follow: https://pypi.org/project/azure-identity/
# using DefaultAzureCredentials, read more at: https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python
from azure.identity import DefaultAzureCredential

client = LoadTestAdministrationClient(endpoint='<endpoint>', credential=DefaultAzureCredential())

<endpoint> 是指資源的資料平面端點/URL。

重要概念

適用于 python 的 Azure 負載測試用戶端程式庫可讓您透過用戶端使用來與每個元件互動。 有兩個最上層用戶端是程式庫的主要進入點

  • LoadTestAdministrationClient (azure.developer.loadtesting.LoadTestAdministrationClient)
  • LoadTestRunClient (azure.developer.loadtesting.LoadTestRunClient)

這兩個用戶端也有非同步對應專案,也就是

  • LoadTestAdministrationClient (azure.developer.loadtesting.aio.LoadTestAdministrationClient)
  • LoadTestRunClient (azure.developer.loadtesting.aio.LoadTestRunClient)

負載測試管理用戶端

LoadTestAdministrationClient可用來管理和設定負載測試、應用程式元件和計量。

測試

測試會指定測試腳本和執行負載測試的組態設定。 您可以在 Azure 負載測試資源中建立一或多個測試。

應用程式元件

當您針對 Azure 裝載的應用程式執行負載測試時,您可以監視不同 Azure 應用程式元件的資源計量 (伺服器端計量)。 當負載測試執行時,以及測試完成後,您可以在 Azure 負載測試儀表板中監視和分析資源計量。

計量

在負載測試期間,Azure 負載測試會收集有關測試執行的計量。 計量的類型有兩種:

  1. 用戶端計量可提供測試引擎所報告的詳細資料。 這些計量包括虛擬使用者數目、要求回應時間、失敗要求數目或每秒要求數目。

  2. 伺服器端計量適用於 Azure 裝載的應用程式,並提供 Azure 應用程式元件的相關資訊。 計量可以用於資料庫讀取數目、HTTP 回應的類型或容器資源耗用量。

測試回合用戶端

LoadTestRunClient用來啟動和停止對應至負載測試的測試回合。 測試回合代表負載測試的一個執行。 其會收集與執行 Apache JMeter 指令碼、負載測試 YAML 組態、要監視的應用程式元件清單,以及測試結果相關聯的記錄。

Data-Plane端點

Azure 負載測試資源的資料平面可以使用下列 URL 格式定址:

00000000-0000-0000-0000-000000000000.aaa.cnt-prod.loadtesting.azure.com

第一個 GUID 00000000-0000-0000-0000-000000000000 是用來存取 Azure 負載測試資源的唯一識別碼。 後面接著 aaa 是資源的 Azure 區域。

資料平面端點是從控制平面 API 取得。

範例:1234abcd-12ab-12ab-12ab-123456abcdef.eus.cnt-prod.loadtesting.azure.com

在上述範例中, eus 代表 Azure 區域 East US

範例

建立負載測試

from azure.developer.loadtesting import LoadTestAdministrationClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
import os

TEST_ID = "some-test-id"  
DISPLAY_NAME = "my-load-test"  

# set SUBSCRIPTION_ID as an environment variable
SUBSCRIPTION_ID = os.environ["SUBSCRIPTION_ID"]  

client = LoadTestAdministrationClient(endpoint='<endpoint>', credential=DefaultAzureCredential())

try:
    result = client.create_or_update_test(
        TEST_ID,
        {
            "description": "",
            "displayName": "My New Load Test",
            "loadTestConfig": {
                "engineInstances": 1,
                "splitAllCSVs": False,
            },
            "passFailCriteria": {
                "passFailMetrics": {
                    "condition1": {
                        "clientmetric": "response_time_ms",
                        "aggregate": "avg",
                        "condition": ">",
                        "value": 300
                    },
                    "condition2": {
                        "clientmetric": "error",
                        "aggregate": "percentage",
                        "condition": ">",
                        "value": 50
                    },
                    "condition3": {
                        "clientmetric": "latency",
                        "aggregate": "avg",
                        "condition": ">",
                        "value": 200,
                        "requestName": "GetCustomerDetails"
                    }
                }
            },
            "secrets": {
                "secret1": {
                    "value": "https://sdk-testing-keyvault.vault.azure.net/secrets/sdk-secret",
                    "type": "AKV_SECRET_URI"
                }
            },
            "environmentVariables": {
                "my-variable": "value"
            }
        }
    )
    print(result)
except HttpResponseError as e:
     print('Service responded with error: {}'.format(e.response.json()))

將 .jmx 檔案上傳至測試

from azure.developer.loadtesting import LoadTestAdministrationClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError

TEST_ID = "some-test-id"  
FILE_NAME = "some-file-name.jmx"  

client = LoadTestAdministrationClient(endpoint='<endpoint>', credential=DefaultAzureCredential())

try:

    # uploading .jmx file to a test
    resultPoller = client.begin_upload_test_file(TEST_ID, FILE_NAME, open("sample.jmx", "rb"))

    # getting result of LRO poller with timeout of 600 secs
    validationResponse = resultPoller.result(600)
    print(validationResponse)
    
except HttpResponseError as e:
    print("Failed with error: {}".format(e.response.json()))

執行測試

from azure.developer.loadtesting import LoadTestRunClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError

TEST_ID = "some-test-id"  
TEST_RUN_ID = "some-testrun-id" 
DISPLAY_NAME = "my-load-test-run"  

client = LoadTestRunClient(endpoint='<endpoint>', credential=DefaultAzureCredential())

try:
    testRunPoller = client.begin_test_run(
    TEST_RUN_ID,
        {
            "testId": TEST_ID,
            "displayName": "My New Load Test Run",
        }
    )

    #waiting for test run status to be completed with timeout = 3600 seconds
    result = testRunPoller.result(3600)
    
    print(result)
except HttpResponseError as e:
    print("Failed with error: {}".format(e.response.json()))

下一步

您可以 在這裡找到更多範例。

參與

此專案歡迎參與和提供建議。 大部分的參與都要求您同意「參與者授權合約 (CLA)」,宣告您有權且確實授與我們使用投稿的權利。 如需詳細資料,請前往 https://cla.microsoft.com

當您提交提取要求時,CLA Bot 會自動判斷您是否需要提供 CLA,並適當地裝飾 PR (例如標籤、註解)。 請遵循 bot 提供的指示。 您只需要使用我們的 CLA 在所有存放庫上執行此動作一次。

此專案採用 Microsoft Open Source Code of Conduct (Microsoft 開放原始碼管理辦法)。 如需詳細資訊,請參閱管理辦法常見問題,或連絡 opencode@microsoft.com 任何其他問題或意見。

疑難排解

即將推出...