From 02b67d9d403e4c7fb02bbfc2b21b74b8db1d1352 Mon Sep 17 00:00:00 2001 From: Milen Dzhumerov Date: Mon, 23 Nov 2015 07:01:06 -0800 Subject: [PATCH] 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 --- Libraries/Utilities/BridgeProfiling.js | 51 +++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/Libraries/Utilities/BridgeProfiling.js b/Libraries/Utilities/BridgeProfiling.js index e62c8b76d..e707b357d 100644 --- a/Libraries/Utilities/BridgeProfiling.js +++ b/Libraries/Utilities/BridgeProfiling.js @@ -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): 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);