-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: re-enable performance metrics #598
Open
hannojg
wants to merge
7
commits into
Expensify:main
Choose a base branch
from
margelo:chore/add-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−6
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92817c4
wip: re-enable performance metrics
hannojg 3f21827
add decorator pattern also to onyx utils
hannojg 65c1571
decorate storage as well
hannojg 2578654
remove example
hannojg 0494c31
provide web impl
hannojg 6e5275f
Merge branch 'main' of github.com:Expensify/react-native-onyx into ch…
hannojg 42055a0
use typeof
hannojg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ love this!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i only hate the
ts-expect-error
suppressions 🤔