deno.land / std@0.107.0 / testing / bench_test.ts

bench_test.ts
View Documentation
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
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.import { bench, BenchmarkRunError, BenchmarkRunProgress, clearBenchmarks, ProgressState, runBenchmarks,} from "./bench.ts";import { assert, assertEquals, assertRejects, assertThrows,} from "./asserts.ts";
Deno.test({ name: "benching",
fn: async function () { bench(function forIncrementX1e3(b): void { b.start(); for (let i = 0; i < 1e3; i++); b.stop(); });
bench(function forDecrementX1e3(b): void { b.start(); for (let i = 1e3; i > 0; i--); b.stop(); });
bench(async function forAwaitFetchDenolandX10(b) { b.start(); for (let i = 0; i < 10; i++) { const r = await fetch("https://deno.land/"); await r.text(); } b.stop(); });
bench(async function promiseAllFetchDenolandX10(b) { const urls = new Array(10).fill("https://deno.land/"); b.start(); await Promise.all( urls.map( async (denoland: string) => { const r = await fetch(denoland); await r.text(); }, ), ); b.stop(); });
bench({ name: "runs100ForIncrementX1e6", runs: 100, func(b): void { b.start(); for (let i = 0; i < 1e6; i++); b.stop(); }, });
bench(function throwing(b): void { b.start(); // Throws bc the timer's stop method is never called });
const benchResult = await runBenchmarks({ skip: /throw/ });
assertEquals(benchResult.filtered, 1); assertEquals(benchResult.results.length, 5);
const resultWithSingleRunsFiltered = benchResult.results.filter( ({ name }) => name === "forDecrementX1e3", ); assertEquals(resultWithSingleRunsFiltered.length, 1);
const resultWithSingleRuns = resultWithSingleRunsFiltered[0]; assert(!!resultWithSingleRuns.runsCount); assert(!!resultWithSingleRuns.measuredRunsAvgMs); assert(!!resultWithSingleRuns.measuredRunsMs); assertEquals(resultWithSingleRuns.runsCount, 1); assertEquals(resultWithSingleRuns.measuredRunsMs.length, 1);
const resultWithMultipleRunsFiltered = benchResult.results.filter( ({ name }) => name === "runs100ForIncrementX1e6", ); assertEquals(resultWithMultipleRunsFiltered.length, 1);
const resultWithMultipleRuns = resultWithMultipleRunsFiltered[0]; assert(!!resultWithMultipleRuns.runsCount); assert(!!resultWithMultipleRuns.measuredRunsAvgMs); assert(!!resultWithMultipleRuns.measuredRunsMs); assertEquals(resultWithMultipleRuns.runsCount, 100); assertEquals(resultWithMultipleRuns.measuredRunsMs.length, 100);
clearBenchmarks(); },});
Deno.test({ name: "Bench without name should throw", fn() { assertThrows( (): void => { bench(() => {}); }, Error, "The benchmark function must not be anonymous", ); },});
Deno.test({ name: "Bench without stop should throw", fn: async function () { await assertRejects( async () => { bench(function benchWithoutStop(b): void { b.start(); // Throws bc the timer's stop method is never called }); await runBenchmarks({ only: /benchWithoutStop/, silent: true }); }, BenchmarkRunError, "The benchmark timer's stop method must be called", ); },});
Deno.test({ name: "Bench without start should throw", fn: async function () { await assertRejects( async () => { bench(function benchWithoutStart(b): void { b.stop(); // Throws bc the timer's start method is never called }); await runBenchmarks({ only: /benchWithoutStart/, silent: true }); }, BenchmarkRunError, "The benchmark timer's start method must be called", ); },});
Deno.test({ name: "Bench with stop before start should throw", fn: async function () { await assertRejects( async () => { bench(function benchStopBeforeStart(b): void { b.stop(); b.start(); // Throws bc the timer's stop is called before start }); await runBenchmarks({ only: /benchStopBeforeStart/, silent: true }); }, BenchmarkRunError, "The benchmark timer's start method must be called before its stop method", ); },});
Deno.test({ name: "clearBenchmarks should clear all candidates", fn: async function () { dummyBench("test");
clearBenchmarks(); const benchingResults = await runBenchmarks({ silent: true });
assertEquals(benchingResults.filtered, 0); assertEquals(benchingResults.results.length, 0); },});
Deno.test({ name: "clearBenchmarks with only as option", fn: async function () { // to reset candidates clearBenchmarks();
dummyBench("test"); dummyBench("onlyclear");
clearBenchmarks({ only: /only/ }); const benchingResults = await runBenchmarks({ silent: true });
assertEquals(benchingResults.filtered, 0); assertEquals(benchingResults.results.length, 1); assertEquals(benchingResults.results[0].name, "test"); },});
Deno.test({ name: "clearBenchmarks with skip as option", fn: async function () { // to reset candidates clearBenchmarks();
dummyBench("test"); dummyBench("skipclear");
clearBenchmarks({ skip: /skip/ }); const benchingResults = await runBenchmarks({ silent: true });
assertEquals(benchingResults.filtered, 0); assertEquals(benchingResults.results.length, 1); assertEquals(benchingResults.results[0].name, "skipclear"); },});
Deno.test({ name: "clearBenchmarks with only and skip as option", fn: async function () { // to reset candidates clearBenchmarks();
dummyBench("test"); dummyBench("clearonly"); dummyBench("clearskip"); dummyBench("clearonly");
clearBenchmarks({ only: /clear/, skip: /skip/ }); const benchingResults = await runBenchmarks({ silent: true });
assertEquals(benchingResults.filtered, 0); assertEquals(benchingResults.results.length, 2); assert(!!benchingResults.results.find(({ name }) => name === "test")); assert(!!benchingResults.results.find(({ name }) => name === "clearskip")); },});
Deno.test({ name: "progressCallback of runBenchmarks", fn: async function () { clearBenchmarks(); dummyBench("skip"); dummyBench("single"); dummyBench("multiple", 2);
const progressCallbacks: BenchmarkRunProgress[] = [];
const benchingResults = await runBenchmarks( { skip: /skip/, silent: true }, (progress) => { progressCallbacks.push(progress); }, );
let pc = 0; // Assert initial progress before running let progress = progressCallbacks[pc++]; assert(progress.queued); assertEquals(progress.state, ProgressState.BenchmarkingStart); assertEquals(progress.filtered, 1); assertEquals(progress.queued.length, 2); assertEquals(progress.running, undefined); assertEquals(progress.results, []);
// Assert start of bench "single" progress = progressCallbacks[pc++]; assertEquals(progress.state, ProgressState.BenchStart); assertEquals(progress.filtered, 1); assert(progress.queued); assertEquals(progress.queued.length, 1); assert(!!progress.queued.find(({ name }) => name == "multiple")); assertEquals(progress.running, { name: "single", runsCount: 1, measuredRunsMs: [], }); assertEquals(progress.results, []);
// Assert running result of bench "single" progress = progressCallbacks[pc++]; assertEquals(progress.state, ProgressState.BenchPartialResult); assert(progress.queued); assertEquals(progress.queued.length, 1); assertEquals(progress.running!.measuredRunsMs.length, 1); assertEquals(progress.results.length, 0);
// Assert result of bench "single" progress = progressCallbacks[pc++]; assertEquals(progress.state, ProgressState.BenchResult); assert(progress.queued); assertEquals(progress.queued.length, 1); assertEquals(progress.running, undefined); assertEquals(progress.results.length, 1); assert(!!progress.results.find(({ name }) => name == "single"));
// Assert start of bench "multiple" progress = progressCallbacks[pc++]; assertEquals(progress.state, ProgressState.BenchStart); assert(progress.queued); assertEquals(progress.queued.length, 0); assertEquals(progress.running, { name: "multiple", runsCount: 2, measuredRunsMs: [], }); assertEquals(progress.results.length, 1);
// Assert first result of bench "multiple" progress = progressCallbacks[pc++]; assertEquals(progress.state, ProgressState.BenchPartialResult); assert(progress.queued); assertEquals(progress.queued.length, 0); assertEquals(progress.running!.measuredRunsMs.length, 1); assertEquals(progress.results.length, 1);
// Assert second result of bench "multiple" progress = progressCallbacks[pc++]; assertEquals(progress.state, ProgressState.BenchPartialResult); assert(progress.queued); assertEquals(progress.queued.length, 0); assertEquals(progress.running!.measuredRunsMs.length, 2); assertEquals(progress.results.length, 1);
// Assert finish of bench "multiple" progress = progressCallbacks[pc++]; assertEquals(progress.state, ProgressState.BenchResult); assert(progress.queued); assertEquals(progress.queued.length, 0); assertEquals(progress.running, undefined); assertEquals(progress.results.length, 2); assert(!!progress.results.find(({ name }) => name == "single")); const resultOfMultiple = progress.results.filter( ({ name }) => name == "multiple", ); assertEquals(resultOfMultiple.length, 1); assert(!!resultOfMultiple[0].measuredRunsMs); assert(!isNaN(resultOfMultiple[0].measuredRunsAvgMs)); assertEquals(resultOfMultiple[0].measuredRunsMs.length, 2);
// The last progress should equal the final result from promise except the state property progress = progressCallbacks[pc++]; assertEquals(progress.state, ProgressState.BenchmarkingEnd); delete progress.state; assertEquals(progress, benchingResults); },});
Deno.test({ name: "async progressCallback", fn: async function () { clearBenchmarks(); dummyBench("single");
const asyncCallbacks = [];
await runBenchmarks({ silent: true }, (progress) => { return new Promise((resolve) => { queueMicrotask(() => { asyncCallbacks.push(progress); resolve(); }); }); });
assertEquals(asyncCallbacks.length, 5); },});
function dummyBench(name: string, runs = 1): void { bench({ name, runs, func(b) { b.start(); b.stop(); }, });}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2121

Version Info

Tagged at
8 months ago