2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-02 17:06:13 -04:00
|
|
|
import {
|
|
|
|
bench,
|
|
|
|
runBenchmarks,
|
|
|
|
BenchmarkRunError,
|
|
|
|
clearBenchmarks,
|
|
|
|
BenchmarkRunProgress,
|
|
|
|
ProgressState,
|
|
|
|
} from "./bench.ts";
|
2020-05-29 02:29:58 -04:00
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assert,
|
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync,
|
|
|
|
} from "./asserts.ts";
|
2019-02-12 10:55:01 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-18 19:25:55 -04:00
|
|
|
name: "benching",
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
fn: async function (): Promise<void> {
|
2020-08-26 14:36:20 -04:00
|
|
|
bench(function forIncrementX1e3(b): void {
|
2020-03-18 19:25:55 -04:00
|
|
|
b.start();
|
2020-08-26 14:36:20 -04:00
|
|
|
for (let i = 0; i < 1e3; i++);
|
2020-03-18 19:25:55 -04:00
|
|
|
b.stop();
|
|
|
|
});
|
|
|
|
|
2020-08-26 14:36:20 -04:00
|
|
|
bench(function forDecrementX1e3(b): void {
|
2020-03-18 19:25:55 -04:00
|
|
|
b.start();
|
2020-08-26 14:36:20 -04:00
|
|
|
for (let i = 1e3; i > 0; i--);
|
2020-03-18 19:25:55 -04:00
|
|
|
b.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
bench(async function forAwaitFetchDenolandX10(b): Promise<void> {
|
2019-02-12 10:55:01 -05:00
|
|
|
b.start();
|
2020-03-18 19:25:55 -04:00
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
const r = await fetch("https://deno.land/");
|
|
|
|
await r.text();
|
|
|
|
}
|
2019-02-12 10:55:01 -05:00
|
|
|
b.stop();
|
2020-03-18 19:25:55 -04:00
|
|
|
});
|
2019-02-12 10:55:01 -05:00
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
bench(async function promiseAllFetchDenolandX10(b): Promise<void> {
|
|
|
|
const urls = new Array(10).fill("https://deno.land/");
|
|
|
|
b.start();
|
|
|
|
await Promise.all(
|
|
|
|
urls.map(
|
|
|
|
async (denoland: string): Promise<void> => {
|
|
|
|
const r = await fetch(denoland);
|
|
|
|
await r.text();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
|
|
|
),
|
2020-03-18 19:25:55 -04:00
|
|
|
);
|
|
|
|
b.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
bench({
|
|
|
|
name: "runs100ForIncrementX1e6",
|
|
|
|
runs: 100,
|
|
|
|
func(b): void {
|
|
|
|
b.start();
|
|
|
|
for (let i = 0; i < 1e6; i++);
|
|
|
|
b.stop();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-18 19:25:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
bench(function throwing(b): void {
|
|
|
|
b.start();
|
|
|
|
// Throws bc the timer's stop method is never called
|
|
|
|
});
|
2019-02-12 10:55:01 -05:00
|
|
|
|
2020-05-29 02:29:58 -04:00
|
|
|
const benchResult = await runBenchmarks({ skip: /throw/ });
|
|
|
|
|
|
|
|
assertEquals(benchResult.filtered, 1);
|
|
|
|
assertEquals(benchResult.results.length, 5);
|
|
|
|
|
2020-06-03 22:33:53 -04:00
|
|
|
const resultWithSingleRunsFiltered = benchResult.results.filter(
|
2020-08-26 14:36:20 -04:00
|
|
|
({ name }) => name === "forDecrementX1e3",
|
2020-06-03 22:33:53 -04:00
|
|
|
);
|
|
|
|
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);
|
|
|
|
|
2020-05-29 02:29:58 -04:00
|
|
|
const resultWithMultipleRunsFiltered = benchResult.results.filter(
|
2020-07-14 15:24:17 -04:00
|
|
|
({ name }) => name === "runs100ForIncrementX1e6",
|
2020-05-29 02:29:58 -04:00
|
|
|
);
|
|
|
|
assertEquals(resultWithMultipleRunsFiltered.length, 1);
|
|
|
|
|
|
|
|
const resultWithMultipleRuns = resultWithMultipleRunsFiltered[0];
|
|
|
|
assert(!!resultWithMultipleRuns.runsCount);
|
2020-06-02 17:06:13 -04:00
|
|
|
assert(!!resultWithMultipleRuns.measuredRunsAvgMs);
|
|
|
|
assert(!!resultWithMultipleRuns.measuredRunsMs);
|
2020-05-29 02:29:58 -04:00
|
|
|
assertEquals(resultWithMultipleRuns.runsCount, 100);
|
2020-06-03 22:33:53 -04:00
|
|
|
assertEquals(resultWithMultipleRuns.measuredRunsMs.length, 100);
|
2020-06-02 17:06:13 -04:00
|
|
|
|
|
|
|
clearBenchmarks();
|
2020-05-29 02:29:58 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "Bench without name should throw",
|
2020-05-29 02:29:58 -04:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
bench(() => {});
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"The benchmark function must not be anonymous",
|
2020-05-29 02:29:58 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "Bench without stop should throw",
|
2020-05-29 02:29:58 -04:00
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
bench(function benchWithoutStop(b): void {
|
|
|
|
b.start();
|
|
|
|
// Throws bc the timer's stop method is never called
|
|
|
|
});
|
|
|
|
await runBenchmarks({ only: /benchWithoutStop/, silent: true });
|
|
|
|
},
|
|
|
|
BenchmarkRunError,
|
2020-07-14 15:24:17 -04:00
|
|
|
"The benchmark timer's stop method must be called",
|
2020-05-29 02:29:58 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "Bench without start should throw",
|
2020-05-29 02:29:58 -04:00
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
bench(function benchWithoutStart(b): void {
|
|
|
|
b.stop();
|
|
|
|
// Throws bc the timer's start method is never called
|
|
|
|
});
|
|
|
|
await runBenchmarks({ only: /benchWithoutStart/, silent: true });
|
|
|
|
},
|
|
|
|
BenchmarkRunError,
|
2020-07-14 15:24:17 -04:00
|
|
|
"The benchmark timer's start method must be called",
|
2020-05-29 02:29:58 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "Bench with stop before start should throw",
|
2020-05-29 02:29:58 -04:00
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
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,
|
2020-07-14 15:24:17 -04:00
|
|
|
"The benchmark timer's start method must be called before its stop method",
|
2020-05-29 02:29:58 -04:00
|
|
|
);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-02-12 10:55:01 -05:00
|
|
|
});
|
2020-06-02 17:06:13 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "clearBenchmarks should clear all candidates",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
dummyBench("test");
|
|
|
|
|
|
|
|
clearBenchmarks();
|
|
|
|
const benchingResults = await runBenchmarks({ silent: true });
|
|
|
|
|
|
|
|
assertEquals(benchingResults.filtered, 0);
|
|
|
|
assertEquals(benchingResults.results.length, 0);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "clearBenchmarks with only as option",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
// 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");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "clearBenchmarks with skip as option",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
// 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");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "clearBenchmarks with only and skip as option",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
// 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"));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-02 17:06:13 -04:00
|
|
|
name: "progressCallback of runBenchmarks",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
clearBenchmarks();
|
|
|
|
dummyBench("skip");
|
|
|
|
dummyBench("single");
|
|
|
|
dummyBench("multiple", 2);
|
|
|
|
|
|
|
|
const progressCallbacks: BenchmarkRunProgress[] = [];
|
|
|
|
|
|
|
|
const benchingResults = await runBenchmarks(
|
|
|
|
{ skip: /skip/, silent: true },
|
|
|
|
(progress) => {
|
|
|
|
progressCallbacks.push(progress);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-02 17:06:13 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
let pc = 0;
|
|
|
|
// Assert initial progress before running
|
|
|
|
let progress = progressCallbacks[pc++];
|
2020-08-24 19:43:54 -04:00
|
|
|
assert(progress.queued);
|
2020-06-02 17:06:13 -04:00
|
|
|
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);
|
2020-08-24 19:43:54 -04:00
|
|
|
assert(progress.queued);
|
2020-06-02 17:06:13 -04:00
|
|
|
assertEquals(progress.queued.length, 1);
|
|
|
|
assert(!!progress.queued.find(({ name }) => name == "multiple"));
|
|
|
|
assertEquals(progress.running, {
|
|
|
|
name: "single",
|
|
|
|
runsCount: 1,
|
|
|
|
measuredRunsMs: [],
|
|
|
|
});
|
|
|
|
assertEquals(progress.results, []);
|
|
|
|
|
2020-06-03 22:33:53 -04:00
|
|
|
// Assert running result of bench "single"
|
|
|
|
progress = progressCallbacks[pc++];
|
|
|
|
assertEquals(progress.state, ProgressState.BenchPartialResult);
|
2020-08-24 19:43:54 -04:00
|
|
|
assert(progress.queued);
|
2020-06-03 22:33:53 -04:00
|
|
|
assertEquals(progress.queued.length, 1);
|
|
|
|
assertEquals(progress.running!.measuredRunsMs.length, 1);
|
|
|
|
assertEquals(progress.results.length, 0);
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
// Assert result of bench "single"
|
|
|
|
progress = progressCallbacks[pc++];
|
|
|
|
assertEquals(progress.state, ProgressState.BenchResult);
|
2020-08-24 19:43:54 -04:00
|
|
|
assert(progress.queued);
|
2020-06-02 17:06:13 -04:00
|
|
|
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);
|
2020-08-24 19:43:54 -04:00
|
|
|
assert(progress.queued);
|
2020-06-02 17:06:13 -04:00
|
|
|
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);
|
2020-08-24 19:43:54 -04:00
|
|
|
assert(progress.queued);
|
2020-06-02 17:06:13 -04:00
|
|
|
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);
|
2020-08-24 19:43:54 -04:00
|
|
|
assert(progress.queued);
|
2020-06-02 17:06:13 -04:00
|
|
|
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);
|
2020-08-24 19:43:54 -04:00
|
|
|
assert(progress.queued);
|
2020-06-02 17:06:13 -04:00
|
|
|
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(
|
2020-07-14 15:24:17 -04:00
|
|
|
({ name }) => name == "multiple",
|
2020-06-02 17:06:13 -04:00
|
|
|
);
|
|
|
|
assertEquals(resultOfMultiple.length, 1);
|
|
|
|
assert(!!resultOfMultiple[0].measuredRunsMs);
|
2020-06-03 22:33:53 -04:00
|
|
|
assert(!isNaN(resultOfMultiple[0].measuredRunsAvgMs));
|
|
|
|
assertEquals(resultOfMultiple[0].measuredRunsMs.length, 2);
|
2020-06-02 17:06:13 -04:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-09 11:19:06 -04:00
|
|
|
name: "async progressCallback",
|
|
|
|
fn: async function (): Promise<void> {
|
|
|
|
clearBenchmarks();
|
|
|
|
dummyBench("single");
|
|
|
|
|
|
|
|
const asyncCallbacks = [];
|
|
|
|
|
|
|
|
await runBenchmarks({ silent: true }, (progress) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
queueMicrotask(() => {
|
|
|
|
asyncCallbacks.push(progress);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(asyncCallbacks.length, 5);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-02 17:06:13 -04:00
|
|
|
function dummyBench(name: string, runs = 1): void {
|
|
|
|
bench({
|
|
|
|
name,
|
|
|
|
runs,
|
|
|
|
func(b) {
|
|
|
|
b.start();
|
|
|
|
b.stop();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|