deno.land / std@0.157.0 / testing / bench.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/** * **Deprecated**. Use `Deno.bench()` instead. * * See: https://doc.deno.land/deno/unstable/~/Deno.bench for details. * * @deprecated (will be removed after 0.157.0) Use `Deno.bench()` instead. * @module */
import { assert } from "../_util/assert.ts";import { deepAssign } from "../_util/deep_assign.ts";
interface BenchmarkClock { start: number; stop: number; for?: string;}
/** 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; /** Defines how many times the provided `func` should be benchmarked in succession */ runs?: number;}
/** Defines runBenchmark's run constraints by matching benchmark names. */export interface BenchmarkRunOptions { /** Only benchmarks which name match this regexp will be run*/ only?: RegExp; /** Benchmarks which name match this regexp will be skipped */ skip?: RegExp; /** Setting it to true prevents default benchmarking progress logs to the commandline*/ silent?: boolean;}
/** 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 */export interface BenchmarkResult { /** The name of the benchmark */ name: string; /** The total time it took to run a given benchmark */ totalMs: number; /** 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[];}
/** Defines the result of a `runBenchmarks` call */export interface BenchmarkRunResult { /** How many benchmark were ignored by the provided `only` and `skip` */ filtered: number; /** The individual results for each benchmark that was run */ results: BenchmarkResult[];}
/** 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",}
export class BenchmarkRunError extends Error { benchmarkName?: string; constructor(msg: string, benchmarkName?: string) { super(msg); this.name = "BenchmarkRunError"; this.benchmarkName = benchmarkName; }}
function red(text: string): string { return Deno.noColor ? text : `\x1b[31m${text}\x1b[0m`;}
function blue(text: string): string { return Deno.noColor ? text : `\x1b[34m${text}\x1b[0m`;}
function verifyOr1Run(runs?: number): number { return runs && runs >= 1 && runs !== Infinity ? Math.floor(runs) : 1;}
function assertTiming(clock: BenchmarkClock) { // NaN indicates that a benchmark has not been timed properly if (!clock.stop) { throw new BenchmarkRunError( `Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's stop method must be called`, clock.for, ); } else if (!clock.start) { throw new BenchmarkRunError( `Running benchmarks FAILED during benchmark named [${clock.for}]. The benchmark timer's start method must be called`, clock.for, ); } else if (clock.start > clock.stop) { 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, ); }}
function createBenchmarkTimer(clock: BenchmarkClock): BenchmarkTimer { return { start() { clock.start = performance.now(); }, stop() { 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, ); } clock.stop = performance.now(); }, };}
const candidates: BenchmarkDefinition[] = [];
/** * @deprecated (will be removed after 0.157.0) Use `Deno.bench()` instead. See https://doc.deno.land/deno/unstable/~/Deno.bench * for details. * * Registers a benchmark as a candidate for the runBenchmarks executor. */export function bench( benchmark: BenchmarkDefinition | BenchmarkFunction,) { 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), func: benchmark.func, }); }}
/** * Clears benchmark candidates which name matches `only` and doesn't match `skip`. * Removes all candidates if options were not provided. * * @deprecated (will be removed after 0.157.0) Use `Deno.bench()` instead. See: https://doc.deno.land/deno/unstable/~/Deno.bench * for details. */export function clearBenchmarks({ only = /[^\s]/, skip = /$^/,}: BenchmarkClearOptions = {}) { const keep = candidates.filter( ({ name }): boolean => !only.test(name) || skip.test(name), ); candidates.splice(0, candidates.length); candidates.push(...keep);}
/** * @deprecated (will be removed after 0.157.0) Use `Deno.bench()` instead. See https://doc.deno.land/deno/unstable/~/Deno.bench * for details. * * 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 = {}, progressCb?: (progress: BenchmarkRunProgress) => void | Promise<void>,): Promise<BenchmarkRunResult> { // Filtering candidates by the "only" and "skip" constraint const benchmarks: BenchmarkDefinition[] = candidates.filter( ({ name }): boolean => only.test(name) && !skip.test(name), ); // Init main counters and error flag const filtered = candidates.length - benchmarks.length; let failError: Error | undefined = undefined; // Setting up a shared benchmark clock and timer const clock: BenchmarkClock = { start: NaN, stop: NaN }; const b = createBenchmarkTimer(clock);
// 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 await publishProgress(progress, ProgressState.BenchmarkingStart, progressCb);
if (!silent) { console.log( "running", benchmarks.length, `benchmark${benchmarks.length === 1 ? " ..." : "s ..."}`, ); }
// Iterating given benchmark definitions (await-in-loop) for (const { name, runs = 0, func } of benchmarks) { if (!silent) { // See https://github.com/denoland/deno/pull/1452 about groupCollapsed console.groupCollapsed(`benchmark ${name} ... `); }
// Provide the benchmark name for clock assertions clock.for = name;
// Remove benchmark from queued assert(progress.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 await publishProgress(progress, ProgressState.BenchStart, progressCb);
// Trying benchmark.func let result = ""; try { // Averaging runs let pendingRuns = runs; let totalMs = 0;
// Would be better 2 not run these serially while (true) { // b is a benchmark timer interfacing an unset (NaN) benchmark clock await func(b); // Making sure the benchmark was started/stopped properly assertTiming(clock);
// Calculate length of run const measuredMs = clock.stop - clock.start;
// Summing up totalMs += measuredMs; // Adding partial result progress.running.measuredRunsMs.push(measuredMs); // Publish partial benchmark results await publishProgress( progress, ProgressState.BenchPartialResult, progressCb, );
// 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 await publishProgress( progress, ProgressState.BenchResult, progressCb, ); break; } } } catch (err) { if (err instanceof Error) { failError = err;
if (!silent) { console.groupEnd(); console.error(red(err.stack ?? "")); } }
break; }
if (!silent) { // Reporting console.log(blue(result)); console.groupEnd(); }
// Resetting the benchmark clock clock.start = clock.stop = NaN; delete clock.for; }
// Indicate finished running delete progress.queued; // Publish final result in Cb too await publishProgress(progress, ProgressState.BenchmarkingEnd, progressCb);
if (!silent) { // Closing results console.log( `benchmark result: ${failError ? red("FAIL") : blue("DONE")}. ` + `${progress.results.length} measured; ${filtered} filtered`, ); }
// Throw error if there was a failing benchmark if (failError) { throw failError; }
const benchmarkRunResult = { filtered, results: progress.results, };
return benchmarkRunResult;}
async function publishProgress( progress: BenchmarkRunProgress, state: ProgressState, progressCb?: (progress: BenchmarkRunProgress) => void | Promise<void>,) { progressCb && (await progressCb(cloneProgressWithState(progress, state)));}
function cloneProgressWithState( progress: BenchmarkRunProgress, state: ProgressState,): BenchmarkRunProgress { return deepAssign({}, progress, { state }) as BenchmarkRunProgress;}
std

Version Info

Tagged at
a year ago