|
| 1 | +import Task from '../src/task'; |
| 2 | + |
| 3 | +/** |
| 4 | + * the task function |
| 5 | + */ |
| 6 | +export type Fn = () => any | Promise<any>; |
| 7 | + |
| 8 | +export interface FnOptions { |
| 9 | + /** |
| 10 | + * An optional function that is run before iterations of this task begin |
| 11 | + */ |
| 12 | + beforeAll?: (this: Task) => void | Promise<void>; |
| 13 | + |
| 14 | + /** |
| 15 | + * An optional function that is run before each iteration of this task |
| 16 | + */ |
| 17 | + beforeEach?: (this: Task) => void | Promise<void>; |
| 18 | + |
| 19 | + /** |
| 20 | + * An optional function that is run after each iteration of this task |
| 21 | + */ |
| 22 | + afterEach?: (this: Task) => void | Promise<void>; |
| 23 | + |
| 24 | + /** |
| 25 | + * An optional function that is run after all iterations of this task end |
| 26 | + */ |
| 27 | + afterAll?: (this: Task) => void | Promise<void>; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * the benchmark task result object |
| 32 | + */ |
| 33 | +export type TaskResult = { |
| 34 | + /* |
| 35 | + * the last error that was thrown while running the task |
| 36 | + */ |
| 37 | + error?: unknown; |
| 38 | + |
| 39 | + /** |
| 40 | + * The amount of time in milliseconds to run the benchmark task (cycle). |
| 41 | + */ |
| 42 | + totalTime: number; |
| 43 | + |
| 44 | + /** |
| 45 | + * the minimum value in the samples |
| 46 | + */ |
| 47 | + min: number; |
| 48 | + /** |
| 49 | + * the maximum value in the samples |
| 50 | + */ |
| 51 | + max: number; |
| 52 | + |
| 53 | + /** |
| 54 | + * the number of operations per second |
| 55 | + */ |
| 56 | + hz: number; |
| 57 | + |
| 58 | + /** |
| 59 | + * how long each operation takes (ms) |
| 60 | + */ |
| 61 | + period: number; |
| 62 | + |
| 63 | + /** |
| 64 | + * task samples of each task iteration time (ms) |
| 65 | + */ |
| 66 | + samples: number[]; |
| 67 | + |
| 68 | + /** |
| 69 | + * samples mean/average (estimate of the population mean) |
| 70 | + */ |
| 71 | + mean: number; |
| 72 | + |
| 73 | + /** |
| 74 | + * samples variance (estimate of the population variance) |
| 75 | + */ |
| 76 | + variance: number; |
| 77 | + |
| 78 | + /** |
| 79 | + * samples standard deviation (estimate of the population standard deviation) |
| 80 | + */ |
| 81 | + sd: number; |
| 82 | + |
| 83 | + /** |
| 84 | + * standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean) |
| 85 | + */ |
| 86 | + sem: number; |
| 87 | + |
| 88 | + /** |
| 89 | + * degrees of freedom |
| 90 | + */ |
| 91 | + df: number; |
| 92 | + |
| 93 | + /** |
| 94 | + * critical value of the samples |
| 95 | + */ |
| 96 | + critical: number; |
| 97 | + |
| 98 | + /** |
| 99 | + * margin of error |
| 100 | + */ |
| 101 | + moe: number; |
| 102 | + |
| 103 | + /** |
| 104 | + * relative margin of error |
| 105 | + */ |
| 106 | + rme: number; |
| 107 | + |
| 108 | + /** |
| 109 | + * p75 percentile |
| 110 | + */ |
| 111 | + p75: number; |
| 112 | + |
| 113 | + /** |
| 114 | + * p99 percentile |
| 115 | + */ |
| 116 | + p99: number; |
| 117 | + |
| 118 | + /** |
| 119 | + * p995 percentile |
| 120 | + */ |
| 121 | + p995: number; |
| 122 | + |
| 123 | + /** |
| 124 | + * p999 percentile |
| 125 | + */ |
| 126 | + p999: number; |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * Both the `Task` and `Bench` objects extend the `EventTarget` object, |
| 131 | + * so you can attach a listeners to different types of events |
| 132 | + * to each class instance using the universal `addEventListener` and |
| 133 | + * `removeEventListener` |
| 134 | + */ |
| 135 | + |
| 136 | +/** |
| 137 | + * Bench events |
| 138 | + */ |
| 139 | +export type BenchEvents = |
| 140 | + | 'abort' // when a signal aborts |
| 141 | + | 'complete' // when running a benchmark finishes |
| 142 | + | 'error' // when the benchmark task throws |
| 143 | + | 'reset' // when the reset function gets called |
| 144 | + | 'start' // when running the benchmarks gets started |
| 145 | + | 'warmup' // when the benchmarks start getting warmed up (before start) |
| 146 | + | 'cycle' // when running each benchmark task gets done (cycle) |
| 147 | + | 'add' // when a Task gets added to the Bench |
| 148 | + | 'remove' // when a Task gets removed of the Bench |
| 149 | + | 'todo'; // when a todo Task gets added to the Bench |
| 150 | + |
| 151 | +export type Hook = (task: Task, mode: 'warmup' | 'run') => void | Promise<void>; |
| 152 | + |
| 153 | +type NoopEventListener = () => any | Promise<any> |
| 154 | +type TaskEventListener = (e: Event & { task: Task }) => any | Promise<any> |
| 155 | + |
| 156 | +export interface BenchEventsMap{ |
| 157 | + abort: NoopEventListener |
| 158 | + start: NoopEventListener |
| 159 | + complete: NoopEventListener |
| 160 | + warmup: NoopEventListener |
| 161 | + reset: NoopEventListener |
| 162 | + add: TaskEventListener |
| 163 | + remove: TaskEventListener |
| 164 | + cycle: TaskEventListener |
| 165 | + error: TaskEventListener |
| 166 | + todo: TaskEventListener |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * task events |
| 171 | + */ |
| 172 | +export type TaskEvents = |
| 173 | + | 'abort' |
| 174 | + | 'complete' |
| 175 | + | 'error' |
| 176 | + | 'reset' |
| 177 | + | 'start' |
| 178 | + | 'warmup' |
| 179 | + | 'cycle'; |
| 180 | + |
| 181 | +export type TaskEventsMap = { |
| 182 | + abort: NoopEventListener |
| 183 | + start: TaskEventListener |
| 184 | + error: TaskEventListener |
| 185 | + cycle: TaskEventListener |
| 186 | + complete: TaskEventListener |
| 187 | + warmup: TaskEventListener |
| 188 | + reset: TaskEventListener |
| 189 | +} |
| 190 | +export type Options = { |
| 191 | + /** |
| 192 | + * time needed for running a benchmark task (milliseconds) @default 500 |
| 193 | + */ |
| 194 | + time?: number; |
| 195 | + |
| 196 | + /** |
| 197 | + * number of times that a task should run if even the time option is finished @default 10 |
| 198 | + */ |
| 199 | + iterations?: number; |
| 200 | + |
| 201 | + /** |
| 202 | + * function to get the current timestamp in milliseconds |
| 203 | + */ |
| 204 | + now?: () => number; |
| 205 | + |
| 206 | + /** |
| 207 | + * An AbortSignal for aborting the benchmark |
| 208 | + */ |
| 209 | + signal?: AbortSignal; |
| 210 | + |
| 211 | + /** |
| 212 | + * Throw if a task fails (events will not work if true) |
| 213 | + */ |
| 214 | + throws?: boolean; |
| 215 | + |
| 216 | + /** |
| 217 | + * warmup time (milliseconds) @default 100ms |
| 218 | + */ |
| 219 | + warmupTime?: number; |
| 220 | + |
| 221 | + /** |
| 222 | + * warmup iterations @default 5 |
| 223 | + */ |
| 224 | + warmupIterations?: number; |
| 225 | + |
| 226 | + /** |
| 227 | + * setup function to run before each benchmark task (cycle) |
| 228 | + */ |
| 229 | + setup?: Hook; |
| 230 | + |
| 231 | + /** |
| 232 | + * teardown function to run after each benchmark task (cycle) |
| 233 | + */ |
| 234 | + teardown?: Hook; |
| 235 | +}; |
| 236 | + |
| 237 | +export type BenchEvent = Event & { |
| 238 | + task: Task | null; |
| 239 | +}; |
| 240 | + |
1 | 241 | // @types/node doesn't have these types globally, and we don't want to bring "dom" lib for everyone
|
2 | 242 |
|
3 | 243 | export type RemoveEventListenerOptionsArgument = Parameters<typeof EventTarget.prototype.removeEventListener>[2];
|
|
0 commit comments