Skip to main content

Package: @memlab/api

Classes

Type Aliases

RunOptions: Object

Options for configuring browser interaction run, all fields are optional

NameTypeDescription
cookiesFile?stringthe absolute path of cookies file
evalInBrowserAfterInitLoad?AnyFunctionfunction to be evaluated in browser context after the web page initial load
scenario?IScenariotest scenario specifying how to interact with browser (for more details view IScenario)
snapshotForEachStep?booleanif true, take heap snapshot for each interaction step, by default this is false, which means memlab will decide which steps it will take heap snapshots
webWorker?Optional<string>if this field is provided, it specifies the web worker as the target for heap analysis. For example {webWorker: null} means analyzing the heap of the first web worker found. {webWorker: 'workerTitle'} means analyzing the heap of the web worker with name: 'workerTitle'.
workDir?stringspecify the working directory where you want memlab to dump heap snapshots and other meta data of the test run. If no working directory is provided, memlab will generate a random temp directory under the operating system's default directory for temporary files. Note: It's the caller's responsibility to make sure the specified working directory exists.

RunResult: Object

A data structure holding the result of the run API call.

NameTypeDescription
leaksISerializedInfo[]leak traces detected and clustered from the browser interaction
runResultBrowserInteractionResultReadera utility for reading browser interaction results from disk

Functions

analyze(runResult, heapAnalyzer, args?)

This API analyzes heap snapshot(s) with a specified heap analysis. This is equivalent to memlab analyze in CLI.

  • Parameters:
    • runResult: BrowserInteractionResultReader | return value of a browser interaction run
    • heapAnalyzer: BaseAnalysis | instance of a heap analysis
    • args: ParsedArgs | other CLI arguments that needs to be passed to the heap analysis
  • Returns: Promise<void> | each analysis may have a different return type, please check out the type definition or the documentation for the process method of the analysis class you are using for heapAnalyzer.
  • Examples:
const {analyze, takeSnapshots, StringAnalysis} = require('@memlab/api');

(async function () {
const scenario = {
url: () => 'https://www.facebook.com',
};
const result = await takeSnapshots({scenario});
const analysis = new StringAnalysis();
await analyze(result, analysis);
})();

findLeaks(runResult)

This API finds memory leaks by analyzing heap snapshot(s). This is equivalent to memlab find-leaks in CLI.

  • Parameters:
  • Returns: Promise<ISerializedInfo[]> | leak traces detected and clustered from the browser interaction
  • Examples:
const {findLeaks, takeSnapshots} = require('@memlab/api');

(async function () {
const scenario = {
url: () => 'https://www.facebook.com',
};
const result = await takeSnapshots({scenario});
const leaks = findLeaks(result);
})();

findLeaksBySnapshotFilePaths(baselineSnapshot, targetSnapshot, finalSnapshot, options?)

This API finds memory leaks by analyzing specified heap snapshots. This is equivalent to memlab find-leaks with the --baseline, --target, and --final flags in CLI.

  • Parameters:

    • baselineSnapshot: string | the file path of the baseline heap snapshot
    • targetSnapshot: string | the file path of the target heap snapshot
    • finalSnapshot: string | the file path of the final heap snapshot
    • options: Object | optionally, you can specify a working directory (other than the default one) for heap analysis
    • options.workDir?: string
  • Returns: Promise<ISerializedInfo[]> | leak traces detected and clustered from the browser interaction

  • Source:


run(runOptions?)

This API runs browser interaction and find memory leaks triggered in browser This is equivalent to running memlab run in CLI. This is also equivalent to warm up, and call takeSnapshots and findLeaks.

  • Parameters:
    • runOptions: RunOptions | configure browser interaction run
  • Returns: Promise<RunResult> | memory leaks detected and a utility reading browser interaction results from disk
  • Examples:
const {run} = require('@memlab/api');

(async function () {
const scenario = {
url: () => 'https://www.facebook.com',
};
const {leaks} = await run({scenario});
})();

takeSnapshots(options?)

This API runs E2E interaction and takes heap snapshots. This is equivalent to running memlab snapshot in CLI.

const {takeSnapshots} = require('@memlab/api');

(async function () {
const scenario = {
url: () => 'https://www.facebook.com',
};
const result = await takeSnapshots({scenario});
})();

warmupAndTakeSnapshots(options?)

This API warms up web server, runs E2E interaction, and takes heap snapshots. This is equivalent to running memlab warmup-and-snapshot in CLI. This is also equivalent to warm up and call takeSnapshots.

const {warmupAndTakeSnapshots} = require('@memlab/api');

(async function () {
const scenario = {
url: () => 'https://www.facebook.com',
};
const result = await warmupAndTakeSnapshots({scenario});
})();
  • Classes
  • Type Aliases
    • RunOptions: Object
    • RunResult: Object
  • Functions
    • analyze(runResult, heapAnalyzer, args?)
    • findLeaks(runResult)
    • findLeaksBySnapshotFilePaths(baselineSnapshot, targetSnapshot, finalSnapshot, options?)
    • run(runOptions?)
    • takeSnapshots(options?)
    • warmupAndTakeSnapshots(options?)