版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

301 строка
7.6 KiB

  1. var H = Object.defineProperty;
  2. var P = (n, a, t) => a in n ? H(n, a, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[a] = t;
  3. var i = (n, a, t) => (P(n, typeof a != "symbol" ? a + "" : a, t), t);
  4. // src/event.ts
  5. function r(n, a = null) {
  6. let t = new Event(n);
  7. return Object.defineProperty(t, "task", {
  8. value: a,
  9. enumerable: !0,
  10. writable: !1,
  11. configurable: !1
  12. }), t;
  13. }
  14. // src/constants.ts
  15. var S = {
  16. 1: 12.71,
  17. 2: 4.303,
  18. 3: 3.182,
  19. 4: 2.776,
  20. 5: 2.571,
  21. 6: 2.447,
  22. 7: 2.365,
  23. 8: 2.306,
  24. 9: 2.262,
  25. 10: 2.228,
  26. 11: 2.201,
  27. 12: 2.179,
  28. 13: 2.16,
  29. 14: 2.145,
  30. 15: 2.131,
  31. 16: 2.12,
  32. 17: 2.11,
  33. 18: 2.101,
  34. 19: 2.093,
  35. 20: 2.086,
  36. 21: 2.08,
  37. 22: 2.074,
  38. 23: 2.069,
  39. 24: 2.064,
  40. 25: 2.06,
  41. 26: 2.056,
  42. 27: 2.052,
  43. 28: 2.048,
  44. 29: 2.045,
  45. 30: 2.042,
  46. 31: 2.0399,
  47. 32: 2.0378,
  48. 33: 2.0357,
  49. 34: 2.0336,
  50. 35: 2.0315,
  51. 36: 2.0294,
  52. 37: 2.0273,
  53. 38: 2.0252,
  54. 39: 2.0231,
  55. 40: 2.021,
  56. 41: 2.0198,
  57. 42: 2.0186,
  58. 43: 2.0174,
  59. 44: 2.0162,
  60. 45: 2.015,
  61. 46: 2.0138,
  62. 47: 2.0126,
  63. 48: 2.0114,
  64. 49: 2.0102,
  65. 50: 2.009,
  66. 51: 2.0081,
  67. 52: 2.0072,
  68. 53: 2.0063,
  69. 54: 2.0054,
  70. 55: 2.0045,
  71. 56: 2.0036,
  72. 57: 2.0027,
  73. 58: 2.0018,
  74. 59: 2.0009,
  75. 60: 2,
  76. 61: 1.9995,
  77. 62: 1.999,
  78. 63: 1.9985,
  79. 64: 1.998,
  80. 65: 1.9975,
  81. 66: 1.997,
  82. 67: 1.9965,
  83. 68: 1.996,
  84. 69: 1.9955,
  85. 70: 1.995,
  86. 71: 1.9945,
  87. 72: 1.994,
  88. 73: 1.9935,
  89. 74: 1.993,
  90. 75: 1.9925,
  91. 76: 1.992,
  92. 77: 1.9915,
  93. 78: 1.991,
  94. 79: 1.9905,
  95. 80: 1.99,
  96. 81: 1.9897,
  97. 82: 1.9894,
  98. 83: 1.9891,
  99. 84: 1.9888,
  100. 85: 1.9885,
  101. 86: 1.9882,
  102. 87: 1.9879,
  103. 88: 1.9876,
  104. 89: 1.9873,
  105. 90: 1.987,
  106. 91: 1.9867,
  107. 92: 1.9864,
  108. 93: 1.9861,
  109. 94: 1.9858,
  110. 95: 1.9855,
  111. 96: 1.9852,
  112. 97: 1.9849,
  113. 98: 1.9846,
  114. 99: 1.9843,
  115. 100: 1.984,
  116. 101: 1.9838,
  117. 102: 1.9836,
  118. 103: 1.9834,
  119. 104: 1.9832,
  120. 105: 1.983,
  121. 106: 1.9828,
  122. 107: 1.9826,
  123. 108: 1.9824,
  124. 109: 1.9822,
  125. 110: 1.982,
  126. 111: 1.9818,
  127. 112: 1.9816,
  128. 113: 1.9814,
  129. 114: 1.9812,
  130. 115: 1.9819,
  131. 116: 1.9808,
  132. 117: 1.9806,
  133. 118: 1.9804,
  134. 119: 1.9802,
  135. 120: 1.98,
  136. infinity: 1.96
  137. }, b = S;
  138. // src/utils.ts
  139. var j = (n) => n / 1e6, w = () => {
  140. var n;
  141. return typeof ((n = globalThis.process) == null ? void 0 : n.hrtime) == "function" ? j(Number(process.hrtime.bigint())) : performance.now();
  142. }, B = (n) => n.reduce((a, t) => a + t, 0) / n.length || 0, F = (n, a) => n.reduce((e, s) => e + (s - a) ** 2) / (n.length - 1) || 0, q = (async () => {
  143. }).constructor, O = (n) => n.constructor === q;
  144. // src/task.ts
  145. var p = class extends EventTarget {
  146. constructor(t, e, s, o = {}) {
  147. super();
  148. i(this, "bench");
  149. i(this, "name");
  150. i(this, "fn");
  151. i(this, "runs", 0);
  152. i(this, "result");
  153. i(this, "opts");
  154. this.bench = t, this.name = e, this.fn = s, this.opts = o;
  155. }
  156. async run() {
  157. var o, u, l;
  158. this.dispatchEvent(r("start", this));
  159. let t = 0, e = [], s = O(this.fn);
  160. for (await this.bench.setup(this, "run"), this.opts.beforeAll != null && await this.opts.beforeAll.call(this); (t < this.bench.time || this.runs < this.bench.iterations) && !((o = this.bench.signal) != null && o.aborted); ) {
  161. this.opts.beforeEach != null && await this.opts.beforeEach.call(this);
  162. let h = 0;
  163. try {
  164. h = this.bench.now(), s ? await this.fn() : this.fn();
  165. } catch (f) {
  166. this.setResult({ error: f });
  167. }
  168. let c = this.bench.now() - h;
  169. this.runs += 1, e.push(c), t += c, this.opts.afterEach != null && await this.opts.afterEach.call(this);
  170. }
  171. this.opts.afterAll != null && await this.opts.afterAll.call(this), await this.bench.teardown(this, "run"), e.sort((h, c) => h - c);
  172. {
  173. let h = e[0], c = e[e.length - 1], f = t / this.runs, L = 1e3 / f, E = B(e), T = F(e, E), k = Math.sqrt(T), g = k / Math.sqrt(e.length), y = e.length - 1, x = b[String(Math.round(y) || 1)] || b.infinity, M = g * x, K = M / E * 100 || 0, A = e[Math.ceil(e.length * (75 / 100)) - 1], R = e[Math.ceil(e.length * (99 / 100)) - 1], _ = e[Math.ceil(e.length * (99.5 / 100)) - 1], I = e[Math.ceil(e.length * (99.9 / 100)) - 1];
  174. if ((u = this.bench.signal) != null && u.aborted)
  175. return this;
  176. this.setResult({
  177. totalTime: t,
  178. min: h,
  179. max: c,
  180. hz: L,
  181. period: f,
  182. samples: e,
  183. mean: E,
  184. variance: T,
  185. sd: k,
  186. sem: g,
  187. df: y,
  188. critical: x,
  189. moe: M,
  190. rme: K,
  191. p75: A,
  192. p99: R,
  193. p995: _,
  194. p999: I
  195. });
  196. }
  197. return (l = this.result) != null && l.error && (this.dispatchEvent(r("error", this)), this.bench.dispatchEvent(r("error", this))), this.dispatchEvent(r("cycle", this)), this.bench.dispatchEvent(r("cycle", this)), this.dispatchEvent(r("complete", this)), this;
  198. }
  199. async warmup() {
  200. var s;
  201. this.dispatchEvent(r("warmup", this));
  202. let t = this.bench.now(), e = 0;
  203. for (this.bench.setup(this, "warmup"); (e < this.bench.warmupTime || this.runs < this.bench.warmupIterations) && !((s = this.bench.signal) != null && s.aborted); ) {
  204. try {
  205. await Promise.resolve().then(this.fn);
  206. } catch (o) {
  207. }
  208. this.runs += 1, e = this.bench.now() - t;
  209. }
  210. this.bench.teardown(this, "warmup"), this.runs = 0;
  211. }
  212. addEventListener(t, e, s) {
  213. super.addEventListener(t, e, s);
  214. }
  215. removeEventListener(t, e, s) {
  216. super.removeEventListener(t, e, s);
  217. }
  218. setResult(t) {
  219. this.result = { ...this.result, ...t }, Object.freeze(this.reset);
  220. }
  221. reset() {
  222. this.dispatchEvent(r("reset", this)), this.runs = 0, this.result = void 0;
  223. }
  224. };
  225. // src/bench.ts
  226. var v = class extends EventTarget {
  227. constructor(t = {}) {
  228. var e, s, o, u, l, h, c;
  229. super();
  230. i(this, "_tasks", /* @__PURE__ */ new Map());
  231. i(this, "signal");
  232. i(this, "warmupTime", 100);
  233. i(this, "warmupIterations", 5);
  234. i(this, "time", 500);
  235. i(this, "iterations", 10);
  236. i(this, "now", w);
  237. i(this, "setup");
  238. i(this, "teardown");
  239. this.now = (e = t.now) != null ? e : this.now, this.warmupTime = (s = t.warmupTime) != null ? s : this.warmupTime, this.warmupIterations = (o = t.warmupIterations) != null ? o : this.warmupIterations, this.time = (u = t.time) != null ? u : this.time, this.iterations = (l = t.iterations) != null ? l : this.iterations, this.signal = t.signal, this.setup = (h = t.setup) != null ? h : () => {
  240. }, this.teardown = (c = t.teardown) != null ? c : () => {
  241. }, this.signal && this.signal.addEventListener("abort", () => {
  242. this.dispatchEvent(r("abort"));
  243. }, { once: !0 });
  244. }
  245. async run() {
  246. var e;
  247. this.dispatchEvent(r("start"));
  248. let t = [];
  249. for (let s of [...this._tasks.values()])
  250. (e = this.signal) != null && e.aborted ? t.push(s) : t.push(await s.run());
  251. return this.dispatchEvent(r("complete")), t;
  252. }
  253. async warmup() {
  254. this.dispatchEvent(r("warmup"));
  255. for (let [, t] of this._tasks)
  256. await t.warmup();
  257. }
  258. reset() {
  259. this.dispatchEvent(r("reset")), this._tasks.forEach((t) => {
  260. t.reset();
  261. });
  262. }
  263. add(t, e, s = {}) {
  264. let o = new p(this, t, e, s);
  265. return this._tasks.set(t, o), this.dispatchEvent(r("add", o)), this;
  266. }
  267. remove(t) {
  268. let e = this.getTask(t);
  269. return this.dispatchEvent(r("remove", e)), this._tasks.delete(t), this;
  270. }
  271. addEventListener(t, e, s) {
  272. super.addEventListener(t, e, s);
  273. }
  274. removeEventListener(t, e, s) {
  275. super.removeEventListener(t, e, s);
  276. }
  277. get results() {
  278. return [...this._tasks.values()].map((t) => t.result);
  279. }
  280. get tasks() {
  281. return [...this._tasks.values()];
  282. }
  283. getTask(t) {
  284. return this._tasks.get(t);
  285. }
  286. };
  287. // src/index.ts
  288. var tt = v;
  289. export {
  290. v as Bench,
  291. p as Task,
  292. tt as default,
  293. w as now
  294. };