-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
bench.ts
146 lines (125 loc) 路 3.3 KB
/
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
import { createBenchEvent } from './event';
import Task from './task';
import { now } from './utils';
/**
* The Benchmark instance for keeping track of the benchmark tasks and controlling
* them.
*/
export default class Bench extends EventTarget {
#tasks: Map<string, Task> = new Map();
signal?: AbortSignal;
warmupTime = 100;
warmupIterations = 5;
time = 500;
iterations = 10;
now = now;
setup: IHook;
teardown: IHook;
constructor(options: IOptions = {}) {
super();
this.now = options.now ?? this.now;
this.warmupTime = options.warmupTime ?? this.warmupTime;
this.warmupIterations = options.warmupIterations ?? this.warmupIterations;
this.time = options.time ?? this.time;
this.iterations = options.iterations ?? this.iterations;
this.signal = options.signal;
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.setup = options.setup ?? (() => {});
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.teardown = options.teardown ?? (() => {});
if (this.signal) {
this.signal.addEventListener(
'abort',
() => {
this.dispatchEvent(createBenchEvent('abort'));
},
{ once: true },
);
}
}
/**
* run the added tasks that were registered using the
* `add` method
*/
async run() {
this.dispatchEvent(createBenchEvent('start'));
const values = await Promise.all(
[...this.#tasks.entries()].map(([_, task]) => {
if (this.signal?.aborted) {
return task;
}
return task.run();
}),
);
this.dispatchEvent(createBenchEvent('complete'));
return values;
}
/**
* warmup the benchmark tasks
*/
async warmup() {
this.dispatchEvent(createBenchEvent('warmup'));
for (const [, task] of this.#tasks) {
await task.warmup();
}
}
/**
* reset each task and remove its result
*/
reset() {
this.dispatchEvent(createBenchEvent('reset'));
this.#tasks.forEach((task) => {
task.reset();
});
}
/**
* add a benchmark task to the task map
*/
add(name: string, fn: Fn) {
const task = new Task(this, name, fn);
this.#tasks.set(name, task);
this.dispatchEvent(createBenchEvent('add', task));
return this;
}
/**
* remove a benchmark task from the task map
*/
remove(name: string) {
const task = this.getTask(name);
this.dispatchEvent(createBenchEvent('remove', task));
this.#tasks.delete(name);
return this;
}
addEventListener(
type: IBenchEvents,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
) {
super.addEventListener(type, listener, options);
}
removeEventListener(
type: IBenchEvents,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions,
) {
super.removeEventListener(type, listener, options);
}
/**
* (getter) tasks results as an array
*/
get results(): (ITaskResult | undefined)[] {
return [...this.#tasks.values()].map((task) => task.result);
}
/**
* (getter) tasks as an array
*/
get tasks(): Task[] {
return [...this.#tasks.values()];
}
/**
* get a task based on the task name
*/
getTask(name: string): Task | undefined {
return this.#tasks.get(name);
}
}