Fix systrace profile handling for relay async calls

Reviewed By: astreet

Differential Revision: D2700239

fb-gh-sync-id: eaa29d63ee4f7688dd70b0cdc12564a9d479f9ef
This commit is contained in:
Mike Armstrong 2015-12-01 07:37:52 -08:00 committed by facebook-github-bot-1
parent 0a3694ce48
commit b6f5c7fa04

View File

@ -22,6 +22,7 @@ var GLOBAL = GLOBAL || this;
var TRACE_TAG_REACT_APPS = 1 << 17;
var _enabled;
var _asyncCookie = 0;
var _ReactPerf = null;
function ReactPerf() {
if (!_ReactPerf) {
@ -37,6 +38,9 @@ var BridgeProfiling = {
ReactPerf().enableMeasure = enabled;
},
/**
* profile/profileEnd for starting and then ending a profile within the same call stack frame
**/
profile(profileName?: any) {
if (_enabled) {
profileName = typeof profileName === 'function' ?
@ -51,6 +55,30 @@ var BridgeProfiling = {
}
},
/**
* profileAsync/profileAsyncEnd for starting and then ending a profile where the end can either
* occur on another thread or out of the current stack frame, eg await
* the returned cookie variable should be used as input into the asyncEnd call to end the profile
**/
profileAsync(profileName?: any): any {
var cookie = _asyncCookie;
if (_enabled) {
_asyncCookie++;
profileName = typeof profileName === 'function' ?
profileName() : profileName;
global.nativeTraceBeginAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie, 0);
}
return cookie;
},
profileAsyncEnd(profileName?: any, cookie?: any) {
if (_enabled) {
profileName = typeof profileName === 'function' ?
profileName() : profileName;
global.nativeTraceEndAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie, 0);
}
},
reactPerfMeasure(objName: string, fnName: string, func: any): any {
return function (component) {
if (!_enabled) {
@ -69,11 +97,15 @@ var BridgeProfiling = {
ReactPerf().injection.injectMeasure(BridgeProfiling.reactPerfMeasure);
},
/**
* Relay profiles use await calls, so likely occur out of current stack frame
* therefore async variant of profiling is used
**/
attachToRelayProfiler(relayProfiler: RelayProfiler) {
relayProfiler.attachProfileHandler('*', (name) => {
BridgeProfiling.profile(name);
var cookie = BridgeProfiling.profileAsync(name);
return () => {
BridgeProfiling.profileEnd();
BridgeProfiling.profileAsyncEnd(name, cookie);
};
});
},