123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- declare class Task extends EventTarget {
- bench: Bench;
-
- name: string;
- fn: Fn;
- runs: number;
-
- result?: TaskResult;
-
- opts: FnOptions;
- constructor(bench: Bench, name: string, fn: Fn, opts?: FnOptions);
- /**
- * 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 {
-
- beforeAll?: (this: Task) => void | Promise<void>;
-
- beforeEach?: (this: Task) => void | Promise<void>;
-
- afterEach?: (this: Task) => void | Promise<void>;
-
- afterAll?: (this: Task) => void | Promise<void>;
- }
- type TaskResult = {
- error?: unknown;
-
- totalTime: number;
-
- min: number;
-
- max: number;
-
- hz: number;
-
- period: number;
-
- samples: number[];
-
- mean: number;
-
- variance: number;
-
- sd: number;
-
- sem: number;
-
- df: number;
-
- critical: number;
-
- moe: number;
-
- rme: number;
-
- p75: number;
-
- p99: number;
-
- p995: number;
-
- p999: number;
- };
- 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;
- }
- 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?: number;
-
- iterations?: number;
-
- now?: () => number;
-
- signal?: AbortSignal;
-
- throws?: boolean;
-
- warmupTime?: number;
-
- warmupIterations?: number;
-
- setup?: Hook;
-
- teardown?: Hook;
- };
- type BenchEvent = Event & {
- task: Task | null;
- };
- type RemoveEventListenerOptionsArgument = Parameters<typeof EventTarget.prototype.removeEventListener>[2];
- type AddEventListenerOptionsArgument = Parameters<typeof EventTarget.prototype.addEventListener>[2];
- declare class Bench extends EventTarget {
- _tasks: Map<string, Task>;
- _todos: Map<string, Task>;
- signal?: AbortSignal;
- throws: boolean;
- warmupTime: number;
- warmupIterations: number;
- time: number;
- iterations: number;
- now: () => number;
- setup: Hook;
- teardown: Hook;
- constructor(options?: Options);
- /**
- * 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[]>;
- /**
- * warmup the benchmark tasks.
- * This is not run by default by the {@link run} method.
- */
- warmup(): 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(): ({
- 'Task Name': string;
- 'ops/sec': string;
- 'Average Time (ns)': string | number;
- Margin: string;
- Samples: 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 };
|