Reviewed By: TheSavior

Differential Revision: D7207468

fbshipit-source-id: 05ae6e003ae3e55bc2d3a905f41c28f7041dd7f0
This commit is contained in:
Tim Yung 2018-03-08 23:33:06 -08:00 коммит произвёл Facebook Github Bot
Родитель 3c86701ea7
Коммит 118521c20b
1 изменённых файлов: 141 добавлений и 127 удалений

Просмотреть файл

@ -6,7 +6,9 @@
*
* @providesModule Systrace
* @flow
* @format
*/
'use strict';
const invariant = require('fbjs/lib/invariant');
@ -14,18 +16,17 @@ const invariant = require('fbjs/lib/invariant');
type RelayProfiler = {
attachProfileHandler(
name: string,
handler: (name: string, state?: any) => () => void
handler: (name: string, state?: any) => () => void,
): void,
attachAggregateHandler(
name: string,
handler: (name: string, callback: () => void) => void
handler: (name: string, callback: () => void) => void,
): void,
};
/* eslint no-bitwise: 0 */
const TRACE_TAG_REACT_APPS = 1 << 17;
const TRACE_TAG_JS_VM_CALLS = 1 << 27;
const TRACE_TAG_REACT_APPS = 1 << 17; // eslint-disable-line no-bitwise
const TRACE_TAG_JS_VM_CALLS = 1 << 27; // eslint-disable-line no-bitwise
let _enabled = false;
let _asyncCookie = 0;
@ -36,7 +37,8 @@ let _canInstallReactHook = false;
// Implements a subset of User Timing API necessary for React measurements.
// https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API
const REACT_MARKER = '\u269B';
const userTimingPolyfill = __DEV__ ? {
const userTimingPolyfill = __DEV__
? {
mark(markName: string) {
if (_enabled) {
_markStackIndex++;
@ -61,7 +63,7 @@ const userTimingPolyfill = __DEV__ ? {
typeof measureName === 'string' &&
typeof startMark === 'string' &&
typeof endMark === 'undefined',
'Only performance.measure(string, string) overload is supported.'
'Only performance.measure(string, string) overload is supported.',
);
const topMark = _markStack[_markStackIndex];
invariant(
@ -95,7 +97,8 @@ const userTimingPolyfill = __DEV__ ? {
// React calls this to avoid memory leaks in browsers, but we don't keep
// measurements anyway.
},
} : null;
}
: null;
const Systrace = {
installReactHook() {
@ -111,9 +114,11 @@ const Systrace = {
if (_enabled !== enabled) {
if (__DEV__) {
if (enabled) {
global.nativeTraceBeginLegacy && global.nativeTraceBeginLegacy(TRACE_TAG_JS_VM_CALLS);
global.nativeTraceBeginLegacy &&
global.nativeTraceBeginLegacy(TRACE_TAG_JS_VM_CALLS);
} else {
global.nativeTraceEndLegacy && global.nativeTraceEndLegacy(TRACE_TAG_JS_VM_CALLS);
global.nativeTraceEndLegacy &&
global.nativeTraceEndLegacy(TRACE_TAG_JS_VM_CALLS);
}
if (_canInstallReactHook) {
if (enabled && global.performance === undefined) {
@ -134,8 +139,8 @@ const Systrace = {
**/
beginEvent(profileName?: any, args?: any) {
if (_enabled) {
profileName = typeof profileName === 'function' ?
profileName() : profileName;
profileName =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceBeginSection(TRACE_TAG_REACT_APPS, profileName, args);
}
},
@ -155,18 +160,26 @@ const Systrace = {
const cookie = _asyncCookie;
if (_enabled) {
_asyncCookie++;
profileName = typeof profileName === 'function' ?
profileName() : profileName;
global.nativeTraceBeginAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie);
profileName =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceBeginAsyncSection(
TRACE_TAG_REACT_APPS,
profileName,
cookie,
);
}
return cookie;
},
endAsyncEvent(profileName?: any, cookie?: any) {
if (_enabled) {
profileName = typeof profileName === 'function' ?
profileName() : profileName;
global.nativeTraceEndAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie);
profileName =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceEndAsyncSection(
TRACE_TAG_REACT_APPS,
profileName,
cookie,
);
}
},
@ -175,8 +188,8 @@ const Systrace = {
**/
counterEvent(profileName?: any, value?: any) {
if (_enabled) {
profileName = typeof profileName === 'function' ?
profileName() : profileName;
profileName =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceCounter &&
global.nativeTraceCounter(TRACE_TAG_REACT_APPS, profileName, value);
}
@ -207,10 +220,7 @@ const Systrace = {
/* This is not called by default due to perf overhead but it's useful
if you want to find traces which spend too much time in JSON. */
swizzleJSON() {
Systrace.measureMethods(JSON, 'JSON', [
'parse',
'stringify'
]);
Systrace.measureMethods(JSON, 'JSON', ['parse', 'stringify']);
},
/**
@ -221,7 +231,11 @@ const Systrace = {
* @param objectName
* @param methodNames Map from method names to method display names.
*/
measureMethods(object: any, objectName: string, methodNames: Array<string>): void {
measureMethods(
object: any,
objectName: string,
methodNames: Array<string>,
): void {
if (!__DEV__) {
return;
}
@ -230,7 +244,7 @@ const Systrace = {
object[methodName] = Systrace.measure(
objectName,
methodName,
object[methodName]
object[methodName],
);
});
},