JavaScript 用 Azure Load Testing クライアント ライブラリ - バージョン 1.0.0

Azure Load Testing は、ユーザーが Azure Load Testing サービスとネイティブに対話できる JavaScript のクライアント ライブラリを提供します。 Azure Load Testing は、大規模な負荷を生成できるフル マネージドのロード テスト サービスです。 サービスは、アプリケーションがどこにホストされているかにかかわらず、そのトラフィックをシミュレートします。 開発者、テスト担当者、品質保証 (QA) エンジニアは、それを使って、アプリケーションのパフォーマンス、スケーラビリティ、または容量を最適化できます。

このライブラリを使用するには、 REST クライアント ドキュメント に大きく依存してください

ドキュメント

作業の開始に役立つさまざまなドキュメントが用意されています

はじめに

現在サポートされている環境

  • Node.js バージョン 14.x.x 以降

前提条件

@azure-rest/load-testing パッケージのインストール

を使用して、JavaScript 用の AzureLoadTesting クライアント REST クライアント ライブラリを npmインストールします。

npm install @azure-rest/load-testing

クライアントを作成して認証するAzureLoadTesting

Azure Active Directory (AAD) トークン資格情報を使用するには、@azure/ID ライブラリから取得した目的の資格情報の種類のインスタンスを指定します。

AAD で認証するには、最初 npm に をインストールする必要があります。 @azure/identity

セットアップ後、使用する資格情報@azure/identityの種類を選択できます。 たとえば、 DefaultAzureCredential を使用してクライアントを認証できます。

AAD アプリケーションのクライアント ID、テナント ID、クライアント シークレットの値を環境変数として設定します(AZURE_CLIENT_ID、AZURE_TENANT_ID、AZURE_CLIENT_SECRET

import AzureLoadTesting, { AzureLoadTestingClient } from "@azure-rest/load-testing";
import { DefaultAzureCredential } from "@azure/identity";

const Client: AzureLoadTestingClient = AzureLoadTesting(Endpoint, new DefaultAzureCredential());

主要な概念

次のコンポーネントは、Azure Load Testing サービスを構成します。 JavaScript 用 Azure Load Test クライアント ライブラリを使用すると、専用のクライアント オブジェクトを使用して、これらの各コンポーネントを操作できます。

テスト

テストでは、テスト スクリプトと、ロード テストを実行するための構成設定を指定します。 Azure Load Testing リソースに 1 つ以上のテストを作成できます。

アプリ コンポーネント

Azure でホストされるアプリケーションに対してロード テストを実行すると、さまざまな Azure アプリケーション コンポーネント (サーバー側のメトリック) のリソース メトリックを監視できます。 ロード テストの実行中、そしてテストの完了後に、Azure Load Testing ダッシュボードでリソース メトリックを監視および分析できます。

メトリック

ロード テストでは、Azure Load Testing によってテストの実行に関するメトリックが収集されます。 メトリックには、次の 2 種類があります。

  1. クライアント側のメトリックでは、テスト エンジンによって報告された詳細が表示されます。 これらのメトリックには、仮想ユーザーの数、要求の応答時間、失敗した要求の数、1 秒あたりの要求数が含まれます。

  2. Azure でホストされるアプリケーションでは、サーバー側のメトリックを使用して、Azure アプリケーション コンポーネントに関する情報を提供できます。 メトリックは、データベースの読み取り数、HTTP 応答の種類、またはコンテナー リソースの消費量に対して指定できます。

テスト エンジン

テスト エンジンは、Apache JMeter テスト スクリプトを実行するコンピューティング インフラストラクチャです。 テスト エンジンの数を構成することによって、ロード テストをスケール アウトできます。 テスト スクリプトは、指定された数のテスト エンジンに対して並列に実行されます。

テストの実行

テストの実行は、1 回のロード テストの実行を表します。 Apache JMeter スクリプトの実行に関連付けられているログ、ロード テストの YAML 構成、監視するアプリ コンポーネントの一覧、テストの結果を収集します。

Data-Plane エンドポイント

Azure Load Testing リソースのデータ プレーンは、次の URL 形式を使用してアドレス指定できます。

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

最初の GUID 00000000-0000-0000-0000-000000000000 は、Azure Load Testing リソースへのアクセスに使用される一意の識別子です。 その後に、リソースの Azure リージョンが続きます aaa

データ プレーン エンドポイントは、コントロール プレーン API から取得されます。

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

上記の例では、 eus は Azure リージョン East USを表しています。

ロード テストの作成

import { AzureLoadTestingClient } from "@azure-rest/load-testing";
import AzureLoadTesting from "@azure-rest/load-testing";
import { DefaultAzureCredential } from "@azure/identity";

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

const client: AzureLoadTestingClient = AzureLoadTesting("<Endpoint>", new DefaultAzureCredential());

await client.path("/tests/{testId}", TEST_ID).patch({
  contentType: "application/merge-patch+json",
  body: {
    displayName: DISPLAY_NAME,
    description: "",
    loadTestConfiguration: {
      engineInstances: 1,
      splitAllCSVs: false,
    },
    secrets: {},
    environmentVariables: {},
    passFailCriteria: { passFailMetrics: {} },
  },
});

.jmx ファイルをテストにアップロードする

import { AzureLoadTestingClient, getLongRunningPoller, isUnexpected } from "@azure-rest/load-testing";
import AzureLoadTesting from "@azure-rest/load-testing";
import { AbortController } from "@azure/abort-controller";
import { DefaultAzureCredential } from "@azure/identity";
import { createReadStream } from "fs";

const client: AzureLoadTestingClient = AzureLoadTesting("<Endpoint>", new DefaultAzureCredential());

var TEST_ID = "some-test-id";
const readStream = createReadStream("./sample.jmx");

const fileUploadResult = await client
  .path("/tests/{testId}/files/{fileName}", TEST_ID, "sample.jmx")
  .put({
    contentType: "application/octet-stream",
    body: readStream,
  });

if (isUnexpected(fileUploadResult)) {
  throw fileUploadResult.body.error;
}

let fileValidateResult;
const fileValidatePoller = await getLongRunningPoller(client, fileUploadResult);
try{
fileValidateResult = await fileValidatePoller.pollUntilDone({
  abortSignal: AbortController.timeout(120*1000), // timeout of 120 seconds
});} catch (ex: any) {
new Error("Error in polling file Validation" + ex.message); //polling timed out
}

if (fileValidatePoller.getOperationState().status != "succeeded" && fileValidateResult)
  throw new Error(
    "There is some issue in validation, please make sure uploaded file is a valid JMX." +
      fileValidateResult.body.validationFailureDetails
  );

テストの実行とメトリックのフェッチ

import { AzureLoadTestingClient, getLongRunningPoller, isUnexpected } from "@azure-rest/load-testing";
import AzureLoadTesting from "@azure-rest/load-testing";
import { DefaultAzureCredential } from "@azure/identity";
import { AbortController } from "@azure/abort-controller";

const client: AzureLoadTestingClient = AzureLoadTesting("<Endpoint>", new DefaultAzureCredential());

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

// Creating/Updating the test run
const testRunCreationResult = await client.path("/test-runs/{testRunId}", TEST_RUN_ID).patch({
  contentType: "application/merge-patch+json",
  body: {
    testId: TEST_ID,
    displayName: DISPLAY_NAME,
  },
});

if (isUnexpected(testRunCreationResult)) {
  throw testRunCreationResult.body.error;
}

if (testRunCreationResult.body.testRunId === undefined)
  throw new Error("Test Run ID returned as undefined.");

const testRunPoller = await getLongRunningPoller(client, testRunCreationResult);
  let testRunResult;

  try {
    testRunResult = await testRunPoller.pollUntilDone({
      abortSignal: AbortController.timeout(60000), // timeout of 60 seconds
    });
  } catch (ex: any) {
    new Error("Error in polling test run completion" + ex.message); //polling timed out
  }

  if (testRunPoller.getOperationState().status != "succeeded")
    throw new Error("There is some issue in running the test, Error Response : " + testRunResult);

  if (testRunResult) {
    let testRunStarttime = testRunResult.body.startDateTime;
    let testRunEndTime = testRunResult.body.endDateTime;

    // get list of all metric namespaces and pick the first one
    const metricNamespaces = await client
      .path("/test-runs/{testRunId}/metric-namespaces", TEST_RUN_ID)
      .get();

    if (isUnexpected(metricNamespaces)) {
      throw metricNamespaces.body.error;
    }

    const metricNamespace = metricNamespaces.body.value[0];

    if (metricNamespace.name === undefined) {
      throw "No Metric Namespace name is defined.";
    }

    // get list of all metric definitions and pick the first one
    const metricDefinitions = await client
      .path("/test-runs/{testRunId}/metric-definitions", TEST_RUN_ID)
      .get({
        queryParameters: {
          metricNamespace: metricNamespace.name,
        },
      });

    if (isUnexpected(metricDefinitions)) {
      throw metricDefinitions.body.error;
    }

    const metricDefinition = metricDefinitions.body.value[0];

    if (metricDefinition.name === undefined) {
      throw "No Metric Namespace name is defined.";
    }

    // fetch client metrics using metric namespace and metric name
    const metricsResult = await client.path("/test-runs/{testRunId}/metrics", TEST_RUN_ID).post({
      queryParameters: {
        metricname: metricDefinition.name,
        metricNamespace: metricNamespace.name,
        timespan: testRunStarttime + "/" + testRunEndTime,
      },
    });

    console.log(metricsResult);
    console.log(testRunResult);
}

トラブルシューティング

ログ記録

ログの記録を有効にすると、エラーに関する有用な情報を明らかにするのに役立つ場合があります。 HTTP 要求と応答のログを表示するには、環境変数 AZURE_LOG_LEVELinfo に設定します。 または、@azure/loggersetLogLevel を呼び出して、実行時にログ記録を有効にすることもできます。

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

ログを有効にする方法の詳細については、@azure/logger パッケージに関するドキュメントを参照してください。

次のステップ

Azure Loading Testing JavaScript SDK サンプルは、SDK の GitHub リポジトリで入手できます。 これらのサンプルでは、一般的に発生するその他のシナリオのコード例を示します。

「Azure Load Testing のサンプル」を参照してください。

共同作成

このリポジトリへの投稿の詳細については、 投稿ガイドを参照してください。

  1. フォーク
  2. 機能ブランチを作成する (git checkout -b my-new-feature)
  3. 変更をコミットする (git commit -am 'Add some feature')
  4. ブランチにプッシュする (git push origin my-new-feature)
  5. 新しい Pull Request を作成する