-
Notifications
You must be signed in to change notification settings - Fork 27k
/
trace-to-tree.mjs
245 lines (230 loc) · 8.05 KB
/
trace-to-tree.mjs
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
234
235
236
237
238
239
240
241
242
243
244
245
import fs from 'fs'
import eventStream from 'event-stream'
import {
bold,
blue,
cyan,
green,
magenta,
red,
yellow,
} from '../packages/next/dist/lib/picocolors.js'
const file = fs.createReadStream(process.argv[2])
const sum = (...args) => args.reduce((a, b) => a + b, 0)
const aggregate = (event) => {
const isBuildModule = event.name.startsWith('build-module-')
event.range = event.timestamp + (event.duration || 0)
event.total = isBuildModule ? event.duration : 0
if (isBuildModule) {
event.packageName = getPackageName(event.tags.name)
if (event.children) {
const queue = [...event.children]
event.children = []
event.childrenTimings = {}
event.mergedChildren = 0
for (const e of queue) {
if (!e.name.startsWith('build-module-')) {
event.childrenTimings[e.name] =
(event.childrenTimings[e.name] || 0) + e.duration
continue
}
const pkgName = getPackageName(e.tags.name)
if (!event.packageName || pkgName !== event.packageName) {
event.children.push(e)
} else {
event.duration += e.duration
event.mergedChildren++
if (e.children) queue.push(...e.children)
}
}
}
}
if (event.children) {
event.children.forEach(aggregate)
event.children.sort((a, b) => a.timestamp - b.timestamp)
event.range = Math.max(
event.range,
...event.children.map((c) => c.range || event.timestamp)
)
event.total += isBuildModule
? sum(...event.children.map((c) => c.total || 0))
: 0
}
}
const formatDuration = (duration, isBold) => {
const color = isBold ? bold : (x) => x
if (duration < 1000) {
return color(`${duration} µs`)
} else if (duration < 10000) {
return color(`${Math.round(duration / 100) / 10} ms`)
} else if (duration < 100000) {
return color(`${Math.round(duration / 1000)} ms`)
} else if (duration < 1_000_000) {
return color(cyan(`${Math.round(duration / 1000)} ms`))
} else if (duration < 10_000_000) {
return color(green(`${Math.round(duration / 100000) / 10} s`))
} else if (duration < 20_000_000) {
return color(yellow(`${Math.round(duration / 1000000)} s`))
} else if (duration < 100_000_000) {
return color(red(`${Math.round(duration / 1000000)} s`))
} else {
return color('🔥' + red(`${Math.round(duration / 1000000)} s`))
}
}
const formatTimes = (event) => {
const range = event.range - event.timestamp
const additionalInfo = []
if (event.total && event.total !== range)
additionalInfo.push(`total ${formatDuration(event.total)}`)
if (event.duration !== range)
additionalInfo.push(`self ${formatDuration(event.duration, bold)}`)
return `${formatDuration(range, additionalInfo.length === 0)}${
additionalInfo.length ? ` (${additionalInfo.join(', ')})` : ''
}`
}
const formatFilename = (filename) => {
return cleanFilename(filename).replace(/.+[\\/]node_modules[\\/]/, '')
}
const cleanFilename = (filename) => {
if (filename.includes('&absolutePagePath=')) {
filename =
'page ' +
decodeURIComponent(
filename.replace(/.+&absolutePagePath=/, '').slice(0, -1)
)
}
filename = filename.replace(/.+!(?!$)/, '')
return filename
}
const getPackageName = (filename) => {
const match = /.+[\\/]node_modules[\\/]((?:@[^\\/]+[\\/])?[^\\/]+)/.exec(
cleanFilename(filename)
)
return match && match[1]
}
const formatEvent = (event) => {
let head
switch (event.name) {
case 'webpack-compilation':
head = `${bold(`${event.tags.name} compilation`)} ${formatTimes(event)}`
break
case 'webpack-invalidated-client':
case 'webpack-invalidated-server':
head = `${bold(`${event.name.slice(-6)} recompilation`)} ${
event.tags.trigger === 'manual'
? '(new page discovered)'
: `(${formatFilename(event.tags.trigger)})`
} ${formatTimes(event)}`
break
case 'add-entry':
head = `${blue('entry')} ${formatFilename(event.tags.request)}`
break
case 'hot-reloader':
head = `${bold(green(`hot reloader`))}`
break
case 'export-page':
head = `${event.name} ${event.tags.path} ${formatTimes(event)}`
break
default:
if (event.name.startsWith('build-module-')) {
const { mergedChildren, childrenTimings, packageName } = event
head = `${magenta('module')} ${
packageName
? `${bold(cyan(packageName))} (${formatFilename(event.tags.name)}${
mergedChildren ? ` + ${mergedChildren}` : ''
})`
: formatFilename(event.tags.name)
} ${formatTimes(event)}`
if (childrenTimings && Object.keys(childrenTimings).length) {
head += ` [${Object.keys(childrenTimings)
.map((key) => `${key} ${formatDuration(childrenTimings[key])}`)
.join(', ')}]`
}
} else {
head = `${event.name} ${formatTimes(event)}`
}
break
}
if (event.children && event.children.length) {
return head + '\n' + treeChildren(event.children.map(formatEvent))
} else {
return head
}
}
const indentWith = (str, firstLinePrefix, otherLinesPrefix) => {
return firstLinePrefix + str.replace(/\n/g, '\n' + otherLinesPrefix)
}
const treeChildren = (items) => {
let str = ''
for (let i = 0; i < items.length; i++) {
if (i !== items.length - 1) {
str += indentWith(items[i], '├─ ', '│ ') + '\n'
} else {
str += indentWith(items[i], '└─ ', ' ')
}
}
return str
}
const tracesById = new Map()
file
.pipe(eventStream.split())
.pipe(
eventStream.mapSync((data) => {
if (!data) return
const json = JSON.parse(data)
json.forEach((event) => {
tracesById.set(event.id, event)
})
})
)
.on('end', () => {
const rootEvents = []
for (const event of tracesById.values()) {
if (event.parentId) {
event.parent = tracesById.get(event.parentId)
if (event.parent) {
if (!event.parent.children) event.parent.children = []
event.parent.children.push(event)
}
}
if (!event.parent) rootEvents.push(event)
}
for (const event of rootEvents) {
aggregate(event)
}
console.log(`Explanation:
${formatEvent({
name: 'build-module-js',
tags: { name: '/Users/next-user/src/magic-ui/pages/index.js' },
duration: 163000,
timestamp: 0,
range: 24000000,
total: 33000000,
childrenTimings: { 'read-resource': 873, 'next-babel-turbo-loader': 135000 },
})}
════════╤═══════════════════════════════════ ═╤═ ═╤═ ═╤════ ═══════════╤════════════════════════════════════════
└─ name of the processed module │ │ │ └─ timings of nested steps
│ │ └─ building the module itself (including overlapping parallel actions)
│ └─ total build time of this modules and all nested ones (including overlapping parallel actions)
└─ how long until the module and all nested modules took compiling (wall time, without overlapping actions)
${formatEvent({
name: 'build-module-js',
tags: {
name: '/Users/next-user/src/magic-ui/node_modules/lodash/camelCase.js',
},
packageName: 'lodash',
duration: 958000,
timestamp: 0,
range: 295000,
childrenTimings: { 'read-resource': 936000 },
mergedChildren: 281,
})}
═╤════ ══════╤════════════ ═╤═
│ │ └─ number of modules that are merged into that line
│ └─ first module that is imported
└─ npm package name
`)
for (const event of rootEvents) {
console.log(formatEvent(event))
}
})