/
api.ts
142 lines (133 loc) · 3.17 KB
/
api.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
import type { Fn, Fn0, Fn2 } from "@thi.ng/api";
export type TimingResult<T> = [T, number];
export interface BenchmarkOpts {
/**
* Benchmark title (only used if `print` enabled)
*/
title: string;
/**
* Number of iterations
*
* @defaultValue 1000
*/
iter: number;
/**
* Number of calls per iteration, i.e. total number of iterations will be
* `iter * size`.
*
* @defaultValue 1
*/
size: number;
/**
* Number of warmup iterations (not included in results).
*
* @defaultValue 10
*/
warmup: number;
/**
* Result formatter
*
* @defaultValue FORMAT_DEFAULT
*/
format: BenchmarkFormatter;
/**
* If false, all output will be supressed.
*
* @defaultValue true
*/
output: boolean;
}
export type OptsWithoutTitle = Omit<BenchmarkOpts, "title">;
export interface BenchmarkSuiteOpts extends OptsWithoutTitle {}
export interface BenchmarkResult {
title: string;
/**
* Number of iterations
*/
iter: number;
/**
* Number of calls per iteration
*/
size: number;
/**
* Total execution time for all runs (in ms)
*/
total: number;
/**
* Mean execution time (in ms)
*/
mean: number;
/**
* Median execution time (in ms)
*/
median: number;
/**
* Min execution time (in ms)
*/
min: number;
/**
* Max execution time (in ms)
*/
max: number;
/**
* First quartile execution time (in ms). I.e. 25% of all runs were
* faster/equal to this measurement.
*/
q1: number;
/**
* Third quartile execution time (in ms). I.e. 25% of all runs were
* equal/slower than this measurement.
*/
q3: number;
/**
* Standard deviation (in percent)
*/
sd: number;
}
export interface BenchmarkFormatter {
/**
* Called once before the benchmark suite runs any benchmarks.
*/
prefix: Fn0<string>;
/**
* Called once for each given benchmark in the suite. Receives benchmark
* options.
*/
start: Fn<BenchmarkOpts, string>;
/**
* Called once per benchmark, just after warmup. Receives warmup time taken
* (in milliseconds) and benchmark opts.
*/
warmup: Fn2<number, BenchmarkOpts, string>;
/**
* Called once per benchmark with collected result.
*/
result: Fn<BenchmarkResult, string>;
/**
* Called once after all benchmarks have run. Receives array of all results.
*/
total: Fn<BenchmarkResult[], string>;
/**
* Called at the very end of the benchmark suite. Useful if a format
* requires any form of final suffix.
*/
suffix: Fn0<string>;
}
export interface Benchmark {
/**
* Benchmark title
*/
title: string;
/**
* Benchmark function. Will be called `size` times per `iter`ation (see
* {@link BenchmarkOpts}).
*/
fn: Fn0<void>;
/**
* Optional & partial benchmark specific option overrides (merged with opts
* given to suite)
*/
opts?: Partial<OptsWithoutTitle>;
}
export const FLOAT = (x: number) => x.toFixed(2);
export const EMPTY = () => "";