From 02865cb5a270b9a2229863e83582f2a8b5115b78 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Mon, 31 Jul 2023 11:02:59 +0100 Subject: feat(bench): add BenchContext::start() and BenchContext::end() (#18734) Closes #17589. ```ts Deno.bench("foo", async (t) => { const resource = setup(); // not included in measurement t.start(); measuredOperation(resource); t.end(); resource.close(); // not included in measurement }); ``` --- cli/tsc/dts/lib.deno.ns.d.ts | 56 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) (limited to 'cli/tsc/dts/lib.deno.ns.d.ts') diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index cba6edb3b..1c8d9db63 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -995,6 +995,48 @@ declare namespace Deno { fn: (t: TestContext) => void | Promise, ): void; + /** + * Context that is passed to a benchmarked function. The instance is shared + * between iterations of the benchmark. Its methods can be used for example + * to override of the measured portion of the function. + * + * @category Testing + */ + export interface BenchContext { + /** The current benchmark name. */ + name: string; + /** The string URL of the current benchmark. */ + origin: string; + + /** Restarts the timer for the bench measurement. This should be called + * after doing setup work which should not be measured. + * + * ```ts + * Deno.bench("foo", async (t) => { + * const data = await Deno.readFile("data.txt"); + * t.start(); + * // some operation on `data`... + * }); + * ``` + */ + start(): void; + + /** End the timer early for the bench measurement. This should be called + * before doing teardown work which should not be measured. + * + * ```ts + * Deno.bench("foo", async (t) => { + * const file = await Deno.open("data.txt"); + * t.start(); + * // some operation on `file`... + * t.end(); + * file.close(); + * }); + * ``` + */ + end(): void; + } + /** * The interface for defining a benchmark test using {@linkcode Deno.bench}. * @@ -1002,7 +1044,7 @@ declare namespace Deno { */ export interface BenchDefinition { /** The test function which will be benchmarked. */ - fn: () => void | Promise; + fn: (b: BenchContext) => void | Promise; /** The name of the test, which will be used in displaying the results. */ name: string; /** If truthy, the benchmark test will be ignored/skipped. */ @@ -1073,7 +1115,7 @@ declare namespace Deno { * * @category Testing */ - export function bench(t: BenchDefinition): void; + export function bench(b: BenchDefinition): void; /** * Register a benchmark test which will be run when `deno bench` is used on @@ -1100,7 +1142,7 @@ declare namespace Deno { */ export function bench( name: string, - fn: () => void | Promise, + fn: (b: BenchContext) => void | Promise, ): void; /** @@ -1126,7 +1168,7 @@ declare namespace Deno { * * @category Testing */ - export function bench(fn: () => void | Promise): void; + export function bench(fn: (b: BenchContext) => void | Promise): void; /** * Register a benchmark test which will be run when `deno bench` is used on @@ -1162,7 +1204,7 @@ declare namespace Deno { export function bench( name: string, options: Omit, - fn: () => void | Promise, + fn: (b: BenchContext) => void | Promise, ): void; /** @@ -1196,7 +1238,7 @@ declare namespace Deno { */ export function bench( options: Omit, - fn: () => void | Promise, + fn: (b: BenchContext) => void | Promise, ): void; /** @@ -1230,7 +1272,7 @@ declare namespace Deno { */ export function bench( options: Omit, - fn: () => void | Promise, + fn: (b: BenchContext) => void | Promise, ): void; /** Exit the Deno process with optional exit code. -- cgit v1.2.3