设置持续测试
使用 memlab 设置持续测试服务以查找 Web 内存泄漏包括以下几个部分
- 准备 测试场景,涵盖被测 Web 应用的关键交互。
- 通过 bash 中的 memlab 命令行界面 或通过 memlab API 触发测试运行。
- 收集转储在磁盘中的内存泄漏结果。
编写测试场景
按照本教程中的说明进行操作。
运行测试
在 CLI 中运行:您的测试作业可以通过 memlab 命令行界面运行测试场景,例如
memlab run --scenario /path/to/test/scenario/file.js \
--work-dir /path/to/save/memlab/run/results/
使用 --work-dir
选项指定一个目录,您希望 memlab 在其中转储浏览器交互元数据(例如,屏幕截图和堆快照)。
在 Node.js 中运行:或者,如果您的测试作业是用 node.js 编写的,您可以调用 memlab API
const {run} = require('@memlab/api');
const scenario = require('/path/to/test/scenario/file.js');
const fs = require('fs-extra');
(async function () {
const workDir = '/path/to/save/memlab/run/results/';
// make sure the working directory exists
fs.mkdirsSync(workDir);
const result = await run({scenario, workDir});
})();
收集结果
memlab 运行完成后,所有结果和元数据将保存在指定的工作目录中,其中包括以下子目录和文件
/path/to/save/memlab/run/results/
├── data
│ ├── cur
│ │ ├── browser-info.txt # browser web console output
│ │ ├── console-log.txt # memlab terminal logging
│ │ ├── leaks.txt # text summary of clustered memory leaks
│ │ ├── run-meta.json # meta data of memlab run and browser configuration
│ │ ├── s1.heapsnapshot # heap snapshot after the url callback (initial page load)
│ │ ├── s2.heapsnapshot # heap snapshot after the action callback (after target interaction)
│ │ ├── s3.heapsnapshot # heap snapshot after the back callback (after reverting target interaction)
│ │ ├── screenshot-1.png # web page screenshot after the url callback (initial page load)
│ │ ├── screenshot-2.png # web page screenshot after the action callback (after target interaction)
│ │ ├── screenshot-3.png # web page screenshot after the back callback (after reverting target interaction)
│ │ └── snap-seq.json # meta data about each browser interaction step
│ ├── logger
│ │ ├── trace-clusters
│ │ │ ├── @1846905.json # meta data for one of memory leak trace cluster
│ │ │ ...
│ │ └── trace-jsons
│ │ ├── @1846905.json # detailed JSON trace of a representative memory leak trace
│ │ ... # of a leak trace cluster, this can be used for trace visualization
...
要读取结果,请使用内置结果读取器,或者如果测试作业在不同的运行时上运行,则编写您自己的脚本或库。
const {BrowserInteractionResultReader} = require('@memlab/api');
const workDir = '/path/to/save/memlab/run/results/';
const result = BrowserInteractionResultReader.from(workDir);
// get absolute paths of all snapshot files
const files = result.getSnapshotFiles();
// print all browser web console output
const metaInfo = result.getRunMetaInfo();
console.log(metaInfo.browserInfo._consoleMessages.join('\n'));
// clean up the results
result.cleanup();
有关内置结果读取器的更多 API,请点击此处。