跳到主要内容

类: SnapshotResultReader

一个实用工具实体,用于读取从基线、目标和最终堆快照生成的所有 MemLab 文件。

这个类最有用的特性是,当你有三个独立的快照(基线、目标和最终),它们不是来自 MemLab,但你仍然想使用 findLeaks 来检测内存泄漏

const {SnapshotResultReader, findLeaks} = require('@memlab/api');

// baseline, target, and final are file paths of heap snapshot files
const reader = SnapshotResultReader.fromSnapshots(baseline, target, final);
const leaks = await findLeaks(reader);

层级结构

  • default

    SnapshotResultReader

方法

getConsoleBackupFile()

此方法获取控制台输出的备份文件。

memlab CLI 命令(例如,memlab find-leaks)输出非结构化的字符串表示形式,便于阅读,而 API(例如,findLeaks) 返回结构化的泄漏表示形式,便于后处理。 如果您需要从 CLI 输出备份文件中读取当前工作目录中的所有字符串输出,您可以通过此方法返回的 CLI 输出备份文件读取它们。

  • 返回值: string | 备份文件的绝对路径
  • 示例:
const {takeSnapshots, findLeaks} = require('@memlab/api');

(async function () {
const scenario = { url: () => 'https://npmjs.net.cn'};
const result = await takeSnapshots({scenario});
const leaks = await findLeaks(result);

// get the console output backup file
const consoleBackupFile = result.getConsoleBackupFile();
})();

getInteractionSteps()

浏览器交互步骤序列

  • 返回值: E2EStepInfo[] | 浏览器交互步骤信息数组

  • 示例:

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

// baseline, target, and final are file paths of heap snapshot files
const reader = SnapshotResultReader.fromSnapshots(baseline, target, final);
const paths = reader.getInteractionSteps();

getRootDirectory()

获取存储 memlab 运行的数据和生成文件的目录

  • 返回值: string | 目录的绝对路径
  • 示例:
const {takeSnapshots} = require('@memlab/api');

(async function () {
const scenario = { url: () => 'https://npmjs.net.cn'};
const result = await takeSnapshots({scenario});

// get the directory that stores all the files
// generated from the takeSnapshots call
const dataDir = result.getRootDirectory();
})();

getSnapshotFiles()

获取与此 SnapshotResultReader 相关的所有快照文件

  • 返回值: string[] | 快照文件绝对路径的数组

  • 示例:

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

// baseline, target, and final are file paths of heap snapshot files
const reader = SnapshotResultReader.fromSnapshots(baseline, target, final);
const paths = reader.getSnapshotFiles();

Static fromSnapshots(baselineSnapshot, targetSnapshot, finalSnapshot)

从基线、目标和最终堆快照文件构建结果读取器。 这三个快照文件不必在同一目录中。

  • 参数:

    • baselineSnapshot: string | 基线堆快照的文件路径
    • targetSnapshot: string | 目标堆快照的文件路径
    • finalSnapshot: string | 最终堆快照的文件路径
  • 返回值: SnapshotResultReader | ResultReader 实例

  • 示例:

const {SnapshotResultReader, findLeaks} = require('@memlab/api');

// baseline, target, and final are file paths of heap snapshot files
const reader = SnapshotResultReader.fromSnapshots(baseline, target, final);
const leaks = await findLeaks(reader);
  • 层级结构
  • 方法
    • getConsoleBackupFile()
    • getInteractionSteps()
    • getRootDirectory()
    • getSnapshotFiles()
    • Static fromSnapshots(baselineSnapshot, targetSnapshot, finalSnapshot)