stats.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. function _defineProperty(obj, key, value) {
  2. if (key in obj) {
  3. Object.defineProperty(obj, key, {
  4. value,
  5. enumerable: true,
  6. configurable: true,
  7. writable: true
  8. });
  9. } else {
  10. obj[key] = value;
  11. }
  12. return obj;
  13. }
  14. function defaultSetTimout() {
  15. throw new Error("setTimeout has not been defined");
  16. }
  17. function defaultClearTimeout() {
  18. throw new Error("clearTimeout has not been defined");
  19. }
  20. var cachedSetTimeout = defaultSetTimout;
  21. var cachedClearTimeout = defaultClearTimeout;
  22. var globalContext;
  23. if (typeof window !== "undefined") {
  24. globalContext = window;
  25. } else if (typeof self !== "undefined") {
  26. globalContext = self;
  27. } else {
  28. globalContext = {};
  29. }
  30. if (typeof globalContext.setTimeout === "function") {
  31. cachedSetTimeout = setTimeout;
  32. }
  33. if (typeof globalContext.clearTimeout === "function") {
  34. cachedClearTimeout = clearTimeout;
  35. }
  36. function runTimeout(fun) {
  37. if (cachedSetTimeout === setTimeout) {
  38. return setTimeout(fun, 0);
  39. }
  40. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  41. cachedSetTimeout = setTimeout;
  42. return setTimeout(fun, 0);
  43. }
  44. try {
  45. return cachedSetTimeout(fun, 0);
  46. } catch (e) {
  47. try {
  48. return cachedSetTimeout.call(null, fun, 0);
  49. } catch (e2) {
  50. return cachedSetTimeout.call(this, fun, 0);
  51. }
  52. }
  53. }
  54. function runClearTimeout(marker) {
  55. if (cachedClearTimeout === clearTimeout) {
  56. return clearTimeout(marker);
  57. }
  58. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  59. cachedClearTimeout = clearTimeout;
  60. return clearTimeout(marker);
  61. }
  62. try {
  63. return cachedClearTimeout(marker);
  64. } catch (e) {
  65. try {
  66. return cachedClearTimeout.call(null, marker);
  67. } catch (e2) {
  68. return cachedClearTimeout.call(this, marker);
  69. }
  70. }
  71. }
  72. var queue = [];
  73. var draining = false;
  74. var currentQueue;
  75. var queueIndex = -1;
  76. function cleanUpNextTick() {
  77. if (!draining || !currentQueue) {
  78. return;
  79. }
  80. draining = false;
  81. if (currentQueue.length) {
  82. queue = currentQueue.concat(queue);
  83. } else {
  84. queueIndex = -1;
  85. }
  86. if (queue.length) {
  87. drainQueue();
  88. }
  89. }
  90. function drainQueue() {
  91. if (draining) {
  92. return;
  93. }
  94. var timeout = runTimeout(cleanUpNextTick);
  95. draining = true;
  96. var len = queue.length;
  97. while (len) {
  98. currentQueue = queue;
  99. queue = [];
  100. while (++queueIndex < len) {
  101. if (currentQueue) {
  102. currentQueue[queueIndex].run();
  103. }
  104. }
  105. queueIndex = -1;
  106. len = queue.length;
  107. }
  108. currentQueue = null;
  109. draining = false;
  110. runClearTimeout(timeout);
  111. }
  112. function nextTick(fun) {
  113. var args = new Array(arguments.length - 1);
  114. if (arguments.length > 1) {
  115. for (var i = 1; i < arguments.length; i++) {
  116. args[i - 1] = arguments[i];
  117. }
  118. }
  119. queue.push(new Item(fun, args));
  120. if (queue.length === 1 && !draining) {
  121. runTimeout(drainQueue);
  122. }
  123. }
  124. function Item(fun, array) {
  125. this.fun = fun;
  126. this.array = array;
  127. }
  128. Item.prototype.run = function() {
  129. this.fun.apply(null, this.array);
  130. };
  131. var title = "browser";
  132. var platform = "browser";
  133. var browser = true;
  134. var argv = [];
  135. var version = "";
  136. var versions = {};
  137. var release = {};
  138. var config = {};
  139. function noop() {
  140. }
  141. var on = noop;
  142. var addListener = noop;
  143. var once = noop;
  144. var off = noop;
  145. var removeListener = noop;
  146. var removeAllListeners = noop;
  147. var emit = noop;
  148. function binding(name) {
  149. throw new Error("process.binding is not supported");
  150. }
  151. function cwd() {
  152. return "/";
  153. }
  154. function chdir(dir) {
  155. throw new Error("process.chdir is not supported");
  156. }
  157. function umask() {
  158. return 0;
  159. }
  160. var performance = globalContext.performance || {};
  161. var performanceNow = performance.now || performance.mozNow || performance.msNow || performance.oNow || performance.webkitNow || function() {
  162. return new Date().getTime();
  163. };
  164. function hrtime(previousTimestamp) {
  165. var clocktime = performanceNow.call(performance) * 1e-3;
  166. var seconds = Math.floor(clocktime);
  167. var nanoseconds = Math.floor(clocktime % 1 * 1e9);
  168. if (previousTimestamp) {
  169. seconds = seconds - previousTimestamp[0];
  170. nanoseconds = nanoseconds - previousTimestamp[1];
  171. if (nanoseconds < 0) {
  172. seconds--;
  173. nanoseconds += 1e9;
  174. }
  175. }
  176. return [seconds, nanoseconds];
  177. }
  178. var startTime = new Date();
  179. function uptime() {
  180. var currentTime = new Date();
  181. var dif = currentTime - startTime;
  182. return dif / 1e3;
  183. }
  184. var process = {
  185. nextTick,
  186. title,
  187. browser,
  188. env: {NODE_ENV: "production"},
  189. argv,
  190. version,
  191. versions,
  192. on,
  193. addListener,
  194. once,
  195. off,
  196. removeListener,
  197. removeAllListeners,
  198. emit,
  199. binding,
  200. cwd,
  201. chdir,
  202. umask,
  203. hrtime,
  204. platform,
  205. release,
  206. config,
  207. uptime
  208. };
  209. function getHiResTimestamp() {
  210. let timestamp;
  211. if (typeof window !== "undefined" && window.performance) {
  212. timestamp = window.performance.now();
  213. } else if (typeof process !== "undefined" && process.hrtime) {
  214. const timeParts = process.hrtime();
  215. timestamp = timeParts[0] * 1e3 + timeParts[1] / 1e6;
  216. } else {
  217. timestamp = Date.now();
  218. }
  219. return timestamp;
  220. }
  221. class Stat {
  222. constructor(name, type) {
  223. _defineProperty(this, "name", void 0);
  224. _defineProperty(this, "type", void 0);
  225. _defineProperty(this, "sampleSize", 1);
  226. _defineProperty(this, "time", void 0);
  227. _defineProperty(this, "count", void 0);
  228. _defineProperty(this, "samples", void 0);
  229. _defineProperty(this, "lastTiming", void 0);
  230. _defineProperty(this, "lastSampleTime", void 0);
  231. _defineProperty(this, "lastSampleCount", void 0);
  232. _defineProperty(this, "_count", 0);
  233. _defineProperty(this, "_time", 0);
  234. _defineProperty(this, "_samples", 0);
  235. _defineProperty(this, "_startTime", 0);
  236. _defineProperty(this, "_timerPending", false);
  237. this.name = name;
  238. this.type = type;
  239. this.reset();
  240. }
  241. setSampleSize(samples) {
  242. this.sampleSize = samples;
  243. return this;
  244. }
  245. incrementCount() {
  246. this.addCount(1);
  247. return this;
  248. }
  249. decrementCount() {
  250. this.subtractCount(1);
  251. return this;
  252. }
  253. addCount(value) {
  254. this._count += value;
  255. this._samples++;
  256. this._checkSampling();
  257. return this;
  258. }
  259. subtractCount(value) {
  260. this._count -= value;
  261. this._samples++;
  262. this._checkSampling();
  263. return this;
  264. }
  265. addTime(time) {
  266. this._time += time;
  267. this.lastTiming = time;
  268. this._samples++;
  269. this._checkSampling();
  270. return this;
  271. }
  272. timeStart() {
  273. this._startTime = getHiResTimestamp();
  274. this._timerPending = true;
  275. return this;
  276. }
  277. timeEnd() {
  278. if (!this._timerPending) {
  279. return this;
  280. }
  281. this.addTime(getHiResTimestamp() - this._startTime);
  282. this._timerPending = false;
  283. this._checkSampling();
  284. return this;
  285. }
  286. getSampleAverageCount() {
  287. return this.sampleSize > 0 ? this.lastSampleCount / this.sampleSize : 0;
  288. }
  289. getSampleAverageTime() {
  290. return this.sampleSize > 0 ? this.lastSampleTime / this.sampleSize : 0;
  291. }
  292. getSampleHz() {
  293. return this.lastSampleTime > 0 ? this.sampleSize / (this.lastSampleTime / 1e3) : 0;
  294. }
  295. getAverageCount() {
  296. return this.samples > 0 ? this.count / this.samples : 0;
  297. }
  298. getAverageTime() {
  299. return this.samples > 0 ? this.time / this.samples : 0;
  300. }
  301. getHz() {
  302. return this.time > 0 ? this.samples / (this.time / 1e3) : 0;
  303. }
  304. reset() {
  305. this.time = 0;
  306. this.count = 0;
  307. this.samples = 0;
  308. this.lastTiming = 0;
  309. this.lastSampleTime = 0;
  310. this.lastSampleCount = 0;
  311. this._count = 0;
  312. this._time = 0;
  313. this._samples = 0;
  314. this._startTime = 0;
  315. this._timerPending = false;
  316. return this;
  317. }
  318. _checkSampling() {
  319. if (this._samples === this.sampleSize) {
  320. this.lastSampleTime = this._time;
  321. this.lastSampleCount = this._count;
  322. this.count += this._count;
  323. this.time += this._time;
  324. this.samples += this._samples;
  325. this._time = 0;
  326. this._count = 0;
  327. this._samples = 0;
  328. }
  329. }
  330. }
  331. class Stats {
  332. constructor(options) {
  333. _defineProperty(this, "id", void 0);
  334. _defineProperty(this, "stats", {});
  335. this.id = options.id;
  336. this.stats = {};
  337. this._initializeStats(options.stats);
  338. Object.seal(this);
  339. }
  340. get(name, type = "count") {
  341. return this._getOrCreate({
  342. name,
  343. type
  344. });
  345. }
  346. get size() {
  347. return Object.keys(this.stats).length;
  348. }
  349. reset() {
  350. for (const key in this.stats) {
  351. this.stats[key].reset();
  352. }
  353. return this;
  354. }
  355. forEach(fn) {
  356. for (const key in this.stats) {
  357. fn(this.stats[key]);
  358. }
  359. }
  360. getTable() {
  361. const table = {};
  362. this.forEach((stat) => {
  363. table[stat.name] = {
  364. time: stat.time || 0,
  365. count: stat.count || 0,
  366. average: stat.getAverageTime() || 0,
  367. hz: stat.getHz() || 0
  368. };
  369. });
  370. return table;
  371. }
  372. _initializeStats(stats = []) {
  373. stats.forEach((stat) => this._getOrCreate(stat));
  374. }
  375. _getOrCreate(stat) {
  376. if (!stat || !stat.name) {
  377. return null;
  378. }
  379. const {
  380. name,
  381. type
  382. } = stat;
  383. if (!this.stats[name]) {
  384. if (stat instanceof Stat) {
  385. this.stats[name] = stat;
  386. } else {
  387. this.stats[name] = new Stat(name, type);
  388. }
  389. }
  390. return this.stats[name];
  391. }
  392. }
  393. export {Stat, Stats, getHiResTimestamp as _getHiResTimestamp};
  394. export default null;