index.d.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * A class that represents each benchmark task in Tinybench. It keeps track of the
  3. * results, name, Bench instance, the task function and the number times the task
  4. * function has been executed.
  5. */
  6. declare class Task extends EventTarget {
  7. bench: Bench;
  8. /**
  9. * task name
  10. */
  11. name: string;
  12. fn: Fn;
  13. runs: number;
  14. /**
  15. * the result object
  16. */
  17. result?: TaskResult;
  18. /**
  19. * Task options
  20. */
  21. opts: FnOptions;
  22. constructor(bench: Bench, name: string, fn: Fn, opts?: FnOptions);
  23. /**
  24. * run the current task and write the results in `Task.result` object
  25. */
  26. run(): Promise<this>;
  27. /**
  28. * warmup the current task
  29. */
  30. warmup(): Promise<void>;
  31. addEventListener<K extends TaskEvents, T = TaskEventsMap[K]>(type: K, listener: T, options?: AddEventListenerOptionsArgument): void;
  32. removeEventListener<K extends TaskEvents, T = TaskEventsMap[K]>(type: K, listener: T, options?: RemoveEventListenerOptionsArgument): void;
  33. /**
  34. * change the result object values
  35. */
  36. setResult(result: Partial<TaskResult>): void;
  37. /**
  38. * reset the task to make the `Task.runs` a zero-value and remove the `Task.result`
  39. * object
  40. */
  41. reset(): void;
  42. }
  43. /**
  44. * the task function
  45. */
  46. type Fn = () => any | Promise<any>;
  47. interface FnOptions {
  48. /**
  49. * An optional function that is run before iterations of this task begin
  50. */
  51. beforeAll?: (this: Task) => void | Promise<void>;
  52. /**
  53. * An optional function that is run before each iteration of this task
  54. */
  55. beforeEach?: (this: Task) => void | Promise<void>;
  56. /**
  57. * An optional function that is run after each iteration of this task
  58. */
  59. afterEach?: (this: Task) => void | Promise<void>;
  60. /**
  61. * An optional function that is run after all iterations of this task end
  62. */
  63. afterAll?: (this: Task) => void | Promise<void>;
  64. }
  65. /**
  66. * the benchmark task result object
  67. */
  68. type TaskResult = {
  69. error?: unknown;
  70. /**
  71. * The amount of time in milliseconds to run the benchmark task (cycle).
  72. */
  73. totalTime: number;
  74. /**
  75. * the minimum value in the samples
  76. */
  77. min: number;
  78. /**
  79. * the maximum value in the samples
  80. */
  81. max: number;
  82. /**
  83. * the number of operations per second
  84. */
  85. hz: number;
  86. /**
  87. * how long each operation takes (ms)
  88. */
  89. period: number;
  90. /**
  91. * task samples of each task iteration time (ms)
  92. */
  93. samples: number[];
  94. /**
  95. * samples mean/average (estimate of the population mean)
  96. */
  97. mean: number;
  98. /**
  99. * samples variance (estimate of the population variance)
  100. */
  101. variance: number;
  102. /**
  103. * samples standard deviation (estimate of the population standard deviation)
  104. */
  105. sd: number;
  106. /**
  107. * standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean)
  108. */
  109. sem: number;
  110. /**
  111. * degrees of freedom
  112. */
  113. df: number;
  114. /**
  115. * critical value of the samples
  116. */
  117. critical: number;
  118. /**
  119. * margin of error
  120. */
  121. moe: number;
  122. /**
  123. * relative margin of error
  124. */
  125. rme: number;
  126. /**
  127. * p75 percentile
  128. */
  129. p75: number;
  130. /**
  131. * p99 percentile
  132. */
  133. p99: number;
  134. /**
  135. * p995 percentile
  136. */
  137. p995: number;
  138. /**
  139. * p999 percentile
  140. */
  141. p999: number;
  142. };
  143. /**
  144. * Both the `Task` and `Bench` objects extend the `EventTarget` object,
  145. * so you can attach a listeners to different types of events
  146. * to each class instance using the universal `addEventListener` and
  147. * `removeEventListener`
  148. */
  149. /**
  150. * Bench events
  151. */
  152. type BenchEvents = 'abort' | 'complete' | 'error' | 'reset' | 'start' | 'warmup' | 'cycle' | 'add' | 'remove' | 'todo';
  153. type Hook = (task: Task, mode: 'warmup' | 'run') => void | Promise<void>;
  154. type NoopEventListener = () => any | Promise<any>;
  155. type TaskEventListener = (e: Event & {
  156. task: Task;
  157. }) => any | Promise<any>;
  158. interface BenchEventsMap {
  159. abort: NoopEventListener;
  160. start: NoopEventListener;
  161. complete: NoopEventListener;
  162. warmup: NoopEventListener;
  163. reset: NoopEventListener;
  164. add: TaskEventListener;
  165. remove: TaskEventListener;
  166. cycle: TaskEventListener;
  167. error: TaskEventListener;
  168. todo: TaskEventListener;
  169. }
  170. /**
  171. * task events
  172. */
  173. type TaskEvents = 'abort' | 'complete' | 'error' | 'reset' | 'start' | 'warmup' | 'cycle';
  174. type TaskEventsMap = {
  175. abort: NoopEventListener;
  176. start: TaskEventListener;
  177. error: TaskEventListener;
  178. cycle: TaskEventListener;
  179. complete: TaskEventListener;
  180. warmup: TaskEventListener;
  181. reset: TaskEventListener;
  182. };
  183. type Options = {
  184. /**
  185. * time needed for running a benchmark task (milliseconds) @default 500
  186. */
  187. time?: number;
  188. /**
  189. * number of times that a task should run if even the time option is finished @default 10
  190. */
  191. iterations?: number;
  192. /**
  193. * function to get the current timestamp in milliseconds
  194. */
  195. now?: () => number;
  196. /**
  197. * An AbortSignal for aborting the benchmark
  198. */
  199. signal?: AbortSignal;
  200. /**
  201. * Throw if a task fails (events will not work if true)
  202. */
  203. throws?: boolean;
  204. /**
  205. * warmup time (milliseconds) @default 100ms
  206. */
  207. warmupTime?: number;
  208. /**
  209. * warmup iterations @default 5
  210. */
  211. warmupIterations?: number;
  212. /**
  213. * setup function to run before each benchmark task (cycle)
  214. */
  215. setup?: Hook;
  216. /**
  217. * teardown function to run after each benchmark task (cycle)
  218. */
  219. teardown?: Hook;
  220. };
  221. type BenchEvent = Event & {
  222. task: Task | null;
  223. };
  224. type RemoveEventListenerOptionsArgument = Parameters<typeof EventTarget.prototype.removeEventListener>[2];
  225. type AddEventListenerOptionsArgument = Parameters<typeof EventTarget.prototype.addEventListener>[2];
  226. /**
  227. * The Benchmark instance for keeping track of the benchmark tasks and controlling
  228. * them.
  229. */
  230. declare class Bench extends EventTarget {
  231. _tasks: Map<string, Task>;
  232. _todos: Map<string, Task>;
  233. signal?: AbortSignal;
  234. throws: boolean;
  235. warmupTime: number;
  236. warmupIterations: number;
  237. time: number;
  238. iterations: number;
  239. now: () => number;
  240. setup: Hook;
  241. teardown: Hook;
  242. constructor(options?: Options);
  243. /**
  244. * run the added tasks that were registered using the
  245. * {@link add} method.
  246. * Note: This method does not do any warmup. Call {@link warmup} for that.
  247. */
  248. run(): Promise<Task[]>;
  249. /**
  250. * warmup the benchmark tasks.
  251. * This is not run by default by the {@link run} method.
  252. */
  253. warmup(): Promise<void>;
  254. /**
  255. * reset each task and remove its result
  256. */
  257. reset(): void;
  258. /**
  259. * add a benchmark task to the task map
  260. */
  261. add(name: string, fn: Fn, opts?: FnOptions): this;
  262. /**
  263. * add a benchmark todo to the todo map
  264. */
  265. todo(name: string, fn?: Fn, opts?: FnOptions): this;
  266. /**
  267. * remove a benchmark task from the task map
  268. */
  269. remove(name: string): this;
  270. addEventListener<K extends BenchEvents, T = BenchEventsMap[K]>(type: K, listener: T, options?: AddEventListenerOptionsArgument): void;
  271. removeEventListener<K extends BenchEvents, T = BenchEventsMap[K]>(type: K, listener: T, options?: RemoveEventListenerOptionsArgument): void;
  272. /**
  273. * table of the tasks results
  274. */
  275. table(): ({
  276. 'Task Name': string;
  277. 'ops/sec': string;
  278. 'Average Time (ns)': string | number;
  279. Margin: string;
  280. Samples: string | number;
  281. } | null)[];
  282. /**
  283. * (getter) tasks results as an array
  284. */
  285. get results(): (TaskResult | undefined)[];
  286. /**
  287. * (getter) tasks as an array
  288. */
  289. get tasks(): Task[];
  290. get todos(): Task[];
  291. /**
  292. * get a task based on the task name
  293. */
  294. getTask(name: string): Task | undefined;
  295. }
  296. declare const hrtimeNow: () => number;
  297. declare const now: () => number;
  298. export { Bench, BenchEvent, BenchEvents, Fn, Hook, Options, Task, TaskEvents, TaskResult, Bench as default, hrtimeNow, now };