2015-06-02 13:15:53 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2015-12-11 11:49:15 +00:00
|
|
|
* @providesModule Systrace
|
2015-06-02 13:15:53 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-11-24 03:22:34 +00:00
|
|
|
type RelayProfiler = {
|
|
|
|
attachProfileHandler(
|
|
|
|
name: string,
|
|
|
|
handler: (name: string, state?: any) => () => void
|
2015-12-11 19:38:19 +00:00
|
|
|
): void,
|
|
|
|
|
|
|
|
attachAggregateHandler(
|
|
|
|
name: string,
|
|
|
|
handler: (name: string, callback: () => void) => void
|
|
|
|
): void,
|
2015-11-24 03:22:34 +00:00
|
|
|
};
|
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
/* eslint no-bitwise: 0 */
|
|
|
|
const TRACE_TAG_REACT_APPS = 1 << 17;
|
|
|
|
const TRACE_TAG_JSC_CALLS = 1 << 27;
|
2016-05-10 22:57:19 +00:00
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
let _enabled = false;
|
|
|
|
let _asyncCookie = 0;
|
|
|
|
|
|
|
|
const ReactSystraceDevtool = __DEV__ ? {
|
2016-05-10 22:57:19 +00:00
|
|
|
onBeginReconcilerTimer(debugID, timerType) {
|
2016-05-23 11:51:25 +00:00
|
|
|
const displayName = require('ReactComponentTreeDevtool').getDisplayName(debugID);
|
2016-05-10 22:57:19 +00:00
|
|
|
Systrace.beginEvent(`ReactReconciler.${timerType}(${displayName})`);
|
|
|
|
},
|
|
|
|
onEndReconcilerTimer(debugID, timerType) {
|
|
|
|
Systrace.endEvent();
|
|
|
|
},
|
|
|
|
onBeginLifeCycleTimer(debugID, timerType) {
|
2016-05-23 11:51:25 +00:00
|
|
|
const displayName = require('ReactComponentTreeDevtool').getDisplayName(debugID);
|
2016-05-10 22:57:19 +00:00
|
|
|
Systrace.beginEvent(`${displayName}.${timerType}()`);
|
|
|
|
},
|
|
|
|
onEndLifeCycleTimer(debugID, timerType) {
|
|
|
|
Systrace.endEvent();
|
|
|
|
},
|
2016-05-23 11:51:25 +00:00
|
|
|
} : null;
|
2015-10-01 21:08:13 +00:00
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
const Systrace = {
|
2015-10-01 21:08:13 +00:00
|
|
|
setEnabled(enabled: boolean) {
|
2015-12-10 12:36:23 +00:00
|
|
|
if (_enabled !== enabled) {
|
2016-05-23 11:51:25 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
if (enabled) {
|
|
|
|
global.nativeTraceBeginLegacy && global.nativeTraceBeginLegacy(TRACE_TAG_JSC_CALLS);
|
|
|
|
require('ReactDebugTool').addDevtool(ReactSystraceDevtool);
|
|
|
|
} else {
|
|
|
|
global.nativeTraceEndLegacy && global.nativeTraceEndLegacy(TRACE_TAG_JSC_CALLS);
|
|
|
|
require('ReactDebugTool').removeDevtool(ReactSystraceDevtool);
|
|
|
|
}
|
2015-12-10 12:36:23 +00:00
|
|
|
}
|
2016-05-23 11:51:25 +00:00
|
|
|
_enabled = enabled;
|
2015-12-10 12:36:23 +00:00
|
|
|
}
|
2015-10-01 21:08:13 +00:00
|
|
|
},
|
|
|
|
|
2015-12-01 15:37:52 +00:00
|
|
|
/**
|
2015-12-11 11:49:15 +00:00
|
|
|
* beginEvent/endEvent for starting and then ending a profile within the same call stack frame
|
2015-12-01 15:37:52 +00:00
|
|
|
**/
|
2015-12-11 11:49:15 +00:00
|
|
|
beginEvent(profileName?: any) {
|
2015-10-01 21:08:13 +00:00
|
|
|
if (_enabled) {
|
[ReactNative] Refactor BatchedBridge and MessageQueue
Summary:
@public
The current implementation of `MessageQueue` is huge, over-complicated and spread
across `MethodQueue`, `MethodQueueMixin`, `BatchedBridge` and `BatchedBridgeFactory`
Refactored in a simpler way, were it's just a `MessageQueue` class and `BatchedBridge`
is only an instance of it.
Test Plan:
I had to make some updates to the tests, but no real update to the native side.
There's also tests covering the `remoteAsync` methods, and more integration tests for UIExplorer.
Verified whats being used by Android, and it should be safe, also tests Android tests have been pretty reliable.
Manually testing: Create a big hierarchy, like `<ListView>` example. Use the `TimerMixin` example to generate multiple calls.
Test the failure callback on the `Geolocation` example.
All the calls go through this entry point, so it's hard to miss if it's broken.
2015-06-17 14:51:48 +00:00
|
|
|
profileName = typeof profileName === 'function' ?
|
|
|
|
profileName() : profileName;
|
2015-10-31 23:41:27 +00:00
|
|
|
global.nativeTraceBeginSection(TRACE_TAG_REACT_APPS, profileName);
|
2015-06-02 13:15:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-11 11:49:15 +00:00
|
|
|
endEvent() {
|
2015-10-01 21:08:13 +00:00
|
|
|
if (_enabled) {
|
2015-10-31 23:41:27 +00:00
|
|
|
global.nativeTraceEndSection(TRACE_TAG_REACT_APPS);
|
2015-06-02 13:15:53 +00:00
|
|
|
}
|
|
|
|
},
|
2015-06-15 20:05:05 +00:00
|
|
|
|
2015-12-01 15:37:52 +00:00
|
|
|
/**
|
2015-12-11 11:49:15 +00:00
|
|
|
* beginAsyncEvent/endAsyncEvent for starting and then ending a profile where the end can either
|
2015-12-01 15:37:52 +00:00
|
|
|
* occur on another thread or out of the current stack frame, eg await
|
2015-12-11 11:49:15 +00:00
|
|
|
* the returned cookie variable should be used as input into the endAsyncEvent call to end the profile
|
2015-12-01 15:37:52 +00:00
|
|
|
**/
|
2015-12-11 11:49:15 +00:00
|
|
|
beginAsyncEvent(profileName?: any): any {
|
2016-05-23 11:51:25 +00:00
|
|
|
const cookie = _asyncCookie;
|
2015-12-01 15:37:52 +00:00
|
|
|
if (_enabled) {
|
|
|
|
_asyncCookie++;
|
|
|
|
profileName = typeof profileName === 'function' ?
|
|
|
|
profileName() : profileName;
|
|
|
|
global.nativeTraceBeginAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie, 0);
|
|
|
|
}
|
|
|
|
return cookie;
|
|
|
|
},
|
|
|
|
|
2015-12-11 11:49:15 +00:00
|
|
|
endAsyncEvent(profileName?: any, cookie?: any) {
|
2015-12-01 15:37:52 +00:00
|
|
|
if (_enabled) {
|
|
|
|
profileName = typeof profileName === 'function' ?
|
|
|
|
profileName() : profileName;
|
|
|
|
global.nativeTraceEndAsyncSection(TRACE_TAG_REACT_APPS, profileName, cookie, 0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-30 07:36:34 +00:00
|
|
|
/**
|
|
|
|
* counterEvent registers the value to the profileName on the systrace timeline
|
|
|
|
**/
|
|
|
|
counterEvent(profileName?: any, value?: any) {
|
|
|
|
if (_enabled) {
|
|
|
|
profileName = typeof profileName === 'function' ?
|
|
|
|
profileName() : profileName;
|
|
|
|
global.nativeTraceCounter &&
|
|
|
|
global.nativeTraceCounter(TRACE_TAG_REACT_APPS, profileName, value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-01 15:37:52 +00:00
|
|
|
/**
|
|
|
|
* Relay profiles use await calls, so likely occur out of current stack frame
|
|
|
|
* therefore async variant of profiling is used
|
|
|
|
**/
|
2015-11-24 03:22:34 +00:00
|
|
|
attachToRelayProfiler(relayProfiler: RelayProfiler) {
|
|
|
|
relayProfiler.attachProfileHandler('*', (name) => {
|
2016-05-23 11:51:25 +00:00
|
|
|
const cookie = Systrace.beginAsyncEvent(name);
|
2015-11-24 03:22:34 +00:00
|
|
|
return () => {
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.endAsyncEvent(name, cookie);
|
2015-11-24 03:22:34 +00:00
|
|
|
};
|
|
|
|
});
|
2015-12-11 19:38:19 +00:00
|
|
|
|
|
|
|
relayProfiler.attachAggregateHandler('*', (name, callback) => {
|
|
|
|
Systrace.beginEvent(name);
|
|
|
|
callback();
|
|
|
|
Systrace.endEvent();
|
|
|
|
});
|
2015-11-23 15:01:06 +00:00
|
|
|
},
|
|
|
|
|
2015-11-23 15:01:11 +00:00
|
|
|
/* 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() {
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.measureMethods(JSON, 'JSON', [
|
2015-11-23 15:01:11 +00:00
|
|
|
'parse',
|
|
|
|
'stringify'
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
|
2015-11-23 15:01:06 +00:00
|
|
|
/**
|
|
|
|
* Measures multiple methods of a class. For example, you can do:
|
2015-12-11 11:49:15 +00:00
|
|
|
* Systrace.measureMethods(JSON, 'JSON', ['parse', 'stringify']);
|
2015-11-23 15:01:06 +00:00
|
|
|
*
|
|
|
|
* @param object
|
|
|
|
* @param objectName
|
|
|
|
* @param methodNames Map from method names to method display names.
|
|
|
|
*/
|
|
|
|
measureMethods(object: any, objectName: string, methodNames: Array<string>): void {
|
|
|
|
if (!__DEV__) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
methodNames.forEach(methodName => {
|
2015-12-11 11:49:15 +00:00
|
|
|
object[methodName] = Systrace.measure(
|
2015-11-23 15:01:06 +00:00
|
|
|
objectName,
|
|
|
|
methodName,
|
|
|
|
object[methodName]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an profiled version of the input function. For example, you can:
|
2015-12-11 11:49:15 +00:00
|
|
|
* JSON.parse = Systrace.measure('JSON', 'parse', JSON.parse);
|
2015-11-23 15:01:06 +00:00
|
|
|
*
|
|
|
|
* @param objName
|
|
|
|
* @param fnName
|
|
|
|
* @param {function} func
|
|
|
|
* @return {function} replacement function
|
|
|
|
*/
|
|
|
|
measure(objName: string, fnName: string, func: any): any {
|
|
|
|
if (!__DEV__) {
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2016-05-23 11:51:25 +00:00
|
|
|
const profileName = `${objName}.${fnName}`;
|
2015-11-23 15:01:06 +00:00
|
|
|
return function() {
|
|
|
|
if (!_enabled) {
|
|
|
|
return func.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.beginEvent(profileName);
|
2016-05-23 11:51:25 +00:00
|
|
|
const ret = func.apply(this, arguments);
|
2015-12-11 11:49:15 +00:00
|
|
|
Systrace.endEvent();
|
2015-11-23 15:01:06 +00:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
},
|
2015-06-02 13:15:53 +00:00
|
|
|
};
|
|
|
|
|
2016-03-14 23:16:22 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
// This is needed, because require callis in polyfills are not processed as
|
|
|
|
// other files. Therefore, calls to `require('moduleId')` are not replaced
|
|
|
|
// with numeric IDs
|
|
|
|
// TODO(davidaurelio) Scan polyfills for dependencies, too (t9759686)
|
|
|
|
require.Systrace = Systrace;
|
|
|
|
}
|
|
|
|
|
2015-12-11 11:49:15 +00:00
|
|
|
module.exports = Systrace;
|