Skip to content

Commit

Permalink
[Perf] Call runAsync() once before starting recording (#17993)
Browse files Browse the repository at this point in the history
- Avoids capturing one-time setup like authorization requests
- Adds ServiceClientGetTest for directly testing the core-client features
  • Loading branch information
mikeharder authored Oct 2, 2021
1 parent adf7cf9 commit 2186357
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 deletions.
5 changes: 5 additions & 0 deletions sdk/test-utils/perfstress/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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",
"@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!();

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"
}
};

Expand Down
67 changes: 67 additions & 0 deletions sdk/test-utils/perfstress/test/serviceClientGet.spec.ts
Original file line number Diff line number Diff line change
@@ -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<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!);
}
}

0 comments on commit 2186357

Please sign in to comment.