File size: 9,041 Bytes
bc20498 |
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
/**
* A class that represents each benchmark task in Tinybench. It keeps track of the
* results, name, Bench instance, the task function and the number times the task
* function has been executed.
*/
declare class Task extends EventTarget {
bench: Bench;
/**
* task name
*/
name: string;
fn: Fn;
runs: number;
/**
* the result object
*/
result?: TaskResult;
/**
* Task options
*/
opts: FnOptions;
constructor(bench: Bench, name: string, fn: Fn, opts?: FnOptions);
private loop;
/**
* run the current task and write the results in `Task.result` object
*/
run(): Promise<this>;
/**
* warmup the current task
*/
warmup(): Promise<void>;
addEventListener<K extends TaskEvents, T = TaskEventsMap[K]>(type: K, listener: T, options?: AddEventListenerOptionsArgument): void;
removeEventListener<K extends TaskEvents, T = TaskEventsMap[K]>(type: K, listener: T, options?: RemoveEventListenerOptionsArgument): void;
/**
* change the result object values
*/
setResult(result: Partial<TaskResult>): void;
/**
* reset the task to make the `Task.runs` a zero-value and remove the `Task.result`
* object
*/
reset(): void;
}
/**
* the task function
*/
type Fn = () => any | Promise<any>;
interface FnOptions {
/**
* An optional function that is run before iterations of this task begin
*/
beforeAll?: (this: Task) => void | Promise<void>;
/**
* An optional function that is run before each iteration of this task
*/
beforeEach?: (this: Task) => void | Promise<void>;
/**
* An optional function that is run after each iteration of this task
*/
afterEach?: (this: Task) => void | Promise<void>;
/**
* An optional function that is run after all iterations of this task end
*/
afterAll?: (this: Task) => void | Promise<void>;
}
/**
* the benchmark task result object
*/
type TaskResult = {
error?: unknown;
/**
* The amount of time in milliseconds to run the benchmark task (cycle).
*/
totalTime: number;
/**
* the minimum value in the samples
*/
min: number;
/**
* the maximum value in the samples
*/
max: number;
/**
* the number of operations per second
*/
hz: number;
/**
* how long each operation takes (ms)
*/
period: number;
/**
* task samples of each task iteration time (ms)
*/
samples: number[];
/**
* samples mean/average (estimate of the population mean)
*/
mean: number;
/**
* samples variance (estimate of the population variance)
*/
variance: number;
/**
* samples standard deviation (estimate of the population standard deviation)
*/
sd: number;
/**
* standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean)
*/
sem: number;
/**
* degrees of freedom
*/
df: number;
/**
* critical value of the samples
*/
critical: number;
/**
* margin of error
*/
moe: number;
/**
* relative margin of error
*/
rme: number;
/**
* p75 percentile
*/
p75: number;
/**
* p99 percentile
*/
p99: number;
/**
* p995 percentile
*/
p995: number;
/**
* p999 percentile
*/
p999: number;
};
/**
* Both the `Task` and `Bench` objects extend the `EventTarget` object,
* so you can attach a listeners to different types of events
* to each class instance using the universal `addEventListener` and
* `removeEventListener`
*/
/**
* Bench events
*/
type BenchEvents = 'abort' | 'complete' | 'error' | 'reset' | 'start' | 'warmup' | 'cycle' | 'add' | 'remove' | 'todo';
type Hook = (task: Task, mode: 'warmup' | 'run') => void | Promise<void>;
type NoopEventListener = () => any | Promise<any>;
type TaskEventListener = (e: Event & {
task: Task;
}) => any | Promise<any>;
interface BenchEventsMap {
abort: NoopEventListener;
start: NoopEventListener;
complete: NoopEventListener;
warmup: NoopEventListener;
reset: NoopEventListener;
add: TaskEventListener;
remove: TaskEventListener;
cycle: TaskEventListener;
error: TaskEventListener;
todo: TaskEventListener;
}
/**
* task events
*/
type TaskEvents = 'abort' | 'complete' | 'error' | 'reset' | 'start' | 'warmup' | 'cycle';
type TaskEventsMap = {
abort: NoopEventListener;
start: TaskEventListener;
error: TaskEventListener;
cycle: TaskEventListener;
complete: TaskEventListener;
warmup: TaskEventListener;
reset: TaskEventListener;
};
type Options = {
/**
* time needed for running a benchmark task (milliseconds) @default 500
*/
time?: number;
/**
* number of times that a task should run if even the time option is finished @default 10
*/
iterations?: number;
/**
* function to get the current timestamp in milliseconds
*/
now?: () => number;
/**
* An AbortSignal for aborting the benchmark
*/
signal?: AbortSignal;
/**
* Throw if a task fails (events will not work if true)
*/
throws?: boolean;
/**
* warmup time (milliseconds) @default 100ms
*/
warmupTime?: number;
/**
* warmup iterations @default 5
*/
warmupIterations?: number;
/**
* setup function to run before each benchmark task (cycle)
*/
setup?: Hook;
/**
* teardown function to run after each benchmark task (cycle)
*/
teardown?: Hook;
};
type BenchEvent = Event & {
task: Task | null;
};
type RemoveEventListenerOptionsArgument = Parameters<typeof EventTarget.prototype.removeEventListener>[2];
type AddEventListenerOptionsArgument = Parameters<typeof EventTarget.prototype.addEventListener>[2];
/**
* The Benchmark instance for keeping track of the benchmark tasks and controlling
* them.
*/
declare class Bench extends EventTarget {
_tasks: Map<string, Task>;
_todos: Map<string, Task>;
/**
* Executes tasks concurrently based on the specified concurrency mode.
*
* - When `mode` is set to `null` (default), concurrency is disabled.
* - When `mode` is set to 'task', each task's iterations (calls of a task function) run concurrently.
* - When `mode` is set to 'bench', different tasks within the bench run concurrently.
*/
concurrency: 'task' | 'bench' | null;
/**
* The maximum number of concurrent tasks to run. Defaults to Infinity.
*/
threshold: number;
signal?: AbortSignal;
throws: boolean;
warmupTime: number;
warmupIterations: number;
time: number;
iterations: number;
now: () => number;
setup: Hook;
teardown: Hook;
constructor(options?: Options);
private runTask;
/**
* run the added tasks that were registered using the
* {@link add} method.
* Note: This method does not do any warmup. Call {@link warmup} for that.
*/
run(): Promise<Task[]>;
/**
* See Bench.{@link concurrency}
*/
runConcurrently(threshold?: number, mode?: NonNullable<Bench['concurrency']>): Promise<Task[]>;
/**
* warmup the benchmark tasks.
* This is not run by default by the {@link run} method.
*/
warmup(): Promise<void>;
/**
* warmup the benchmark tasks concurrently.
* This is not run by default by the {@link runConcurrently} method.
*/
warmupConcurrently(threshold?: number, mode?: NonNullable<Bench['concurrency']>): Promise<void>;
/**
* reset each task and remove its result
*/
reset(): void;
/**
* add a benchmark task to the task map
*/
add(name: string, fn: Fn, opts?: FnOptions): this;
/**
* add a benchmark todo to the todo map
*/
todo(name: string, fn?: Fn, opts?: FnOptions): this;
/**
* remove a benchmark task from the task map
*/
remove(name: string): this;
addEventListener<K extends BenchEvents, T = BenchEventsMap[K]>(type: K, listener: T, options?: AddEventListenerOptionsArgument): void;
removeEventListener<K extends BenchEvents, T = BenchEventsMap[K]>(type: K, listener: T, options?: RemoveEventListenerOptionsArgument): void;
/**
* table of the tasks results
*/
table(convert?: (task: Task) => Record<string, string | number> | undefined): (Record<string, string | number> | null)[];
/**
* (getter) tasks results as an array
*/
get results(): (TaskResult | undefined)[];
/**
* (getter) tasks as an array
*/
get tasks(): Task[];
get todos(): Task[];
/**
* get a task based on the task name
*/
getTask(name: string): Task | undefined;
}
declare const hrtimeNow: () => number;
declare const now: () => number;
export { Bench, BenchEvent, BenchEvents, Fn, Hook, Options, Task, TaskEvents, TaskResult, Bench as default, hrtimeNow, now };
|