Discussion: Is it possible to add return value to heap-analysis plugins?
See original GitHub issueHi team,
Thanks for a lot for let this amazing project open source!
I am from the Lark desktop Team of ByteDance, and we would like to use memlab to help us find memory leaks.
In our scenario, Lark is a desktop app, not a SPA webpage, we can’t use memlab to start a puppeteer and run the e2e test, and the cost to rewrite all of out test cases using memlab is too high to afford.
Thus we want to use our existing e2e framwork to take snapshot, and pass it to a node server running memlab to analyse it.
Here is a minimal demo running memlab at a node server https://github.com/MoonShadowV/memlab-demo-server
It works fine for findLeaks as it dumps the result to the disk => /data/out/leaks.txt
import { findLeaks, BrowserInteractionResultReader } from '@memlab/api';
import { MEMLAB_WORKDIR } from './constant';
async function memlabFindLeak() {
try {
const reader = BrowserInteractionResultReader.from(MEMLAB_WORKDIR);
const leaks = await findLeaks(reader);
return Boolean(leaks);
} catch (error) {
throw new Error(`memlab analyse failed: ${error}`);
}
}
export { memlabFindLeak };
But if we want to analyse single snapshot, the heap-analysis plugin return nothing, just print it to the termianl. We can’t get the output.
eg: https://github.com/facebook/memlab/blob/main/packages/heap-analysis/src/plugins/DetachedDOMElementAnalysis.ts#L50 In this plugin, the process function just call
async process(options: HeapAnalysisOptions): Promise<void> {
const snapshot = await pluginUtils.loadHeapSnapshot(options);
this.detachedElements = pluginUtils.filterOutLargestObjects(
snapshot,
utils.isDetachedDOMNode,
);
pluginUtils.printNodeListInTerminal(this.detachedElements); // print result to terminal, return nothing
}
to print the detachedElements in the terminal.
we can not access it from outside. ↓
(async function () {
const analyse = new DetachedDOMElementAnalysis();
await analyse.analyzeSnapshotFromFile(`./s2.heapsnapshot`); // just return null
})();
Is it possible to add return value to heap-analysis plugins so that we can to access it’s result in the code?
(async function () {
const analyse = new DetachedDOMElementAnalysis();
// get the detachedeElements
const result = await analyse.analyzeSnapshotFromFile(`./s2.heapsnapshot`);
})();
Issue Analytics
- State:
- Created 10 months ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
That makes sense. We can add the option that supports setting an optional working directory for heap analysis calls
@MoonShadowV I added support for setting optional working directory. Let me know if the new API is flexible for your use case.