From d93d68a64763a89e7ac9f22511f96021e2d60fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 3 Feb 2025 03:46:13 -0800 Subject: [PATCH] Add method to take a save a JS memory heap snapshot Summary: Changelog: [internal] This adds support for taking JS memory heap snapshots from Fantom tests via `Fantom.saveJSMemoryHeapSnapshot`. This can be used in one-off tests to do memory analysis and determine the existence of leaks: ``` // Warm up Fantom.saveJSMemoryHeapSnapshot('/path/to/my/1.heapsnapshot'); // Do work Fantom.saveJSMemoryHeapSnapshot('/path/to/my/2.heapsnapshot'); // Clean up Fantom.saveJSMemoryHeapSnapshot('/path/to/my/3.heapsnapshot'); ``` Load these snapshots in Chrome and select "Objects allocated between 1 and 2" in the dropdown to see the potentially leaked objects. In the future we could introduce additional utilities to analyze the snapshots and do the detection automatically, e.g.: ``` // Warm up const baseline = Fantom.takeJSMemoryHeapSnapshot(); // Do work const before = Fantom.takeJSMemoryHeapSnapshot(); // Clean up const after = Fantom.takeJSMemoryHeapSnapshot(); const leaks = findMemoryLeaks(baseline, before, after); expect(leaks.sizeKB()).toBeLessThan(THRESHOLD); ``` Differential Revision: D68953788 --- packages/react-native-fantom/src/index.js | 9 +++++++++ .../src/private/specs/modules/NativeFantom.js | 1 + 2 files changed, 10 insertions(+) diff --git a/packages/react-native-fantom/src/index.js b/packages/react-native-fantom/src/index.js index 273ff804ada831..28a95ca489a4af 100644 --- a/packages/react-native-fantom/src/index.js +++ b/packages/react-native-fantom/src/index.js @@ -272,6 +272,14 @@ if (typeof global.EventTarget === 'undefined') { ); } +function saveJSMemoryHeapSnapshot(filePath: string): void { + if (getConstants().isRunningFromCI) { + throw new Error('Unexpected call to `saveJSMemoryHeapSnapshot` from CI'); + } + + NativeFantom.saveJSMemoryHeapSnapshot(filePath); +} + export default { scheduleTask, runTask, @@ -282,4 +290,5 @@ export default { flushAllNativeEvents, unstable_benchmark: Benchmark, scrollTo, + saveJSMemoryHeapSnapshot, }; diff --git a/packages/react-native/src/private/specs/modules/NativeFantom.js b/packages/react-native/src/private/specs/modules/NativeFantom.js index 2a956ed2527200..83542e7fb7ce9e 100644 --- a/packages/react-native/src/private/specs/modules/NativeFantom.js +++ b/packages/react-native/src/private/specs/modules/NativeFantom.js @@ -82,6 +82,7 @@ interface Spec extends TurboModule { validateEmptyMessageQueue: () => void; getRenderedOutput: (surfaceId: number, config: RenderFormatOptions) => string; reportTestSuiteResultsJSON: (results: string) => void; + saveJSMemoryHeapSnapshot: (filePath: string) => void; } export default TurboModuleRegistry.getEnforcing(