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 Tests] Helper methods from perf framework #13012

Merged
2 commits merged into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/test-utils/perfstress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./options";
export * from "./policy";
export * from "./parallel";
export * from "./program";
export { getEnvVar } from "./utils";
34 changes: 34 additions & 0 deletions sdk/test-utils/perfstress/src/parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,37 @@ export interface PerfStressParallel {
*/
lastMillisecondsElapsed: number;
}

/**
* Repeats the "async function" task for a "count" number of times by awaiting on
* "parallel" number of while loops in which each iteration executes the provided function once.
*
* @export
* @param {(count: number, parallelIndex: number) => Promise<void>} func "async function" task to be executed repeatedly
* @param {number} count Number of the times the func to be executed
* @param {number} parallel Number of parallel while loops to iterate over the function task
*/
export async function executeParallel(
func: (count: number, parallelIndex: number) => Promise<void>,
count: number,
parallel: number
): Promise<void> {
async function executeParallelHelper(
func: (count: number, parallelIndex: number) => Promise<void>,
count: number,
parallelIndex: number,
completed: { count: number }
) {
while (completed.count < count) {
const currentCount = completed.count++;
await func(currentCount, parallelIndex);
}
}

var completed = { count: 0 };
const tasks = [];
for (let i = 0; i < parallel; i++) {
tasks.push(executeParallelHelper(func, count, i, completed));
}
await Promise.all(tasks);
}
16 changes: 16 additions & 0 deletions sdk/test-utils/perfstress/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/**
* Returns the environment variable, throws an error if not defined.
*
* @export
* @param {string} name
*/
export function getEnvVar(name: string) {
const val = process.env[name];
if (!val) {
throw `Environment variable ${name} is not defined.`;
}
return val;
}