-
Notifications
You must be signed in to change notification settings - Fork 113
/
BaseAnalysis.ts
233 lines (219 loc) · 8.17 KB
/
BaseAnalysis.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* 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 {AnyValue, BaseOption} from '@memlab/core';
import type {
AnalyzeSnapshotResult,
HeapAnalysisOptions,
RunHeapAnalysisOptions,
} from './PluginUtils';
import {config, constant, fileManager, info, runInfoUtils} from '@memlab/core';
import {defaultTestPlanner} from '@memlab/e2e';
import pluginUtils from './PluginUtils';
// Identify the target scenario and
// add its setting to Config
function loadScenarioConfig(): void {
runInfoUtils.runMetaInfoManager.setConfigFromRunMeta({silentFail: true});
if (
config.targetApp === 'external' ||
config.targetTab.startsWith(constant.namePrefixForScenarioFromFile)
) {
return;
}
const synthesizer = defaultTestPlanner.getSynthesizer({needCookies: false});
synthesizer
.getNodeNameBlocklist()
.forEach(name => config.nodeNameBlockList.add(name));
synthesizer
.getEdgeNameBlocklist()
.forEach(name => config.edgeNameBlockList.add(name));
}
abstract class Analysis {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async process(_options: HeapAnalysisOptions): Promise<void> {
const className = this.constructor.name;
throw new Error(`${className}.process is not implemented`);
}
/**
* DO NOT override this method if you are implementing your own analysis
* by extending {@link BaseAnalysis}.
* @param options This is the auto-generated arguments passed to all the
* `process` method that your self-defined heap analysis should implement.
* You are not supposed to construct instances of this class.
* @returns any type of value returned from the overridden `process` method
* of the heap analysis instance. Each heap analysis class can define
* different return value format.
* @internal
*/
public async run(
options: HeapAnalysisOptions = pluginUtils.defaultAnalysisArgs,
): Promise<void> {
loadScenarioConfig();
await this.process(options);
}
/**
* Run heap analysis for a single heap snapshot file
* @param file the absolute path of a `.heapsnapshot` file.
* @param options optional configuration for the heap analysis run
* @returns this API returns {@link AnalyzeSnapshotResult}, which contains
* the logging file of analysis console output. Alternatively, to get more
* structured analysis results, check out the documentation of the hosting
* heap analysis class and call the analysis-specific API to get results
* after calling this method.
* * **Example**:
* ```typescript
* const analysis = new StringAnalysis();
* // analysis console output is saved in result.analysisOutputFile
* const result = await analysis.analyzeSnapshotFromFile(snapshotFile);
* // query analysis-specific and structured results
* const stringPatterns = analysis.getTopDuplicatedStringsInCount();
* ```
* Additionally, you can specify a working directory to where
* the intermediate, logging, and final output files will be dumped:
* ```typescript
* const analysis = new StringAnalysis();
* // analysis console output is saved in result.analysisOutputFile
* // which is inside the specified working directory
* const result = await analysis.analyzeSnapshotFromFile(snapshotFile, {
* // if the specified directory doesn't exist, memlab will create it
* workDir: '/tmp/your/work/dir',
* });
* ```
*/
public async analyzeSnapshotFromFile(
file: string,
options: RunHeapAnalysisOptions = {},
): Promise<AnalyzeSnapshotResult> {
if (options.workDir) {
// set and init the new work dir
config.defaultFileManagerOption = options;
}
const analysisOutputFile = fileManager.initNewHeapAnalysisLogFile();
info.registerLogFile(analysisOutputFile);
await this.process({
args: {
_: [],
snapshot: file,
'snapshot-dir': '<MUST_PROVIDE_SNAPSHOT_DIR>',
},
});
info.unregisterLogFile(analysisOutputFile);
return {analysisOutputFile};
}
/**
* Run heap analysis for a series of heap snapshot files
* @param directory the absolute path of the directory holding a series of
* `.heapsnapshot` files, all snapshot files will be loaded and analyzed
* in the alphanumerically ascending order of those snapshot file names.
* @param options optional configuration for the heap analysis run
* @returns this API returns {@link AnalyzeSnapshotResult}, which contains
* the logging file of analysis console output. Alternatively, to get more
* structured analysis results, check out the documentation of the hosting
* heap analysis class and call the analysis-specific API to get results
* after calling this method.
* * **Example**:
* ```typescript
* const analysis = new ShapeUnboundGrowthAnalysis();
* // analysis console output is saved in result.analysisOutputFile
* const result = await analysis.analyzeSnapshotsInDirectory(snapshotDirectory);
* // query analysis-specific and structured results
* const shapes = analysis.getShapesWithUnboundGrowth();
* ```
* * Additionally, you can specify a working directory to where
* the intermediate, logging, and final output files will be dumped:
* ```typescript
* const analysis = new ShapeUnboundGrowthAnalysis();
* // analysis console output is saved in result.analysisOutputFile
* // which is inside the specified working directory
* const result = await analysis.analyzeSnapshotsInDirectory(snapshotDirectory, {
* // if the specified directory doesn't exist, memlab will create it
* workDir: '/tmp/your/work/dir',
* });
* ```
*/
public async analyzeSnapshotsInDirectory(
directory: string,
options: RunHeapAnalysisOptions = {},
): Promise<AnalyzeSnapshotResult> {
if (options.workDir) {
// set and init the new work dir
config.defaultFileManagerOption = options;
}
const analysisOutputFile = fileManager.initNewHeapAnalysisLogFile();
info.registerLogFile(analysisOutputFile);
await this.process({
args: {
_: [],
snapshot: '<MUST_PROVIDE_SNAPSHOT_FILE>',
'snapshot-dir': directory,
},
});
info.unregisterLogFile(analysisOutputFile);
return {analysisOutputFile};
}
}
/**
*
*/
class BaseAnalysis extends Analysis {
/**
* Get the name of the heap analysis, which is also used to reference
* the analysis in memlab command-line tool.
*
* The following terminal command will initiate with this analysis:
* `memlab analyze <ANALYSIS_NAME>`
*
* @returns the name of the analysis
* * **Examples**:
* ```typescript
* const analysis = new YourAnalysis();
* const name = analysis.getCommandName();
* ```
*/
getCommandName(): string {
const className = this.constructor.name;
throw new Error(`${className}.getCommandName is not implemented`);
}
/**
* Get the textual description of the heap analysis.
* The description of this analysis will be printed by:
* `memlab analyze list`
*
* @returns the description
*/
getDescription(): string {
const className = this.constructor.name;
throw new Error(`${className}.getDescription is not implemented`);
}
/**
* Callback for `memlab analyze <command-name>`.
* Do the memory analysis and print results in this callback
* The analysis should support:
* 1) printing results on screen
* 2) returning results via the return value
* @param options This is the auto-generated arguments passed to all the
* `process` method that your self-defined heap analysis should implement.
* You are not supposed to construct instances of this class.
*/
public async process(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options: HeapAnalysisOptions,
): Promise<AnyValue> {
const className = this.constructor.name;
throw new Error(`${className}.process is not implemented`);
}
/**
* override this method if you would like CLI to print the option info
* @returns an array of command line options
*/
getOptions(): BaseOption[] {
return [];
}
}
export default BaseAnalysis;