-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.ts
More file actions
233 lines (197 loc) · 6.84 KB
/
analyzer.ts
File metadata and controls
233 lines (197 loc) · 6.84 KB
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
import type { FileStats, Statistics, TraceEvent } from "./types.ts";
/**
* Analyze trace data and generate statistics
*/
export function analyzeTrace(events: TraceEvent[]): Statistics {
// Initial statistics structure
const stats: Statistics = {
totalTime: 0,
totalFiles: 0,
operationTimes: {},
categoryTimes: {},
slowestFiles: [],
filesByType: {},
moduleResolution: {
totalTime: 0,
totalCount: 0,
averageTime: 0,
},
};
// Track begin events by their name and file path for matching with end events
const beginEvents: Record<string, Record<string, TraceEvent>> = {};
// Track all unique files
const uniqueFiles = new Set<string>();
// Track time spent on each file
const fileTimings: Record<string, FileStats> = {};
// Process events
for (const event of events) {
// Skip metadata events
if (event.ph === "M") continue;
// Handle complete events (ph="X") which include duration
if (event.ph === "X" && event.dur !== undefined) {
// Get operation name
const operation = event.name;
// Increment operation stats
if (!stats.operationTimes[operation]) {
stats.operationTimes[operation] = {
totalTime: 0,
count: 0,
averageTime: 0,
};
}
stats.operationTimes[operation].totalTime += event.dur;
stats.operationTimes[operation].count++;
// Increment category stats
if (!stats.categoryTimes[event.cat]) {
stats.categoryTimes[event.cat] = {
totalTime: 0,
count: 0,
averageTime: 0,
};
}
stats.categoryTimes[event.cat].totalTime += event.dur;
stats.categoryTimes[event.cat].count++;
// Track module resolution specifically
if (operation === "resolveModuleNamesWorker") {
stats.moduleResolution.totalTime += event.dur;
stats.moduleResolution.totalCount++;
}
// Track file specific timing
const filePath = getFilePath(event);
if (filePath) {
uniqueFiles.add(filePath);
if (!fileTimings[filePath]) {
fileTimings[filePath] = {
path: filePath,
totalTime: 0,
operations: {},
};
}
fileTimings[filePath].totalTime += event.dur;
if (!fileTimings[filePath].operations[operation]) {
fileTimings[filePath].operations[operation] = 0;
}
fileTimings[filePath].operations[operation] += event.dur;
// Track file types
const ext = getFileExtension(filePath);
if (!stats.filesByType[ext]) {
stats.filesByType[ext] = 0;
}
stats.filesByType[ext]++;
}
} // Handle begin events (ph="B")
else if (event.ph === "B") {
const filePath = getFilePath(event);
const key = `${event.name}:${filePath || "unknown"}`;
if (!beginEvents[key]) {
beginEvents[key] = {};
}
// Use the timestamp as a unique identifier for this specific begin event
const timeKey = event.ts.toString();
beginEvents[key][timeKey] = event;
} // Handle end events (ph="E")
else if (event.ph === "E") {
const filePath = getFilePath(event);
const key = `${event.name}:${filePath || "unknown"}`;
// Find the matching begin event
if (beginEvents[key]) {
// Get the most recent begin event
const timeKeys = Object.keys(beginEvents[key]).sort();
if (timeKeys.length > 0) {
const timeKey = timeKeys[0]; // Get the earliest begin event
const beginEvent = beginEvents[key][timeKey];
// Calculate duration
const duration = event.ts - beginEvent.ts;
// Update operation stats
const operation = event.name;
if (!stats.operationTimes[operation]) {
stats.operationTimes[operation] = {
totalTime: 0,
count: 0,
averageTime: 0,
};
}
stats.operationTimes[operation].totalTime += duration;
stats.operationTimes[operation].count++;
// Update category stats
if (!stats.categoryTimes[event.cat]) {
stats.categoryTimes[event.cat] = {
totalTime: 0,
count: 0,
averageTime: 0,
};
}
stats.categoryTimes[event.cat].totalTime += duration;
stats.categoryTimes[event.cat].count++;
// Track file specific timing
if (filePath) {
uniqueFiles.add(filePath);
if (!fileTimings[filePath]) {
fileTimings[filePath] = {
path: filePath,
totalTime: 0,
operations: {},
};
}
fileTimings[filePath].totalTime += duration;
if (!fileTimings[filePath].operations[operation]) {
fileTimings[filePath].operations[operation] = 0;
}
fileTimings[filePath].operations[operation] += duration;
// Track file types
const ext = getFileExtension(filePath);
if (!stats.filesByType[ext]) {
stats.filesByType[ext] = 0;
}
stats.filesByType[ext]++;
}
// Remove the begin event so we don't match it again
delete beginEvents[key][timeKey];
}
}
}
}
// Calculate average time for all operations
for (const op in stats.operationTimes) {
const operation = stats.operationTimes[op];
operation.averageTime = operation.totalTime / operation.count;
}
// Calculate average time for all categories
for (const cat in stats.categoryTimes) {
const category = stats.categoryTimes[cat];
category.averageTime = category.totalTime / category.count;
}
// Calculate average time for module resolution
if (stats.moduleResolution.totalCount > 0) {
stats.moduleResolution.averageTime = stats.moduleResolution.totalTime /
stats.moduleResolution.totalCount;
}
// Determine total build time (max timestamp - min timestamp)
const timestamps = events
.filter((e) => e.ph !== "M") // Skip metadata events
.map((e) => e.ph === "E" ? e.ts : e.ts + (e.dur || 0));
if (timestamps.length > 0) {
stats.totalTime = Math.max(...timestamps) -
Math.min(...events.map((e) => e.ts));
}
// Set total files count
stats.totalFiles = uniqueFiles.size;
// Get the slowest files (top 10)
stats.slowestFiles = Object.values(fileTimings)
.sort((a, b) => b.totalTime - a.totalTime)
.slice(0, 10);
return stats;
}
/**
* Extract file extension from a file path
*/
function getFileExtension(filePath: string): string {
const match = filePath.match(/\.([^\.]+)$/);
return match ? match[1] : "unknown";
}
/**
* Get the file path from a trace event
*/
function getFilePath(event: TraceEvent): string | null {
return event.args.path || event.args.fileName || null;
}