-
Notifications
You must be signed in to change notification settings - Fork 31
/
list-functions.ts
64 lines (55 loc) · 1.68 KB
/
list-functions.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
import { Command } from '@oclif/command';
import * as fs from 'fs-extra';
import { loadTrace } from 'tracerbench';
import { locations, traceJSONOutput } from '../helpers/flags';
import { normalizeFnName } from '../helpers/utils';
export default class ListFunctions extends Command {
public static description =
'Lists all the functions and source locations from a trace.';
public static flags = {
traceJSONOutput: traceJSONOutput({ required: true }),
locations: locations(),
};
public async run() {
let events: any = '';
let trace: any = null;
let profile: any = null;
const { flags } = this.parse(ListFunctions);
const { traceJSONOutput, locations } = flags;
const methods = new Set();
try {
events = JSON.parse(fs.readFileSync(traceJSONOutput, 'utf8'));
} catch (error) {
this.error(
`Could not extract trace events from trace file at path ${traceJSONOutput}, ${error}`
);
}
try {
trace = loadTrace(events.traceEvents);
} catch (error) {
this.error(error);
}
try {
profile = trace.cpuProfile(-1, -1);
} catch (error) {
this.error(error);
}
if (locations) {
profile.nodeMap.forEach((node: any) => {
const { functionName, url, lineNumber, columnNumber } = node.callFrame;
methods.add(
`${url}:${lineNumber}:${columnNumber}.${normalizeFnName(
functionName
)}`
);
});
} else {
profile.nodeMap.forEach((node: any) => {
methods.add(normalizeFnName(node.callFrame.functionName));
});
}
methods.forEach(method =>
this.log(`Successfully listed method: ${method}`)
);
}
}