Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Perf] Call runAsync() once before starting recording #17993

Merged
merged 12 commits into from
Oct 2, 2021
1 change: 1 addition & 0 deletions sdk/test-utils/perfstress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/minimist": "~1.2.0"
},
"devDependencies": {
"@azure/core-client":"^1.3.1",
mikeharder marked this conversation as resolved.
Show resolved Hide resolved
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@types/node": "^12.0.0",
"@types/node-fetch": "^2.5.0",
Expand Down
3 changes: 3 additions & 0 deletions sdk/test-utils/perfstress/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
mikeharder marked this conversation as resolved.
Show resolved Hide resolved

await recorder.startRecording();
recorder._mode = "record";
await test.runAsync!();
Expand Down
4 changes: 3 additions & 1 deletion sdk/test-utils/perfstress/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===");

Expand All @@ -24,7 +25,8 @@ const perfStressProgram = new PerfStressProgram(
Exception,
PerfStressPolicyTest,
SleepTest,
NodeFetchTest
NodeFetchTest,
ServiceClientGetTest
])
);

Expand Down
4 changes: 2 additions & 2 deletions sdk/test-utils/perfstress/test/nodeFetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export class NodeFetchTest extends PerfStressTest<NodeFetchOptions> {
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"
mikeharder marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down
66 changes: 66 additions & 0 deletions sdk/test-utils/perfstress/test/serviceClientGet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation.
mikeharder marked this conversation as resolved.
Show resolved Hide resolved
// 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;
sadasant marked this conversation as resolved.
Show resolved Hide resolved
url: string;
}

export class ServiceClientGetTest extends PerfStressTest<ServiceClientGetOptions> {
client: ServiceClient;
request: PipelineRequest;
firstRun: boolean = true;

public options: PerfStressOptionDictionary<ServiceClientGetOptions> = {
"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<void> {
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!);
}
}