-
Notifications
You must be signed in to change notification settings - Fork 113
/
BaseResultReader.ts
129 lines (121 loc) · 3.59 KB
/
BaseResultReader.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
/**
* 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 {utils, FileManager} from '@memlab/core';
import fs from 'fs-extra';
/**
* A utility entity to read all generated files from
* the directory holding the data and results from
* a memlab run
*/
export default class BaseResultReader {
protected workDir: string;
protected fileManager: FileManager;
private isValid: boolean;
/**
* build a result reader
* @param workDir absolute path of the directory where the data
* and generated files of the memlab run were stored
*/
protected constructor(workDir = '') {
this.fileManager = new FileManager();
if (workDir === '') {
workDir = this.fileManager.getWorkDir();
}
this.workDir = workDir;
this.check();
}
protected check(): void {
this.isValid = fs.existsSync(this.workDir);
if (!this.isValid) {
utils.haltOrThrow(`invalid/removed data directory: ${this.workDir}`);
}
}
/**
* internal
* @param workDir
* @returns
*/
static from(workDir = ''): BaseResultReader {
return new BaseResultReader(workDir);
}
/**
* get the directory where the data and generated files of
* the memlab run were stored
* @returns 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 directory that stores all the files
* // generated from the takeSnapshots call
* const dataDir = result.getRootDirectory();
* })();
* ```
*/
public getRootDirectory(): string {
this.check();
return this.workDir;
}
/**
* This method gets the backup file of the console output.
*
* The memlab CLI commands (e.g., `memlab find-leaks`) outputs a
* non-structured string representation for easy reading, while the
* APIs (e.g., <code>{@link findLeaks}</code>) return structured leaks
* representation that is handy for post-processing. If you need to
* obtain all the string output from the CLI in the current working directory,
* you can read them from the CLI output backup file returned by this method.
*
* @returns the absolute path of the backup file
* * **Examples**:
* ```javascript
* const {takeSnapshots, findLeaks} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
* const leaks = await findLeaks(result);
*
* // get the console output backup file
* const consoleBackupFile = result.getConsoleBackupFile();
* })();
* ```
*/
public getConsoleBackupFile(): string {
return this.fileManager.getConsoleBackupFile({workDir: this.workDir});
}
/**
* clean up data/files generated from the memlab browser interaction run
* @returns no return value
* * **Examples**:
* ```javascript
* const {takeSnapshots} = require('@memlab/api');
*
* (async function () {
* const scenario = { url: () => 'https://www.npmjs.com'};
* const result = await takeSnapshots({scenario});
*
* // delete all data/files generated by takeSnapshots
* result.cleanup();
* })();
* ```
*/
public cleanup(): void {
if (!this.isValid) {
return;
}
fs.removeSync(this.workDir);
this.isValid = false;
}
}