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

Error handling in custom benchmark #22245

Merged
merged 26 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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 examples/benchmarks/tablebench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"test": "npm run test:mocha",
"test:benchmark:report": "mocha --exit --perfMode --parentProcess --fgrep @Benchmark --reporter @fluid-tools/benchmark/dist/MochaReporter.js --timeout 60000",
"test:customBenchmarks": "mocha --config ./.mocharc.customBenchmarks.cjs",
"test:customBenchmarks:verbose": "cross-env FLUID_TEST_VERBOSE=1 npm run test:customBenchmarks",
"test:mocha": "npm run test:mocha:esm",
"test:mocha:esm": "mocha --exit",
"test:mocha:verbose": "cross-env FLUID_TEST_VERBOSE=1 npm run test:mocha",
Expand Down
1 change: 1 addition & 0 deletions packages/dds/tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"test:benchmark:report": "mocha --exit --perfMode --parentProcess --fgrep @Benchmark --reporter @fluid-tools/benchmark/dist/MochaReporter.js --timeout 60000",
"test:coverage": "c8 npm test",
"test:customBenchmarks": "mocha --config ./.mocharc.customBenchmarks.cjs",
"test:customBenchmarks:verbose": "cross-env FLUID_TEST_VERBOSE=1 npm run test:customBenchmarks",
"test:memory": "mocha --config ./src/test/memory/.mocharc.cjs",
"test:memory-profiling:report": "mocha --config ./src/test/memory/.mocharc.cjs",
"test:mocha": "npm run test:mocha:esm && echo skipping cjs to avoid overhead - npm run test:mocha:cjs",
Expand Down
14 changes: 12 additions & 2 deletions tools/benchmark/src/mocha/customOutputRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Test } from "mocha";

import type { BenchmarkDescription, MochaExclusiveOptions, Titled } from "../Configuration";
import type { BenchmarkData, CustomData } from "../ResultTypes";
import type { BenchmarkData, BenchmarkError, CustomData } from "../ResultTypes";
import { prettyNumber } from "../RunnerUtilities";
import { timer } from "../timer";

Expand Down Expand Up @@ -49,7 +49,17 @@ export function benchmarkCustom(options: CustomBenchmarkOptions): Test {
};

const startTime = timer.now();
await options.run(reporter);

try {
await options.run(reporter);
} catch (error) {
const toBenchmarkError: BenchmarkError = { error: (error as Error).message };

// Instead of throwing the error, we emit it as an event so the test can continue.
test.emit("benchmark end", toBenchmarkError);
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
throw error;
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
}

const elapsedSeconds = timer.toSeconds(startTime, timer.now());

const results: BenchmarkData = {
Expand Down
29 changes: 28 additions & 1 deletion tools/benchmark/src/test/CustomOptionBenchmark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { benchmarkCustom } from "..";
import { benchmarkCustom, type BenchmarkError } from "..";
import { BenchmarkType } from "../Configuration";

describe("`benchmarkCustom` function", () => {
Expand All @@ -17,3 +17,30 @@ describe("`benchmarkCustom` function", () => {
});
});
});

describe("BenchmarkCustom error handling", () => {
const expectedErrorMessage = "INTENTIONAL error to test error handling";
let benchmarkEndPayloadIsCorrect: boolean = false;

const testObject = benchmarkCustom({
title: `test`,
type: BenchmarkType.Measurement,
run: async () => {
throw new Error(expectedErrorMessage);
},
});

testObject.on("benchmark end", (benchmarkError: BenchmarkError) => {
if (benchmarkError.error === expectedErrorMessage) {
benchmarkEndPayloadIsCorrect = true;
}
});

afterEach(function () {
if (benchmarkEndPayloadIsCorrect) {
this.skip();
} else {
throw new Error("The benchmark end payload was not correct.");
}
jikim-msft marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading