Skip to main content

Package: @memlab/api

Enumerations

Classes

Type Aliases

RunOptions: Object

Options for configuring browser interaction run, all fields are optional

NameTypeDescription
chromiumBinary?stringif not specified, memlab will use the Chromium binary installed by Puppeteer. Use this option to specify a different binary if Puppeteer does not install the Chromium binary correctly (e.g., in a environtment Docker) or when you may want to use a different version of Chromium binary.
consoleMode?ConsoleModespecifying the terminal output mode, default is default. For more details. please check out ConsoleMode
cookiesFile?stringthe absolute path of cookies file
evalInBrowserAfterInitLoad?AnyFunctionfunction to be evaluated in browser context after the web page initial load. Note that this function is defined in node.js context but it will be evaluated in browser context so the function should not use any closure variables outside of the browser context.
scenario?IScenariotest scenario specifying how to interact with browser (for more details view IScenario)
skipWarmup?booleanskip the initial page loading warmup for the web application being tested
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: default | 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, options?)

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

  • Parameters:
    • runResult: default | return value of a browser interaction run
    • options: Object | configure memory leak detection run
    • options.consoleMode?: ConsoleMode | specify the terminal output mode (see ConsoleMode)
  • 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, consoleMode: 'SILENT'});
const leaks = findLeaks(result, {consoleMode: 'CONTINUOUS_TEST'});
})();

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 mode for heap analysis
    • options.consoleMode?: ConsoleMode | specify the terminal output mode (see ConsoleMode)
    • options.workDir?: string | specify a working directory (other than the default one)
  • Returns: Promise<ISerializedInfo[]> | leak traces detected and clustered from the browser interaction

  • Source:


run(options?)

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:
  • 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});
})();
  • Enumerations
  • Classes
  • Type Aliases
    • RunOptions: Object
    • RunResult: Object
  • Functions
    • analyze(runResult, heapAnalyzer, args?)
    • findLeaks(runResult, options?)
    • findLeaksBySnapshotFilePaths(baselineSnapshot, targetSnapshot, finalSnapshot, options?)
    • run(options?)
    • takeSnapshots(options?)
    • warmupAndTakeSnapshots(options?)