-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support tagging memory leaks in js-under-test
Summary: **How it works:** 1. In web app, we know at certain points some objects are no longer needed to be kept alive in memory. For those objects at the right time, we can "tag" or "mark" those objects that should not be kept alive. Those targged objects will be added to a special group of `WeakSet` in browser memory after their React components are released. 2. MemLab will examine the heap snapshots and mark all such objects as memory leaks. NOTE: In MemLab, we control when GC will be triggered, so all objects in the heap snapshots are guaranteed to survive the GC, which means all tagged objects tracked by the logic added in this diff are memory leaks. Differential Revision: D50312753 Privacy Context Container: L1212261 fbshipit-source-id: fc85bde625c06ef4a5a3626a3d7e0ca0ce12ebd3
- Loading branch information
1 parent
80fd71b
commit 040315e
Showing
15 changed files
with
255 additions
and
17 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
packages/api/src/__tests__/API/E2EFindTaggedMemoryLeaks.test.ts
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,101 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @oncall web_perf_infra | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
|
||
import type {Page} from 'puppeteer'; | ||
import type {IScenario} from '@memlab/core'; | ||
|
||
import os from 'os'; | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import {run} from '../../index'; | ||
import {testSetup, testTimeout} from './lib/E2ETestSettings'; | ||
|
||
beforeEach(testSetup); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
type Objectish = object | Function; | ||
|
||
// The structure of classes and objects should be fixed | ||
// so that MemLab can analyze them correctly in heap. | ||
type TrackedItem = { | ||
useCaseId: string; | ||
taggedObjects: WeakSet<Objectish>; | ||
}; | ||
|
||
function tagObjectsAsLeaks() { | ||
// this class definition must be defined within `tagObjectsAsLeaks` | ||
// since this function will be serialized and executed in browser context | ||
class MemLabTracker { | ||
memlabIdentifier: string; | ||
tagToTrackedObjectsMap: Map<string, TrackedItem>; | ||
|
||
constructor() { | ||
this.memlabIdentifier = 'MemLabObjectTracker'; | ||
this.tagToTrackedObjectsMap = new Map(); | ||
} | ||
|
||
tag(target: Objectish, useCaseId = 'MEMLAB_TAGGED'): void { | ||
let trackedItems = this.tagToTrackedObjectsMap.get(useCaseId); | ||
if (!trackedItems) { | ||
trackedItems = { | ||
useCaseId, | ||
taggedObjects: new WeakSet(), | ||
}; | ||
this.tagToTrackedObjectsMap.set(useCaseId, trackedItems); | ||
} | ||
trackedItems.taggedObjects.add(target); | ||
} | ||
} | ||
|
||
// @ts-ignore | ||
window.injectHookForLink4 = () => { | ||
// @ts-ignore | ||
const tracker = (window._memlabTrack = new MemLabTracker()); | ||
const leaks: Array<{x: number}> = []; | ||
// @ts-ignore | ||
window._taggedLeaks = leaks; | ||
for (let i = 0; i < 11; ++i) { | ||
leaks.push({x: i}); | ||
} | ||
leaks.forEach(item => { | ||
tracker.tag(item); | ||
}); | ||
}; | ||
} | ||
|
||
test( | ||
'tagged leak objects can be identified as leaks', | ||
async () => { | ||
const selfDefinedScenario: IScenario = { | ||
app: (): string => 'test-spa', | ||
url: (): string => '', | ||
action: async (page: Page): Promise<void> => | ||
await page.click('[data-testid="link-4"]'), | ||
}; | ||
|
||
const workDir = path.join(os.tmpdir(), 'memlab-api-test', `${process.pid}`); | ||
fs.mkdirsSync(workDir); | ||
|
||
const result = await run({ | ||
scenario: selfDefinedScenario, | ||
evalInBrowserAfterInitLoad: tagObjectsAsLeaks, | ||
workDir, | ||
}); | ||
// tagged objects should be detected as leaks | ||
expect(result.leaks.length).toBe(1); | ||
// expect all traces are found | ||
expect( | ||
result.leaks.some(leak => JSON.stringify(leak).includes('_taggedLeaks')), | ||
); | ||
}, | ||
testTimeout, | ||
); |
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
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
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
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
Oops, something went wrong.