-
Notifications
You must be signed in to change notification settings - Fork 17
/
BenchmarkCollection.ts
55 lines (50 loc) · 1.57 KB
/
BenchmarkCollection.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
import Benchmark from 'benchmark';
type testFunction = () => void;
type setupFunction = () => void | Promise<void>;
type teardownFunction = () => void | Promise<void>;
export default abstract class BenchmarkCollection {
protected readonly _benchmarks: {
benchmark: Benchmark;
setup?: setupFunction;
teardown?: teardownFunction;
}[] = [];
protected readonly _comparisons: {
benchmarks: Benchmark[];
name: string;
setup?: setupFunction;
teardown?: teardownFunction;
}[] = [];
public addBenchmark(
name: string,
test: testFunction,
setup?: setupFunction,
teardown?: teardownFunction
): void {
this._benchmarks.push({
benchmark: new Benchmark(name, test),
// We do not use the setup and teardown which is offered within the API of benchmarkjs
// as several attempts to get this working did not yield any successful results.
setup,
teardown,
});
}
public compareBenchmarks(
name: string,
setup?: setupFunction,
teardown?: teardownFunction,
...benchmarks: {
name: string;
test: testFunction;
}[]
): void {
// We do not use the setup and teardown which is offered within the API of benchmarkjs
// as several attempts to get this working did not yield any successful results. We also
// allow only 1 setup and teardown as all functions which compare with one another should
// use the same data to test with.
const comparison = { name, benchmarks: [], setup, teardown };
for (const benchmark of benchmarks) {
comparison.benchmarks.push(new Benchmark(benchmark.name, benchmark.test));
}
this._comparisons.push(comparison);
}
}