|
| 1 | +/*! |
| 2 | + * Copyright (c) Microsoft Corporation and contributors. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +import { Test } from "mocha"; |
| 7 | + |
| 8 | +import type { BenchmarkDescription, MochaExclusiveOptions, Titled } from "../Configuration"; |
| 9 | +import type { BenchmarkData } from "../ResultTypes"; |
| 10 | +import { timer } from "../timer"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Options to configure a benchmark that reports custom measurements. |
| 14 | + * |
| 15 | + * @public |
| 16 | + */ |
| 17 | +export interface CustomBenchmarkOptions |
| 18 | + extends Titled, |
| 19 | + BenchmarkDescription, |
| 20 | + MochaExclusiveOptions { |
| 21 | + /** |
| 22 | + * Runs the benchmark. |
| 23 | + */ |
| 24 | + run: (reporter: IMeasurementReporter) => void | Promise<unknown>; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * This is a wrapper for Mocha's `it` function which runs the specified function {@link CustomBenchmarkOptions.run} |
| 29 | + * and gives it full control over the measurements that will be reported as benchmark output. |
| 30 | + * |
| 31 | + * @remarks |
| 32 | + * Tests created with this function get tagged with '\@CustomBenchmark', so mocha's --grep/--fgrep |
| 33 | + * options can be used to only run this type of tests by filtering on that value. |
| 34 | + * |
| 35 | + * @public |
| 36 | + */ |
| 37 | +export function benchmarkCustom(options: CustomBenchmarkOptions): Test { |
| 38 | + const itFunction = options.only === true ? it.only : it; |
| 39 | + const test = itFunction(`${options.title} @CustomBenchmark`, async () => { |
| 40 | + const customData: Record<string, number> = {}; |
| 41 | + const customDataFormatters: Record<string, (value: unknown) => string> = {}; |
| 42 | + const reporter: IMeasurementReporter = { |
| 43 | + addMeasurement: (key: string, value: number) => { |
| 44 | + if (key in customData) { |
| 45 | + throw new Error(`Measurement key '${key}' was already used.`); |
| 46 | + } |
| 47 | + customData[key] = value; |
| 48 | + }, |
| 49 | + }; |
| 50 | + |
| 51 | + const startTime = timer.now(); |
| 52 | + await options.run(reporter); |
| 53 | + const elapsedSeconds = timer.toSeconds(startTime, timer.now()); |
| 54 | + |
| 55 | + const results: BenchmarkData = { |
| 56 | + elapsedSeconds, |
| 57 | + customData, |
| 58 | + customDataFormatters, |
| 59 | + }; |
| 60 | + |
| 61 | + test.emit("benchmark end", results); |
| 62 | + }); |
| 63 | + return test; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Allows the benchmark code to report custom measurements. |
| 68 | + * |
| 69 | + * @public |
| 70 | + */ |
| 71 | +export interface IMeasurementReporter { |
| 72 | + /** |
| 73 | + * Adds a custom measurement to the benchmark output. |
| 74 | + * @param key - Key to uniquely identify the measurement. |
| 75 | + * @param value - Measurement value. |
| 76 | + * |
| 77 | + * @remarks |
| 78 | + * A given key should be used only once per benchmark. |
| 79 | + * Trying to add a measurement with a key that was already used will throw an error. |
| 80 | + */ |
| 81 | + addMeasurement(key: string, value: number): void; |
| 82 | +} |
0 commit comments