版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

332 lines
7.4 KiB

  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?: boolean | AddEventListenerOptions): void;
  32. removeEventListener<K extends TaskEvents, T = TaskEventsMap[K]>(type: K, listener: T, options?: boolean | EventListenerOptions): 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. /*
  70. * the last error that was thrown while running the task
  71. */
  72. error?: unknown;
  73. /**
  74. * The amount of time in milliseconds to run the benchmark task (cycle).
  75. */
  76. totalTime: number;
  77. /**
  78. * the minimum value in the samples
  79. */
  80. min: number;
  81. /**
  82. * the maximum value in the samples
  83. */
  84. max: number;
  85. /**
  86. * the number of operations per second
  87. */
  88. hz: number;
  89. /**
  90. * how long each operation takes (ms)
  91. */
  92. period: number;
  93. /**
  94. * task samples of each task iteration time (ms)
  95. */
  96. samples: number[];
  97. /**
  98. * samples mean/average (estimate of the population mean)
  99. */
  100. mean: number;
  101. /**
  102. * samples variance (estimate of the population variance)
  103. */
  104. variance: number;
  105. /**
  106. * samples standard deviation (estimate of the population standard deviation)
  107. */
  108. sd: number;
  109. /**
  110. * standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean)
  111. */
  112. sem: number;
  113. /**
  114. * degrees of freedom
  115. */
  116. df: number;
  117. /**
  118. * critical value of the samples
  119. */
  120. critical: number;
  121. /**
  122. * margin of error
  123. */
  124. moe: number;
  125. /**
  126. * relative margin of error
  127. */
  128. rme: number;
  129. /**
  130. * p75 percentile
  131. */
  132. p75: number;
  133. /**
  134. * p99 percentile
  135. */
  136. p99: number;
  137. /**
  138. * p995 percentile
  139. */
  140. p995: number;
  141. /**
  142. * p999 percentile
  143. */
  144. p999: number;
  145. };
  146. /**
  147. * Both the `Task` and `Bench` objects extend the `EventTarget` object,
  148. * so you can attach a listeners to different types of events
  149. * to each class instance using the universal `addEventListener` and
  150. * `removeEventListener`
  151. */
  152. /**
  153. * Bench events
  154. */
  155. type BenchEvents =
  156. | 'abort' // when a signal aborts
  157. | 'complete' // when running a benchmark finishes
  158. | 'error' // when the benchmark task throws
  159. | 'reset' // when the reset function gets called
  160. | 'start' // when running the benchmarks gets started
  161. | 'warmup' // when the benchmarks start getting warmed up (before start)
  162. | 'cycle' // when running each benchmark task gets done (cycle)
  163. | 'add' // when a Task gets added to the Bench
  164. | 'remove'; // when a Task gets removed of the Bench
  165. type Hook = (task: Task, mode: 'warmup' | 'run') => void | Promise<void>;
  166. type NoopEventListener = () => any | Promise<any>
  167. type TaskEventListener = (e: Event & { task: Task }) => any | Promise<any>
  168. interface BenchEventsMap{
  169. abort: NoopEventListener
  170. start: NoopEventListener
  171. complete: NoopEventListener
  172. warmup: NoopEventListener
  173. reset: NoopEventListener
  174. add: TaskEventListener
  175. remove: TaskEventListener
  176. cycle: TaskEventListener
  177. error: TaskEventListener
  178. }
  179. /**
  180. * task events
  181. */
  182. type TaskEvents =
  183. | 'abort'
  184. | 'complete'
  185. | 'error'
  186. | 'reset'
  187. | 'start'
  188. | 'warmup'
  189. | 'cycle';
  190. type TaskEventsMap = {
  191. abort: NoopEventListener
  192. start: TaskEventListener
  193. error: TaskEventListener
  194. cycle: TaskEventListener
  195. complete: TaskEventListener
  196. warmup: TaskEventListener
  197. reset: TaskEventListener
  198. }
  199. type Options = {
  200. /**
  201. * time needed for running a benchmark task (milliseconds) @default 500
  202. */
  203. time?: number;
  204. /**
  205. * number of times that a task should run if even the time option is finished @default 10
  206. */
  207. iterations?: number;
  208. /**
  209. * function to get the current timestamp in milliseconds
  210. */
  211. now?: () => number;
  212. /**
  213. * An AbortSignal for aborting the benchmark
  214. */
  215. signal?: AbortSignal;
  216. /**
  217. * warmup time (milliseconds) @default 100ms
  218. */
  219. warmupTime?: number;
  220. /**
  221. * warmup iterations @default 5
  222. */
  223. warmupIterations?: number;
  224. /**
  225. * setup function to run before each benchmark task (cycle)
  226. */
  227. setup?: Hook;
  228. /**
  229. * teardown function to run after each benchmark task (cycle)
  230. */
  231. teardown?: Hook;
  232. };
  233. type BenchEvent = Event & {
  234. task: Task | null;
  235. };
  236. /**
  237. * The Benchmark instance for keeping track of the benchmark tasks and controlling
  238. * them.
  239. */
  240. declare class Bench extends EventTarget {
  241. _tasks: Map<string, Task>;
  242. signal?: AbortSignal;
  243. warmupTime: number;
  244. warmupIterations: number;
  245. time: number;
  246. iterations: number;
  247. now: () => number;
  248. setup: Hook;
  249. teardown: Hook;
  250. constructor(options?: Options);
  251. /**
  252. * run the added tasks that were registered using the
  253. * {@link add} method.
  254. * Note: This method does not do any warmup. Call {@link warmup} for that.
  255. */
  256. run(): Promise<Task[]>;
  257. /**
  258. * warmup the benchmark tasks.
  259. * This is not run by default by the {@link run} method.
  260. */
  261. warmup(): Promise<void>;
  262. /**
  263. * reset each task and remove its result
  264. */
  265. reset(): void;
  266. /**
  267. * add a benchmark task to the task map
  268. */
  269. add(name: string, fn: Fn, opts?: FnOptions): this;
  270. /**
  271. * remove a benchmark task from the task map
  272. */
  273. remove(name: string): this;
  274. addEventListener<K extends BenchEvents, T = BenchEventsMap[K]>(type: K, listener: T, options?: boolean | AddEventListenerOptions): void;
  275. removeEventListener<K extends BenchEvents, T = BenchEventsMap[K]>(type: K, listener: T, options?: boolean | EventListenerOptions): void;
  276. /**
  277. * (getter) tasks results as an array
  278. */
  279. get results(): (TaskResult | undefined)[];
  280. /**
  281. * (getter) tasks as an array
  282. */
  283. get tasks(): Task[];
  284. /**
  285. * get a task based on the task name
  286. */
  287. getTask(name: string): Task | undefined;
  288. }
  289. declare const now: () => number;
  290. export { Bench, BenchEvent, BenchEvents, Fn, Hook, Options, Task, TaskEvents, TaskResult, Bench as default, now };