-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #598 from margelo/chore/add-metrics
chore: re-enable performance metrics
- Loading branch information
Showing
10 changed files
with
250 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Stores settings from Onyx.init globally so they can be made accessible by other parts of the library. | ||
*/ | ||
|
||
const globalSettings = { | ||
enablePerformanceMetrics: false, | ||
}; | ||
|
||
type GlobalSettings = typeof globalSettings; | ||
|
||
const listeners = new Set<(settings: GlobalSettings) => unknown>(); | ||
function addGlobalSettingsChangeListener(listener: (settings: GlobalSettings) => unknown) { | ||
listeners.add(listener); | ||
return () => { | ||
listeners.delete(listener); | ||
}; | ||
} | ||
|
||
function notifyListeners() { | ||
listeners.forEach((listener) => listener(globalSettings)); | ||
} | ||
|
||
function setPerformanceMetricsEnabled(enabled: boolean) { | ||
globalSettings.enablePerformanceMetrics = enabled; | ||
notifyListeners(); | ||
} | ||
|
||
function isPerformanceMetricsEnabled() { | ||
return globalSettings.enablePerformanceMetrics; | ||
} | ||
|
||
export {setPerformanceMetricsEnabled, isPerformanceMetricsEnabled, addGlobalSettingsChangeListener}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
type ImportType = ReturnType<typeof require>; | ||
|
||
/** | ||
* Create a lazily-imported module proxy. | ||
* This is useful for lazily requiring optional dependencies. | ||
*/ | ||
const createModuleProxy = <TModule>(getModule: () => ImportType): TModule => { | ||
const holder: {module: TModule | undefined} = {module: undefined}; | ||
|
||
const proxy = new Proxy(holder, { | ||
get: (target, property) => { | ||
if (property === '$$typeof') { | ||
// If inlineRequires is enabled, Metro will look up all imports | ||
// with the $$typeof operator. In this case, this will throw the | ||
// `OptionalDependencyNotInstalledError` error because we try to access the module | ||
// even though we are not using it (Metro does it), so instead we return undefined | ||
// to bail out of inlineRequires here. | ||
return undefined; | ||
} | ||
|
||
if (target.module == null) { | ||
// lazy initialize module via require() | ||
// caller needs to make sure the require() call is wrapped in a try/catch | ||
// eslint-disable-next-line no-param-reassign | ||
target.module = getModule() as TModule; | ||
} | ||
return target.module[property as keyof typeof holder.module]; | ||
}, | ||
}); | ||
return proxy as unknown as TModule; | ||
}; | ||
|
||
class OptionalDependencyNotInstalledError extends Error { | ||
constructor(name: string) { | ||
super(`${name} is not installed!`); | ||
} | ||
} | ||
|
||
export {createModuleProxy, OptionalDependencyNotInstalledError}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type performance from 'react-native-performance'; | ||
import {createModuleProxy, OptionalDependencyNotInstalledError} from '../ModuleProxy'; | ||
|
||
const PerformanceProxy = createModuleProxy<typeof performance>(() => { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
return require('react-native-performance').default; | ||
} catch { | ||
throw new OptionalDependencyNotInstalledError('react-native-performance'); | ||
} | ||
}); | ||
|
||
export default PerformanceProxy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Use the existing performance API on web | ||
export default performance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import PerformanceProxy from './dependencies/PerformanceProxy'; | ||
|
||
const decoratedAliases = new Set(); | ||
|
||
/** | ||
* Capture a measurement between the start mark and now | ||
*/ | ||
function measureMarkToNow(startMark: PerformanceMark, detail: Record<string, unknown>) { | ||
PerformanceProxy.measure(`${startMark.name} [${startMark.detail.args.toString()}]`, { | ||
start: startMark.startTime, | ||
end: PerformanceProxy.now(), | ||
detail: {...startMark.detail, ...detail}, | ||
}); | ||
} | ||
|
||
function isPromiseLike(value: unknown): value is Promise<unknown> { | ||
return value != null && typeof value === 'object' && 'then' in value; | ||
} | ||
|
||
/** | ||
* Wraps a function with metrics capturing logic | ||
*/ | ||
function decorateWithMetrics<Args extends unknown[], ReturnType>(func: (...args: Args) => ReturnType, alias = func.name) { | ||
if (decoratedAliases.has(alias)) { | ||
throw new Error(`"${alias}" is already decorated`); | ||
} | ||
|
||
decoratedAliases.add(alias); | ||
function decorated(...args: Args) { | ||
const mark = PerformanceProxy.mark(alias, {detail: {args, alias}}); | ||
|
||
const originalReturnValue = func(...args); | ||
|
||
if (isPromiseLike(originalReturnValue)) { | ||
/* | ||
* The handlers added here are not affecting the original promise | ||
* They create a separate chain that's not exposed (returned) to the original caller | ||
*/ | ||
originalReturnValue | ||
.then((result) => { | ||
measureMarkToNow(mark, {result}); | ||
}) | ||
.catch((error) => { | ||
measureMarkToNow(mark, {error}); | ||
}); | ||
|
||
return originalReturnValue; | ||
} | ||
|
||
measureMarkToNow(mark, {result: originalReturnValue}); | ||
return originalReturnValue; | ||
} | ||
decorated.name = `${alias}_DECORATED`; | ||
|
||
return decorated; | ||
} | ||
|
||
export default decorateWithMetrics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters