-
Notifications
You must be signed in to change notification settings - Fork 113
/
BrowserInteractionResultReader.ts
137 lines (130 loc) · 4.2 KB
/
BrowserInteractionResultReader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall web_perf_infra
*/
import type {E2EStepInfo, RunMetaInfo} from '@memlab/core';
import fs from 'fs-extra';
import path from 'path';
import {runInfoUtils, utils} from '@memlab/core';
import BaseResultReader from './BaseResultReader';
/**
* A utility entity to read all generated files from
* the directory holding the data and results from the
* last MemLab browser interaction run
*/
export default class BrowserInteractionResultReader extends BaseResultReader {
/**
* build a result reader from a data directory where the data
* and generated files of a memlab run were stored
* @param workDir absolute path of the data directory
* @returns the ResultReader instance
*
* * **Examples**:
* ```javascript
* const {BrowserInteractionResultReader} = require('@memlab/api');
*
* const dataDir = '/tmp/memlab'; // where the last memlab run stores results
* const reader = BrowserInteractionResultReader.from(dataDir);
* reader.cleanup(); // clean up the results
* ```
*/
static from(workDir = ''): BrowserInteractionResultReader {
return new BrowserInteractionResultReader(workDir);
}
/**
* get all snapshot files generated from last memlab browser interaction
* @returns an array of snapshot file's absolute path
* * **Examples**:
* ```javascript
* const {takeSnapshots} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
*
* // get absolute paths of all snapshot files
* const files = result.getSnapshotFiles();
* })();
* ```
*/
public getSnapshotFiles(): string[] {
this.check();
const dataDir = this.fileManager.getCurDataDir({workDir: this.workDir});
return fs
.readdirSync(dataDir)
.filter(file => file.endsWith('heapsnapshot'))
.map(file => path.join(dataDir, file));
}
/**
* get the directory holding all snapshot files
* @returns the absolute path of the directory
* * **Examples**:
* ```javascript
* const {takeSnapshots} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
*
* // get the absolute path the directory holding all snapshot files
* const files = result.getSnapshotFileDir();
* })();
* ```
*/
public getSnapshotFileDir(): string {
this.check();
return this.fileManager.getCurDataDir({workDir: this.workDir});
}
/**
* browser interaction step sequence
* @returns an array of browser interaction step information
* * **Examples**:
* ```javascript
* const {takeSnapshots} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
*
* const steps = result.getInteractionSteps();
* // print each browser interaction's name and JavaScript heap size (in bytes)
* steps.forEach(step => console.log(step.name, step.JSHeapUsedSize))
* })();
* ```
*/
public getInteractionSteps(): E2EStepInfo[] {
this.check();
const metaFile = this.fileManager.getSnapshotSequenceMetaFile({
workDir: this.workDir,
});
return utils.loadTabsOrder(metaFile);
}
/**
* general meta data of the browser interaction run
* @returns meta data about the entire browser interaction
* * **Examples**:
* ```javascript
* const {takeSnapshots} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
*
* const metaInfo = result.getRunMetaInfo();
* // print all browser web console output
* console.log(metaInfo.browserInfo._consoleMessages.join('\n'));
* })();
* ```
*/
public getRunMetaInfo(): RunMetaInfo {
this.check();
return runInfoUtils.runMetaInfoManager.loadRunMetaInfo({
workDir: this.workDir,
});
}
}