createPerformanceLogger.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow
  8. * @format
  9. */
  10. 'use strict';
  11. const Systrace = require('../Performance/Systrace');
  12. const infoLog = require('./infoLog');
  13. const performanceNow =
  14. global.nativeQPLTimestamp ||
  15. global.nativePerformanceNow ||
  16. require('fbjs/lib/performanceNow');
  17. type Timespan = {
  18. description?: string,
  19. totalTime?: number,
  20. startTime?: number,
  21. endTime?: number,
  22. ...
  23. };
  24. export type IPerformanceLogger = {
  25. addTimespan(string, number, string | void): void,
  26. startTimespan(string, string | void): void,
  27. stopTimespan(string, options?: {update?: boolean}): void,
  28. clear(): void,
  29. clearCompleted(): void,
  30. clearExceptTimespans(Array<string>): void,
  31. currentTimestamp(): number,
  32. getTimespans(): {[key: string]: Timespan, ...},
  33. hasTimespan(string): boolean,
  34. logTimespans(): void,
  35. addTimespans(Array<number>, Array<string>): void,
  36. setExtra(string, any): void,
  37. getExtras(): {[key: string]: any, ...},
  38. removeExtra(string): ?any,
  39. logExtras(): void,
  40. markPoint(string, number | void): void,
  41. getPoints(): {[key: string]: number, ...},
  42. logPoints(): void,
  43. logEverything(): void,
  44. ...
  45. };
  46. const _cookies: {[key: string]: number, ...} = {};
  47. const PRINT_TO_CONSOLE: false = false; // Type as false to prevent accidentally committing `true`;
  48. /**
  49. * This function creates performance loggers that can be used to collect and log
  50. * various performance data such as timespans, points and extras.
  51. * The loggers need to have minimal overhead since they're used in production.
  52. */
  53. function createPerformanceLogger(): IPerformanceLogger {
  54. const result: IPerformanceLogger & {
  55. _timespans: {[key: string]: Timespan, ...},
  56. _extras: {[key: string]: any, ...},
  57. _points: {[key: string]: number, ...},
  58. ...
  59. } = {
  60. _timespans: {},
  61. _extras: {},
  62. _points: {},
  63. addTimespan(key: string, lengthInMs: number, description?: string) {
  64. if (this._timespans[key]) {
  65. if (PRINT_TO_CONSOLE && __DEV__) {
  66. infoLog(
  67. 'PerformanceLogger: Attempting to add a timespan that already exists ',
  68. key,
  69. );
  70. }
  71. return;
  72. }
  73. this._timespans[key] = {
  74. description: description,
  75. totalTime: lengthInMs,
  76. };
  77. },
  78. startTimespan(key: string, description?: string) {
  79. if (this._timespans[key]) {
  80. if (PRINT_TO_CONSOLE && __DEV__) {
  81. infoLog(
  82. 'PerformanceLogger: Attempting to start a timespan that already exists ',
  83. key,
  84. );
  85. }
  86. return;
  87. }
  88. this._timespans[key] = {
  89. description: description,
  90. startTime: performanceNow(),
  91. };
  92. _cookies[key] = Systrace.beginAsyncEvent(key);
  93. if (PRINT_TO_CONSOLE) {
  94. infoLog('PerformanceLogger.js', 'start: ' + key);
  95. }
  96. },
  97. stopTimespan(key: string, options?: {update?: boolean}) {
  98. const timespan = this._timespans[key];
  99. if (!timespan || !timespan.startTime) {
  100. if (PRINT_TO_CONSOLE && __DEV__) {
  101. infoLog(
  102. 'PerformanceLogger: Attempting to end a timespan that has not started ',
  103. key,
  104. );
  105. }
  106. return;
  107. }
  108. if (timespan.endTime && !options?.update) {
  109. if (PRINT_TO_CONSOLE && __DEV__) {
  110. infoLog(
  111. 'PerformanceLogger: Attempting to end a timespan that has already ended ',
  112. key,
  113. );
  114. }
  115. return;
  116. }
  117. timespan.endTime = performanceNow();
  118. timespan.totalTime = timespan.endTime - (timespan.startTime || 0);
  119. if (PRINT_TO_CONSOLE) {
  120. infoLog('PerformanceLogger.js', 'end: ' + key);
  121. }
  122. if (_cookies[key] != null) {
  123. Systrace.endAsyncEvent(key, _cookies[key]);
  124. delete _cookies[key];
  125. }
  126. },
  127. clear() {
  128. this._timespans = {};
  129. this._extras = {};
  130. this._points = {};
  131. if (PRINT_TO_CONSOLE) {
  132. infoLog('PerformanceLogger.js', 'clear');
  133. }
  134. },
  135. clearCompleted() {
  136. for (const key in this._timespans) {
  137. if (this._timespans[key].totalTime) {
  138. delete this._timespans[key];
  139. }
  140. }
  141. this._extras = {};
  142. this._points = {};
  143. if (PRINT_TO_CONSOLE) {
  144. infoLog('PerformanceLogger.js', 'clearCompleted');
  145. }
  146. },
  147. clearExceptTimespans(keys: Array<string>) {
  148. this._timespans = Object.keys(this._timespans).reduce(function(
  149. previous,
  150. key,
  151. ) {
  152. if (keys.indexOf(key) !== -1) {
  153. previous[key] = this._timespans[key];
  154. }
  155. return previous;
  156. },
  157. {});
  158. this._extras = {};
  159. this._points = {};
  160. if (PRINT_TO_CONSOLE) {
  161. infoLog('PerformanceLogger.js', 'clearExceptTimespans', keys);
  162. }
  163. },
  164. currentTimestamp() {
  165. return performanceNow();
  166. },
  167. getTimespans() {
  168. return this._timespans;
  169. },
  170. hasTimespan(key: string) {
  171. return !!this._timespans[key];
  172. },
  173. logTimespans() {
  174. if (PRINT_TO_CONSOLE) {
  175. for (const key in this._timespans) {
  176. if (this._timespans[key].totalTime) {
  177. infoLog(key + ': ' + this._timespans[key].totalTime + 'ms');
  178. }
  179. }
  180. }
  181. },
  182. addTimespans(newTimespans: Array<number>, labels: Array<string>) {
  183. for (let ii = 0, l = newTimespans.length; ii < l; ii += 2) {
  184. const label = labels[ii / 2];
  185. this.addTimespan(label, newTimespans[ii + 1] - newTimespans[ii], label);
  186. }
  187. },
  188. setExtra(key: string, value: any) {
  189. if (this._extras[key]) {
  190. if (PRINT_TO_CONSOLE && __DEV__) {
  191. infoLog(
  192. 'PerformanceLogger: Attempting to set an extra that already exists ',
  193. {key, currentValue: this._extras[key], attemptedValue: value},
  194. );
  195. }
  196. return;
  197. }
  198. this._extras[key] = value;
  199. },
  200. getExtras() {
  201. return this._extras;
  202. },
  203. removeExtra(key: string): ?any {
  204. const value = this._extras[key];
  205. delete this._extras[key];
  206. return value;
  207. },
  208. logExtras() {
  209. if (PRINT_TO_CONSOLE) {
  210. infoLog(this._extras);
  211. }
  212. },
  213. markPoint(key: string, timestamp?: number) {
  214. if (this._points[key]) {
  215. if (PRINT_TO_CONSOLE && __DEV__) {
  216. infoLog(
  217. 'PerformanceLogger: Attempting to mark a point that has been already logged ',
  218. key,
  219. );
  220. }
  221. return;
  222. }
  223. this._points[key] = timestamp ?? performanceNow();
  224. },
  225. getPoints() {
  226. return this._points;
  227. },
  228. logPoints() {
  229. if (PRINT_TO_CONSOLE) {
  230. for (const key in this._points) {
  231. infoLog(key + ': ' + this._points[key] + 'ms');
  232. }
  233. }
  234. },
  235. logEverything() {
  236. this.logTimespans();
  237. this.logExtras();
  238. this.logPoints();
  239. },
  240. };
  241. return result;
  242. }
  243. module.exports = createPerformanceLogger;