diff --git a/sdk/test-utils/perfstress/CHANGELOG.md b/sdk/test-utils/perfstress/CHANGELOG.md index e84991cca087..53b38c0cd57e 100644 --- a/sdk/test-utils/perfstress/CHANGELOG.md +++ b/sdk/test-utils/perfstress/CHANGELOG.md @@ -2,6 +2,11 @@ ## 1.0.0 (Unreleased) +### 2021-10-01 + +- Calls runAsync() once before starting recording, to avoid capturing one-time setup like authorization requests. + [#17993](https://github.com/Azure/azure-sdk-for-js/pull/17993) + ### 2021-09-29 - Allows connecting to the proxy-tool with https with the "insecure" boolean option. diff --git a/sdk/test-utils/perfstress/package.json b/sdk/test-utils/perfstress/package.json index e544f3c811f4..df62c99409a5 100644 --- a/sdk/test-utils/perfstress/package.json +++ b/sdk/test-utils/perfstress/package.json @@ -68,6 +68,7 @@ "@types/minimist": "~1.2.0" }, "devDependencies": { + "@azure/core-client": "^1.3.1", "@azure/eslint-plugin-azure-sdk": "^3.0.0", "@types/node": "^12.0.0", "@types/node-fetch": "^2.5.0", diff --git a/sdk/test-utils/perfstress/src/program.ts b/sdk/test-utils/perfstress/src/program.ts index 418a242917bb..36492c6546b2 100644 --- a/sdk/test-utils/perfstress/src/program.ts +++ b/sdk/test-utils/perfstress/src/program.ts @@ -366,6 +366,9 @@ export class PerfStressProgram { ); } + // Call Run() once before starting recording, to avoid capturing one-time setup like authorization requests. + await test.runAsync!(); + await recorder.startRecording(); recorder._mode = "record"; await test.runAsync!(); diff --git a/sdk/test-utils/perfstress/test/index.spec.ts b/sdk/test-utils/perfstress/test/index.spec.ts index cc4e90aab257..d9bfd7df2840 100644 --- a/sdk/test-utils/perfstress/test/index.spec.ts +++ b/sdk/test-utils/perfstress/test/index.spec.ts @@ -12,6 +12,7 @@ import { Exception } from "./exception.spec"; import { PerfStressPolicyTest } from "./perfStressPolicy.spec"; import { SleepTest } from "./sleep.spec"; import { NodeFetchTest } from "./nodeFetch.spec"; +import { ServiceClientGetTest } from "./serviceClientGet.spec"; console.log("=== Starting the perfStress test ==="); @@ -24,7 +25,8 @@ const perfStressProgram = new PerfStressProgram( Exception, PerfStressPolicyTest, SleepTest, - NodeFetchTest + NodeFetchTest, + ServiceClientGetTest ]) ); diff --git a/sdk/test-utils/perfstress/test/nodeFetch.spec.ts b/sdk/test-utils/perfstress/test/nodeFetch.spec.ts index 34df4ae4b7a1..55fd91a57437 100644 --- a/sdk/test-utils/perfstress/test/nodeFetch.spec.ts +++ b/sdk/test-utils/perfstress/test/nodeFetch.spec.ts @@ -23,8 +23,8 @@ export class NodeFetchTest extends PerfStressTest { description: "Required option", shortName: "u", longName: "url", - defaultValue: "http://bing.com", - value: "http://bing.com" + defaultValue: "http://www.example.org", + value: "http://www.example.org" } }; diff --git a/sdk/test-utils/perfstress/test/serviceClientGet.spec.ts b/sdk/test-utils/perfstress/test/serviceClientGet.spec.ts new file mode 100644 index 000000000000..6dab2cb656a8 --- /dev/null +++ b/sdk/test-utils/perfstress/test/serviceClientGet.spec.ts @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import { createPipelineRequest, PipelineRequest } from "@azure/core-rest-pipeline"; +import { ServiceClient } from "@azure/core-client"; +import { PerfStressTest, PerfStressOptionDictionary, drainStream } from "../src"; +import { getCachedHttpsAgent } from "../src/utils"; + +interface ServiceClientGetOptions { + "first-run-extra-requests": number; + url: string; +} + +export class ServiceClientGetTest extends PerfStressTest { + client: ServiceClient; + request: PipelineRequest; + firstRun: boolean = true; + + public options: PerfStressOptionDictionary = { + "first-run-extra-requests": { + description: + "Extra requests to send on first run. " + + "Simulates SDKs which require extra requests (like authentication) on first API call.", + defaultValue: 0 + }, + url: { + required: true, + description: "URL to retrieve", + shortName: "u", + longName: "url" + } + }; + + constructor() { + super(); + + const url = this.parsedOptions.url.value as string; + const insecure = this.parsedOptions.insecure.value as boolean; + + this.client = this.configureClient(new ServiceClient()); + this.request = createPipelineRequest({ + allowInsecureConnection: true, + streamResponseStatusCodes: new Set([200]), + url: url + }); + + if (insecure && url.toLowerCase().startsWith("https:")) { + this.request.agent = getCachedHttpsAgent(true); + } + } + + async runAsync(): Promise { + var response; + + if (this.firstRun) { + const extraRequests = this.parsedOptions["first-run-extra-requests"].value as number; + for (var i = 0; i < extraRequests; i++) { + response = await this.client.sendRequest(this.request); + await drainStream(response.readableStreamBody!); + } + this.firstRun = false; + } + + response = await this.client.sendRequest(this.request); + await drainStream(response.readableStreamBody!); + } +}