mirror of
https://github.com/hanzoai/azure-sdk-for-js.git
synced 2026-10-30 19:51:51 +00:00
Generated new sdk for data plane api version 2025-11-01-preview SDK is generated with the help of typespec API spec change PR - https://github.com/Azure/azure-rest-api-specs/pull/39372 ### Packages impacted by this PR @azure-rest/load-testing ### Issues associated with this PR ### Describe the problem that is addressed by this PR SDK for new data plane api version ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ API spec change PR - https://github.com/Azure/azure-rest-api-specs/pull/39372 ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ Referred this doc for sdk generation - https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/RLC-quickstart.md Updated tsp-location.yaml and ran tsp-client update ### Checklists - [x] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [x] Added a changelog (if necessary)
115 lines
3.8 KiB
JavaScript
115 lines
3.8 KiB
JavaScript
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
/**
|
|
* This sample demonstrates how to run a test, poll for its status and then fetch metrics from it.
|
|
*
|
|
* @summary Demonstrates how to start a test run and get metrics.
|
|
*/
|
|
|
|
const AzureLoadTesting = require("@azure-rest/load-testing").default,
|
|
{ isUnexpected, getLongRunningPoller } = require("@azure-rest/load-testing");
|
|
const { DefaultAzureCredential } = require("@azure/identity");
|
|
const { randomUUID } = require("node:crypto");
|
|
|
|
async function main() {
|
|
/**
|
|
* The dataplane endpoint for the Azure Load Testing resource.
|
|
* Refer to https://learn.microsoft.com/rest/api/apptesting/loadtest/data-plane-uri to understand how to obtain the data-plane endpoint.
|
|
*/
|
|
const endpoint = process.env["LOADTESTSERVICE_ENDPOINT"] || "";
|
|
|
|
/** Microsoft Entra ID authentication */
|
|
/**
|
|
* In this sample you can populate the three AZURE_CLIENT_ID, AZURE_CLIENT_SECRET & AZURE_TENANT_ID variables for Microsoft Entra ID auth
|
|
*/
|
|
const credential = new DefaultAzureCredential();
|
|
const testId = process.env["LOADTESTSERVICE_TESTID"] || ""; // TestId of a test already created.
|
|
|
|
const testRunId = randomUUID(); // Random ID for the Test Run
|
|
const displayName = "Sample Test Run";
|
|
|
|
// Build a client through AAD
|
|
const client = AzureLoadTesting(endpoint, credential);
|
|
|
|
// Creating the test run
|
|
const testRunCreationResult = await client.path("/test-runs/{testRunId}", testRunId).patch({
|
|
contentType: "application/merge-patch+json",
|
|
body: {
|
|
testId: testId,
|
|
displayName: displayName,
|
|
},
|
|
});
|
|
|
|
if (isUnexpected(testRunCreationResult)) {
|
|
throw testRunCreationResult.body.error;
|
|
}
|
|
|
|
let testRunResult;
|
|
|
|
const testRunPoller = await getLongRunningPoller(client, testRunCreationResult);
|
|
try {
|
|
testRunResult = await testRunPoller.pollUntilDone({
|
|
abortSignal: AbortSignal.timeout(90000), // timeout of 90 seconds
|
|
});
|
|
} catch (ex) {
|
|
new Error("Error in polling for 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) {
|
|
const testRunStartTime = testRunResult.body.startDateTime;
|
|
const testRunEndTime = testRunResult.body.endDateTime;
|
|
|
|
// Get the list of all metric namespaces and pick the first one
|
|
const metricNamespaces = await client
|
|
.path("/test-runs/{testRunId}/metric-namespaces", testRunId)
|
|
.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 the list of all metric definitions and pick the first one
|
|
const metricDefinitions = await client
|
|
.path("/test-runs/{testRunId}/metric-definitions", testRunId)
|
|
.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 Definition name is defined.";
|
|
}
|
|
|
|
// Fetch client metrics using metric namespace and metric definition name
|
|
const metricsResult = await client.path("/test-runs/{testRunId}/metrics", testRunId).post({
|
|
queryParameters: {
|
|
metricname: metricDefinition.name,
|
|
metricNamespace: metricNamespace.name,
|
|
timespan: testRunStartTime + "/" + testRunEndTime,
|
|
},
|
|
});
|
|
|
|
console.log(metricsResult);
|
|
console.log(testRunResult);
|
|
}
|
|
}
|
|
main().catch(console.error);
|