diff --git a/Libraries/Performance/Systrace.d.ts b/Libraries/Performance/Systrace.d.ts index 3340158c545..6b1e7371269 100644 --- a/Libraries/Performance/Systrace.d.ts +++ b/Libraries/Performance/Systrace.d.ts @@ -7,70 +7,60 @@ * @format */ -export interface RelayProfiler { - attachProfileHandler( - name: string, - handler: (name: string, state?: any) => () => void, - ): void; +type EventName = string | (() => string); +type EventArgs = {[key: string]: string} | void | null; - attachAggregateHandler( - name: string, - handler: (name: string, callback: () => void) => void, - ): void; -} +/** + * Indicates if the application is currently being traced. + * + * Calling methods on this module when the application isn't being traced is + * cheap, but this method can be used to avoid computing expensive values for + * those functions. + * + * @example + * if (Systrace.isEnabled()) { + * const expensiveArgs = computeExpensiveArgs(); + * Systrace.beginEvent('myEvent', expensiveArgs); + * } + */ +export function isEnabled(): boolean; -export interface SystraceStatic { - setEnabled(enabled: boolean): void; +/** + * @deprecated This function is now a no-op but it's left for backwards + * compatibility. `isEnabled` will now synchronously check if we're actively + * profiling or not. This is necessary because we don't have callbacks to know + * when profiling has started/stopped on Android APIs. + */ +export function setEnabled(_doEnable: boolean): void; - /** - * beginEvent/endEvent for starting and then ending a profile within the same call stack frame - **/ - beginEvent(profileName?: any, args?: any): void; - endEvent(): void; +/** + * Marks the start of a synchronous event that should end in the same stack + * frame. The end of this event should be marked using the `endEvent` function. + */ +export function beginEvent(eventName: EventName, args?: EventArgs): void; - /** - * beginAsyncEvent/endAsyncEvent 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 endAsyncEvent call to end the profile - **/ - beginAsyncEvent(profileName?: any): any; - endAsyncEvent(profileName?: any, cookie?: any): void; +/** + * Marks the end of a synchronous event started in the same stack frame. + */ +export function endEvent(args?: EventArgs): void; - /** - * counterEvent registers the value to the profileName on the systrace timeline - **/ - counterEvent(profileName?: any, value?: any): void; +/** + * Marks the start of a potentially asynchronous event. The end of this event + * should be marked calling the `endAsyncEvent` function with the cookie + * returned by this function. + */ +export function beginAsyncEvent(eventName: EventName, args?: EventArgs): number; - /** - * Relay profiles use await calls, so likely occur out of current stack frame - * therefore async variant of profiling is used - **/ - attachToRelayProfiler(relayProfiler: RelayProfiler): void; +/** + * Registers a new value for a counter event. + */ +export function endAsyncEvent( + eventName: EventName, + cookie: number, + args?: EventArgs, +): void; - /* 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(): void; - - /** - * Measures multiple methods of a class. For example, you can do: - * Systrace.measureMethods(JSON, 'JSON', ['parse', 'stringify']); - * - * @param methodNames Map from method names to method display names. - */ - measureMethods( - object: any, - objectName: string, - methodNames: Array, - ): void; - - /** - * Returns an profiled version of the input function. For example, you can: - * JSON.parse = Systrace.measure('JSON', 'parse', JSON.parse); - * - * @return replacement function - */ - measure(objName: string, fnName: string, func: T): T; -} - -export const Systrace: SystraceStatic; -export type Systrace = SystraceStatic; +/** + * counterEvent registers the value to the eventName on the systrace timeline + **/ +export function counterEvent(eventName: EventName, value: number): void; diff --git a/Libraries/Performance/Systrace.js b/Libraries/Performance/Systrace.js index 9310ad0a8e8..a7cdce1a523 100644 --- a/Libraries/Performance/Systrace.js +++ b/Libraries/Performance/Systrace.js @@ -8,94 +8,118 @@ * @format */ -'use strict'; +import * as Systrace from './Systrace'; const TRACE_TAG_REACT_APPS = 1 << 17; // eslint-disable-line no-bitwise let _enabled = false; let _asyncCookie = 0; -const Systrace = { - setEnabled(enabled: boolean) { - _enabled = enabled; - }, +type EventName = string | (() => string); +type EventArgs = ?{[string]: string}; - isEnabled(): boolean { - return _enabled; - }, +/** + * Indicates if the application is currently being traced. + * + * Calling methods on this module when the application isn't being traced is + * cheap, but this method can be used to avoid computing expensive values for + * those functions. + * + * @example + * if (Systrace.isEnabled()) { + * const expensiveArgs = computeExpensiveArgs(); + * Systrace.beginEvent('myEvent', expensiveArgs); + * } + */ +export function isEnabled(): boolean { + return _enabled; +} - /** - * beginEvent/endEvent for starting and then ending a profile within the same call stack frame - **/ - beginEvent( - profileName?: string | (() => string), - args?: {[string]: string, ...}, - ) { - if (_enabled) { - const profileNameString = - typeof profileName === 'function' ? profileName() : profileName; - global.nativeTraceBeginSection( - TRACE_TAG_REACT_APPS, - profileNameString, - args, - ); - } - }, +/** + * @deprecated This function is now a no-op but it's left for backwards + * compatibility. `isEnabled` will now synchronously check if we're actively + * profiling or not. This is necessary because we don't have callbacks to know + * when profiling has started/stopped on Android APIs. + */ +export function setEnabled(_doEnable: boolean): void {} - endEvent() { - if (_enabled) { - global.nativeTraceEndSection(TRACE_TAG_REACT_APPS); - } - }, +/** + * Marks the start of a synchronous event that should end in the same stack + * frame. The end of this event should be marked using the `endEvent` function. + */ +export function beginEvent(eventName: EventName, args?: EventArgs): void { + if (_enabled) { + const eventNameString = + typeof eventName === 'function' ? eventName() : eventName; + global.nativeTraceBeginSection(TRACE_TAG_REACT_APPS, eventNameString, args); + } +} - /** - * beginAsyncEvent/endAsyncEvent 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 endAsyncEvent call to end the profile - **/ - beginAsyncEvent(profileName?: string | (() => string)): number { - const cookie = _asyncCookie; - if (_enabled) { - _asyncCookie++; - const profileNameString = - typeof profileName === 'function' ? profileName() : profileName; - global.nativeTraceBeginAsyncSection( - TRACE_TAG_REACT_APPS, - profileNameString, - cookie, - ); - } - return cookie; - }, +/** + * Marks the end of a synchronous event started in the same stack frame. + */ +export function endEvent(args?: EventArgs): void { + if (_enabled) { + global.nativeTraceEndSection(TRACE_TAG_REACT_APPS, args); + } +} - endAsyncEvent(profileName?: string | (() => string), cookie?: number) { - if (_enabled) { - const profileNameString = - typeof profileName === 'function' ? profileName() : profileName; - global.nativeTraceEndAsyncSection( - TRACE_TAG_REACT_APPS, - profileNameString, - cookie, - ); - } - }, +/** + * Marks the start of a potentially asynchronous event. The end of this event + * should be marked calling the `endAsyncEvent` function with the cookie + * returned by this function. + */ +export function beginAsyncEvent( + eventName: EventName, + args?: EventArgs, +): number { + const cookie = _asyncCookie; + if (_enabled) { + _asyncCookie++; + const eventNameString = + typeof eventName === 'function' ? eventName() : eventName; + global.nativeTraceBeginAsyncSection( + TRACE_TAG_REACT_APPS, + eventNameString, + cookie, + args, + ); + } + return cookie; +} - /** - * counterEvent registers the value to the profileName on the systrace timeline - **/ - counterEvent(profileName?: string | (() => string), value?: number) { - if (_enabled) { - const profileNameString = - typeof profileName === 'function' ? profileName() : profileName; - global.nativeTraceCounter && - global.nativeTraceCounter( - TRACE_TAG_REACT_APPS, - profileNameString, - value, - ); - } - }, -}; +/** + * Marks the end of a potentially asynchronous event, which was started with + * the given cookie. + */ +export function endAsyncEvent( + eventName: EventName, + cookie: number, + args?: EventArgs, +): void { + if (_enabled) { + const eventNameString = + typeof eventName === 'function' ? eventName() : eventName; + global.nativeTraceEndAsyncSection( + TRACE_TAG_REACT_APPS, + eventNameString, + cookie, + args, + ); + } +} + +/** + * Registers a new value for a counter event. + */ +export function counterEvent(eventName: EventName, value: number): void { + if (_enabled) { + const eventNameString = + typeof eventName === 'function' ? eventName() : eventName; + global.nativeTraceCounter && + global.nativeTraceCounter(TRACE_TAG_REACT_APPS, eventNameString, value); + } +} if (__DEV__) { // The metro require polyfill can not have dependencies (true for all polyfills). @@ -104,7 +128,5 @@ if (__DEV__) { } if (global.__RCTProfileIsProfiling) { - Systrace.setEnabled(true); + _enabled = true; } - -module.exports = Systrace; diff --git a/index.js b/index.js index 31d15f58d3e..056fb7d8bd3 100644 --- a/index.js +++ b/index.js @@ -76,7 +76,7 @@ import typeof PushNotificationIOS from './Libraries/PushNotificationIOS/PushNoti import typeof Settings from './Libraries/Settings/Settings'; import typeof Share from './Libraries/Share/Share'; import typeof StyleSheet from './Libraries/StyleSheet/StyleSheet'; -import typeof Systrace from './Libraries/Performance/Systrace'; +import typeof * as Systrace from './Libraries/Performance/Systrace'; import typeof ToastAndroid from './Libraries/Components/ToastAndroid/ToastAndroid'; import typeof * as TurboModuleRegistry from './Libraries/TurboModule/TurboModuleRegistry'; import typeof UIManager from './Libraries/ReactNative/UIManager'; diff --git a/types/index.d.ts b/types/index.d.ts index fdab96b0d5d..ad0f8e34046 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -122,7 +122,7 @@ export * from '../Libraries/Lists/SectionList'; export * from '../Libraries/Lists/VirtualizedList'; export * from '../Libraries/LogBox/LogBox'; export * from '../Libraries/Modal/Modal'; -export * from '../Libraries/Performance/Systrace'; +export * as Systrace from '../Libraries/Performance/Systrace'; export * from '../Libraries/PermissionsAndroid/PermissionsAndroid'; export * from '../Libraries/PushNotificationIOS/PushNotificationIOS'; export * from '../Libraries/ReactNative/AppRegistry';