2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-02 17:06:13 -04:00
|
|
|
import { deepAssign } from "../_util/deep_assign.ts";
|
2019-03-11 14:21:13 -04:00
|
|
|
|
|
|
|
interface BenchmarkClock {
|
|
|
|
start: number;
|
|
|
|
stop: number;
|
2020-06-03 13:45:37 -04:00
|
|
|
for?: string;
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Provides methods for starting and stopping a benchmark clock. */
|
|
|
|
export interface BenchmarkTimer {
|
|
|
|
start: () => void;
|
|
|
|
stop: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Defines a benchmark through a named function. */
|
|
|
|
export interface BenchmarkFunction {
|
|
|
|
(b: BenchmarkTimer): void | Promise<void>;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Defines a benchmark definition with configurable runs. */
|
|
|
|
export interface BenchmarkDefinition {
|
|
|
|
func: BenchmarkFunction;
|
|
|
|
name: string;
|
2020-06-02 17:06:13 -04:00
|
|
|
/** Defines how many times the provided `func` should be benchmarked in succession */
|
2019-03-11 14:21:13 -04:00
|
|
|
runs?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Defines runBenchmark's run constraints by matching benchmark names. */
|
|
|
|
export interface BenchmarkRunOptions {
|
2020-06-02 17:06:13 -04:00
|
|
|
/** Only benchmarks which name match this regexp will be run*/
|
2019-03-11 14:21:13 -04:00
|
|
|
only?: RegExp;
|
2020-06-02 17:06:13 -04:00
|
|
|
/** Benchmarks which name match this regexp will be skipped */
|
2019-03-11 14:21:13 -04:00
|
|
|
skip?: RegExp;
|
2020-06-02 17:06:13 -04:00
|
|
|
/** Setting it to true prevents default benchmarking progress logs to the commandline*/
|
2020-05-29 02:29:58 -04:00
|
|
|
silent?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
/** Defines clearBenchmark's constraints by matching benchmark names. */
|
|
|
|
export interface BenchmarkClearOptions {
|
|
|
|
/** Only benchmarks which name match this regexp will be removed */
|
|
|
|
only?: RegExp;
|
|
|
|
/** Benchmarks which name match this regexp will be kept */
|
|
|
|
skip?: RegExp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Defines the result of a single benchmark */
|
2020-05-29 02:29:58 -04:00
|
|
|
export interface BenchmarkResult {
|
2020-06-02 17:06:13 -04:00
|
|
|
/** The name of the benchmark */
|
2020-05-29 02:29:58 -04:00
|
|
|
name: string;
|
2020-06-02 17:06:13 -04:00
|
|
|
/** The total time it took to run a given bechmark */
|
2020-05-29 02:29:58 -04:00
|
|
|
totalMs: number;
|
2020-06-03 22:33:53 -04:00
|
|
|
/** Times the benchmark was run in succession. */
|
|
|
|
runsCount: number;
|
|
|
|
/** The average time of running the benchmark in milliseconds. */
|
|
|
|
measuredRunsAvgMs: number;
|
|
|
|
/** The individual measurements in milliseconds it took to run the benchmark.*/
|
|
|
|
measuredRunsMs: number[];
|
2020-05-29 02:29:58 -04:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
/** Defines the result of a `runBenchmarks` call */
|
2020-05-29 02:29:58 -04:00
|
|
|
export interface BenchmarkRunResult {
|
2020-06-02 17:06:13 -04:00
|
|
|
/** How many benchmark were ignored by the provided `only` and `skip` */
|
2020-05-29 02:29:58 -04:00
|
|
|
filtered: number;
|
2020-06-02 17:06:13 -04:00
|
|
|
/** The individual results for each benchmark that was run */
|
2020-05-29 02:29:58 -04:00
|
|
|
results: BenchmarkResult[];
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
/** Defines the current progress during the run of `runBenchmarks` */
|
|
|
|
export interface BenchmarkRunProgress extends BenchmarkRunResult {
|
|
|
|
/** List of the queued benchmarks to run with their name and their run count */
|
|
|
|
queued: Array<{ name: string; runsCount: number }>;
|
|
|
|
/** The currently running benchmark with its name, run count and the already finished measurements in milliseconds */
|
|
|
|
running?: { name: string; runsCount: number; measuredRunsMs: number[] };
|
|
|
|
/** Indicates in which state benchmarking currently is */
|
|
|
|
state: ProgressState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Defines the states `BenchmarkRunProgress` can be in */
|
|
|
|
export enum ProgressState {
|
|
|
|
BenchmarkingStart = "benchmarking_start",
|
|
|
|
BenchStart = "bench_start",
|
|
|
|
BenchPartialResult = "bench_partial_result",
|
|
|
|
BenchResult = "bench_result",
|
|
|
|
BenchmarkingEnd = "benchmarking_end",
|
|
|
|
}
|
|
|
|
|
2020-05-29 02:29:58 -04:00
|
|
|
export class BenchmarkRunError extends Error {
|
|
|
|
benchmarkName?: string;
|
|
|
|
constructor(msg: string, benchmarkName?: string) {
|
|
|
|
super(msg);
|
|
|
|
this.name = "BenchmarkRunError";
|
|
|
|
this.benchmarkName = benchmarkName;
|
|
|
|
}
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function red(text: string): string {
|
2020-06-12 15:23:38 -04:00
|
|
|
return Deno.noColor ? text : `\x1b[31m${text}\x1b[0m`;
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function blue(text: string): string {
|
2020-06-12 15:23:38 -04:00
|
|
|
return Deno.noColor ? text : `\x1b[34m${text}\x1b[0m`;
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function verifyOr1Run(runs?: number): number {
|
|
|
|
return runs && runs >= 1 && runs !== Infinity ? Math.floor(runs) : 1;
|
|
|
|
}
|
|
|
|
|
2020-06-03 13:45:37 -04:00
|
|
|
function assertTiming(clock: BenchmarkClock): void {
|
2019-03-11 14:21:13 -04:00
|
|
|
// NaN indicates that a benchmark has not been timed properly
|
|
|
|
if (!clock.stop) {
|
2020-05-29 02:29:58 -04:00
|
|
|
throw new BenchmarkRunError(
|
2020-06-03 13:45:37 -04:00
|
|
|
`Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's stop method must be called`,
|
|
|
|
clock.for
|
2020-05-29 02:29:58 -04:00
|
|
|
);
|
2019-03-11 14:21:13 -04:00
|
|
|
} else if (!clock.start) {
|
2020-05-29 02:29:58 -04:00
|
|
|
throw new BenchmarkRunError(
|
2020-06-03 13:45:37 -04:00
|
|
|
`Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's start method must be called`,
|
|
|
|
clock.for
|
2020-05-29 02:29:58 -04:00
|
|
|
);
|
2019-03-11 14:21:13 -04:00
|
|
|
} else if (clock.start > clock.stop) {
|
2020-05-29 02:29:58 -04:00
|
|
|
throw new BenchmarkRunError(
|
2020-06-03 13:45:37 -04:00
|
|
|
`Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's start method must be called before its stop method`,
|
|
|
|
clock.for
|
2019-03-11 14:21:13 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createBenchmarkTimer(clock: BenchmarkClock): BenchmarkTimer {
|
|
|
|
return {
|
|
|
|
start(): void {
|
2019-05-10 10:48:06 -04:00
|
|
|
clock.start = performance.now();
|
2019-03-11 14:21:13 -04:00
|
|
|
},
|
|
|
|
stop(): void {
|
2020-06-03 13:45:37 -04:00
|
|
|
if (isNaN(clock.start)) {
|
|
|
|
throw new BenchmarkRunError(
|
|
|
|
`Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's start method must be called before its stop method`,
|
|
|
|
clock.for
|
|
|
|
);
|
|
|
|
}
|
2019-05-10 10:48:06 -04:00
|
|
|
clock.stop = performance.now();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-03-11 14:21:13 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const candidates: BenchmarkDefinition[] = [];
|
|
|
|
|
|
|
|
/** Registers a benchmark as a candidate for the runBenchmarks executor. */
|
|
|
|
export function bench(
|
|
|
|
benchmark: BenchmarkDefinition | BenchmarkFunction
|
|
|
|
): void {
|
|
|
|
if (!benchmark.name) {
|
|
|
|
throw new Error("The benchmark function must not be anonymous");
|
|
|
|
}
|
|
|
|
if (typeof benchmark === "function") {
|
|
|
|
candidates.push({ name: benchmark.name, runs: 1, func: benchmark });
|
|
|
|
} else {
|
|
|
|
candidates.push({
|
|
|
|
name: benchmark.name,
|
|
|
|
runs: verifyOr1Run(benchmark.runs),
|
2020-03-28 13:03:49 -04:00
|
|
|
func: benchmark.func,
|
2019-03-11 14:21:13 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
/** Clears benchmark candidates which name matches `only` and doesn't match `skip`.
|
|
|
|
* Removes all candidates if options were not provided */
|
|
|
|
export function clearBenchmarks({
|
2019-03-11 14:21:13 -04:00
|
|
|
only = /[^\s]/,
|
2020-06-02 17:06:13 -04:00
|
|
|
skip = /$^/,
|
|
|
|
}: BenchmarkClearOptions = {}): void {
|
|
|
|
const keep = candidates.filter(
|
|
|
|
({ name }): boolean => !only.test(name) || skip.test(name)
|
|
|
|
);
|
|
|
|
candidates.splice(0, candidates.length);
|
|
|
|
candidates.push(...keep);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs all registered and non-skipped benchmarks serially.
|
|
|
|
*
|
|
|
|
* @param [progressCb] provides the possibility to get updates of the current progress during the run of the benchmarking
|
|
|
|
* @returns results of the benchmarking
|
|
|
|
*/
|
|
|
|
export async function runBenchmarks(
|
|
|
|
{ only = /[^\s]/, skip = /^\s*$/, silent }: BenchmarkRunOptions = {},
|
2020-06-09 11:19:06 -04:00
|
|
|
progressCb?: (progress: BenchmarkRunProgress) => void | Promise<void>
|
2020-06-02 17:06:13 -04:00
|
|
|
): Promise<BenchmarkRunResult> {
|
2019-03-11 14:21:13 -04:00
|
|
|
// Filtering candidates by the "only" and "skip" constraint
|
|
|
|
const benchmarks: BenchmarkDefinition[] = candidates.filter(
|
2019-04-24 07:41:23 -04:00
|
|
|
({ name }): boolean => only.test(name) && !skip.test(name)
|
2019-03-11 14:21:13 -04:00
|
|
|
);
|
|
|
|
// Init main counters and error flag
|
|
|
|
const filtered = candidates.length - benchmarks.length;
|
2020-05-29 02:29:58 -04:00
|
|
|
let failError: Error | undefined = undefined;
|
2019-03-11 14:21:13 -04:00
|
|
|
// Setting up a shared benchmark clock and timer
|
|
|
|
const clock: BenchmarkClock = { start: NaN, stop: NaN };
|
|
|
|
const b = createBenchmarkTimer(clock);
|
2020-05-29 02:29:58 -04:00
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
// Init progress data
|
|
|
|
const progress: BenchmarkRunProgress = {
|
|
|
|
// bench.run is already ensured with verifyOr1Run on register
|
|
|
|
queued: benchmarks.map((bench) => ({
|
|
|
|
name: bench.name,
|
|
|
|
runsCount: bench.runs!,
|
|
|
|
})),
|
|
|
|
results: [],
|
|
|
|
filtered,
|
|
|
|
state: ProgressState.BenchmarkingStart,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Publish initial progress data
|
2020-06-09 11:19:06 -04:00
|
|
|
await publishProgress(progress, ProgressState.BenchmarkingStart, progressCb);
|
2020-06-02 17:06:13 -04:00
|
|
|
|
2020-05-29 02:29:58 -04:00
|
|
|
if (!silent) {
|
|
|
|
console.log(
|
|
|
|
"running",
|
|
|
|
benchmarks.length,
|
|
|
|
`benchmark${benchmarks.length === 1 ? " ..." : "s ..."}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
// Iterating given benchmark definitions (await-in-loop)
|
2019-03-11 14:21:13 -04:00
|
|
|
for (const { name, runs = 0, func } of benchmarks) {
|
2020-05-29 02:29:58 -04:00
|
|
|
if (!silent) {
|
|
|
|
// See https://github.com/denoland/deno/pull/1452 about groupCollapsed
|
|
|
|
console.groupCollapsed(`benchmark ${name} ... `);
|
|
|
|
}
|
|
|
|
|
2020-06-03 13:45:37 -04:00
|
|
|
// Provide the benchmark name for clock assertions
|
|
|
|
clock.for = name;
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
// Remove benchmark from queued
|
|
|
|
const queueIndex = progress.queued.findIndex(
|
|
|
|
(queued) => queued.name === name && queued.runsCount === runs
|
|
|
|
);
|
|
|
|
if (queueIndex != -1) {
|
|
|
|
progress.queued.splice(queueIndex, 1);
|
|
|
|
}
|
|
|
|
// Init the progress of the running benchmark
|
|
|
|
progress.running = { name, runsCount: runs, measuredRunsMs: [] };
|
|
|
|
// Publish starting of a benchmark
|
2020-06-09 11:19:06 -04:00
|
|
|
await publishProgress(progress, ProgressState.BenchStart, progressCb);
|
2020-06-02 17:06:13 -04:00
|
|
|
|
2019-03-11 14:21:13 -04:00
|
|
|
// Trying benchmark.func
|
|
|
|
let result = "";
|
|
|
|
try {
|
2020-06-03 22:33:53 -04:00
|
|
|
// Averaging runs
|
|
|
|
let pendingRuns = runs;
|
|
|
|
let totalMs = 0;
|
|
|
|
|
|
|
|
// Would be better 2 not run these serially
|
|
|
|
while (true) {
|
2019-03-11 14:21:13 -04:00
|
|
|
// b is a benchmark timer interfacing an unset (NaN) benchmark clock
|
|
|
|
await func(b);
|
|
|
|
// Making sure the benchmark was started/stopped properly
|
2020-06-03 13:45:37 -04:00
|
|
|
assertTiming(clock);
|
2020-06-03 22:33:53 -04:00
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
// Calculate length of run
|
|
|
|
const measuredMs = clock.stop - clock.start;
|
|
|
|
|
2020-06-03 22:33:53 -04:00
|
|
|
// Summing up
|
|
|
|
totalMs += measuredMs;
|
|
|
|
// Adding partial result
|
|
|
|
progress.running.measuredRunsMs.push(measuredMs);
|
|
|
|
// Publish partial benchmark results
|
2020-06-09 11:19:06 -04:00
|
|
|
await publishProgress(
|
|
|
|
progress,
|
|
|
|
ProgressState.BenchPartialResult,
|
|
|
|
progressCb
|
|
|
|
);
|
2020-06-03 22:33:53 -04:00
|
|
|
|
|
|
|
// Resetting the benchmark clock
|
|
|
|
clock.start = clock.stop = NaN;
|
|
|
|
// Once all ran
|
|
|
|
if (!--pendingRuns) {
|
|
|
|
result =
|
|
|
|
runs == 1
|
|
|
|
? `${totalMs}ms`
|
|
|
|
: `${runs} runs avg: ${totalMs / runs}ms`;
|
|
|
|
// Adding results
|
|
|
|
progress.results.push({
|
|
|
|
name,
|
|
|
|
totalMs,
|
|
|
|
runsCount: runs,
|
|
|
|
measuredRunsAvgMs: totalMs / runs,
|
|
|
|
measuredRunsMs: progress.running.measuredRunsMs,
|
|
|
|
});
|
|
|
|
// Clear currently running
|
|
|
|
delete progress.running;
|
|
|
|
// Publish results of the benchmark
|
2020-06-09 11:19:06 -04:00
|
|
|
await publishProgress(
|
|
|
|
progress,
|
|
|
|
ProgressState.BenchResult,
|
|
|
|
progressCb
|
|
|
|
);
|
2020-06-03 22:33:53 -04:00
|
|
|
break;
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2020-05-29 02:29:58 -04:00
|
|
|
failError = err;
|
|
|
|
|
|
|
|
if (!silent) {
|
|
|
|
console.groupEnd();
|
|
|
|
console.error(red(err.stack));
|
|
|
|
}
|
|
|
|
|
2019-03-11 14:21:13 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-05-29 02:29:58 -04:00
|
|
|
|
|
|
|
if (!silent) {
|
|
|
|
// Reporting
|
|
|
|
console.log(blue(result));
|
|
|
|
console.groupEnd();
|
|
|
|
}
|
|
|
|
|
2019-03-11 14:21:13 -04:00
|
|
|
// Resetting the benchmark clock
|
|
|
|
clock.start = clock.stop = NaN;
|
2020-06-03 13:45:37 -04:00
|
|
|
delete clock.for;
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
2020-05-29 02:29:58 -04:00
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
// Indicate finished running
|
|
|
|
delete progress.queued;
|
|
|
|
// Publish final result in Cb too
|
2020-06-09 11:19:06 -04:00
|
|
|
await publishProgress(progress, ProgressState.BenchmarkingEnd, progressCb);
|
2020-06-02 17:06:13 -04:00
|
|
|
|
2020-05-29 02:29:58 -04:00
|
|
|
if (!silent) {
|
|
|
|
// Closing results
|
|
|
|
console.log(
|
2020-07-01 10:04:56 -04:00
|
|
|
`benchmark result: ${failError ? red("FAIL") : blue("DONE")}. ` +
|
2020-06-02 17:06:13 -04:00
|
|
|
`${progress.results.length} measured; ${filtered} filtered`
|
2020-05-29 02:29:58 -04:00
|
|
|
);
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
// Throw error if there was a failing benchmark
|
2020-07-01 10:04:56 -04:00
|
|
|
if (failError) {
|
2020-05-29 02:29:58 -04:00
|
|
|
throw failError;
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
2020-05-29 02:29:58 -04:00
|
|
|
|
|
|
|
const benchmarkRunResult = {
|
|
|
|
filtered,
|
2020-06-02 17:06:13 -04:00
|
|
|
results: progress.results,
|
2020-05-29 02:29:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return benchmarkRunResult;
|
2019-03-11 14:21:13 -04:00
|
|
|
}
|
2020-06-02 17:06:13 -04:00
|
|
|
|
2020-06-09 11:19:06 -04:00
|
|
|
async function publishProgress(
|
2020-06-02 17:06:13 -04:00
|
|
|
progress: BenchmarkRunProgress,
|
|
|
|
state: ProgressState,
|
2020-06-09 11:19:06 -04:00
|
|
|
progressCb?: (progress: BenchmarkRunProgress) => void | Promise<void>
|
|
|
|
): Promise<void> {
|
|
|
|
progressCb && (await progressCb(cloneProgressWithState(progress, state)));
|
2020-06-02 17:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function cloneProgressWithState(
|
|
|
|
progress: BenchmarkRunProgress,
|
|
|
|
state: ProgressState
|
|
|
|
): BenchmarkRunProgress {
|
|
|
|
return deepAssign({}, progress, { state }) as BenchmarkRunProgress;
|
|
|
|
}
|