BridgeProfiling measure methods

Summary: public Add measure() family of methods which allow to easily swizzle methods for profiling

Reviewed By: tadeuzagallo

Differential Revision: D2679904

fb-gh-sync-id: 3724440e1bdaca9e854f4d4124a897a204966dc7
This commit is contained in:
Milen Dzhumerov 2015-11-23 07:01:06 -08:00 committed by facebook-github-bot-7
parent 6b3a6e5958
commit 02b67d9d40
1 changed files with 50 additions and 1 deletions

View File

@ -78,7 +78,56 @@ var BridgeProfiling = {
};
});
} catch(err) {}
}
},
/**
* Measures multiple methods of a class. For example, you can do:
* BridgeProfiling.measureMethods(JSON, 'JSON', ['parse', 'stringify']);
*
* @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 => {
object[methodName] = BridgeProfiling.measure(
objectName,
methodName,
object[methodName]
);
});
},
/**
* Returns an profiled version of the input function. For example, you can:
* JSON.parse = BridgeProfiling.measure('JSON', 'parse', JSON.parse);
*
* @param objName
* @param fnName
* @param {function} func
* @return {function} replacement function
*/
measure(objName: string, fnName: string, func: any): any {
if (!__DEV__) {
return func;
}
var profileName = `${objName}.${fnName}`;
return function() {
if (!_enabled) {
return func.apply(this, arguments);
}
BridgeProfiling.profile(profileName);
var ret = func.apply(this, arguments);
BridgeProfiling.profileEnd();
return ret;
};
},
};
BridgeProfiling.setEnabled(global.__RCTProfileIsProfiling || false);