-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-timings-to-trace.mjs
219 lines (194 loc) · 6.11 KB
/
user-timings-to-trace.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
// Take some user timings (performance.measure/mark) and generate a trace for visualization.
// Perfect if you instrment in Node.js with performance mark()/measure(), or the NPM marky package.
// Run like:
// node user-timings-to-trace.mjs user-timings.json
//
// Most of this file is from Lighthouse: https://github.com/GoogleChrome/lighthouse/blob/0da3e1d85d1920e3e75e423e6f905ddf4bd8fd53/core/lib/timing-trace-saver.js
// But I've adapted it to be solo and modernized it a tad. ~Paul. 2024-10
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'fs';
import path from 'path';
/** @typedef {import('./types/chromium-trace').TraceEvent} TraceEvent */
/**
* Generates a Chrome trace file from user timing measures
*
* Originally adapted from https://github.com/tdresser/performance-observer-tracing
*
* @param {PerformanceEntryList} entries user timing entries
* @param {number=} threadId Can be provided to separate a series of trace events into another thread, useful if timings do not share the same timeOrigin, but should both be "left-aligned".
*/
export function generateTraceEvents(entries, threadId = 8) {
entries.sort((a, b) => a.startTime - b.startTime);
/** @type {TraceEvent[]} */
const currentTrace = [];
const baseEvt = {
pid: 7,
tid: threadId,
args: {},
// Hack: Use one to avoid some mishandling in the devtools of valid trace events
// with a ts of 0. We should fix the bug there, but making this a microsecond off
// seems an okay tradeoff.
ts: 1,
name: '',
};
const frameData = {
processId: baseEvt.pid,
frame: '_frameid_',
name: '_frame_name_',
url: '',
navigationId: '_navid_',
};
function addBaselineTraceEvents() {
/** @type {TraceEvent} */
const metaEvtBase = {
...baseEvt,
cat: '__metadata',
ph: 'M',
};
currentTrace.push({
...metaEvtBase,
name: 'process_labels',
args: {labels: 'User Timing'},
});
currentTrace.push({
...metaEvtBase,
name: 'thread_name',
args: {
name: 'CrRendererMain',
},
});
currentTrace.push({
...metaEvtBase,
name: 'process_name',
args: {
name: 'Renderer',
},
});
threadId === 8 &&
currentTrace.push({
...metaEvtBase,
cat: 'disabled-by-default-devtools.timeline',
name: 'TracingStartedInBrowser',
ph: 'I',
// s: 't',
args: {
data: {
frameTreeNodeId: 1,
persistentIds: true,
frames: [frameData],
},
},
});
threadId === 8 &&
currentTrace.push({
...metaEvtBase,
cat: 'disabled-by-default-devtools.timeline',
name: 'FrameCommittedInBrowser',
ph: 'I',
args: {
data: frameData,
},
});
}
addBaselineTraceEvents();
entries.forEach((entry, i) => {
if (entry.entryType === 'mark') {
const markEvt = {
...baseEvt,
name: entry.name,
cat: 'blink.user_timing',
ts: entry.startTime * 1000,
ph: 'I',
};
return currentTrace.push(markEvt);
}
/** @type {TraceEvent} */
const measureBeginEvt = {
...baseEvt,
// FYI Colons in user_timing names get special handling in about:tracing you may not want. https://github.com/catapult-project/catapult/blob/b026043a43f9ce3f37c1cd57269f92cb8bee756c/tracing/tracing/extras/importer/trace_event_importer.html#L1643-L1654
// But no adjustments made here.
name: entry.name,
cat: 'blink.user_timing',
ts: entry.startTime * 1000,
id2: {local: '0x' + (i + 1).toString(16)},
ph: 'b',
};
const measureEndEvt = {
...measureBeginEvt,
ph: 'e',
ts: measureBeginEvt.ts + entry.duration * 1000,
};
currentTrace.push(measureBeginEvt);
currentTrace.push(measureEndEvt);
});
// DevTools likes to calculate trace bounds with 'real' events.
// We'll give it a little breathing room for more enjoyable UI.
const firstTs = (entries.at(0)?.startTime ?? 0) * 1000;
const lastTs = currentTrace.at(-1)?.ts ?? currentTrace.reduce((acc, e) => (e.ts + (e.dur ?? 0) > acc ? e.ts + (e.dur ?? 0) : acc), 0);
const finalTs = 2.1 * lastTs - firstTs;
const zeroEvent = {
...baseEvt,
name: 'RunTask',
cat: 'disabled-by-default-devtools.timeline',
ph: 'X',
ts: firstTs * 0.9,
dur: 2,
};
const finalEvent = {
...zeroEvent,
ts: finalTs,
};
currentTrace.push(zeroEvent);
currentTrace.push(finalEvent);
return currentTrace;
}
/**
* Writes a trace file to disk
* @param {PerformanceEntryList} entries
* @return {string}
*/
export function createTraceString(entries) {
if (!Array.isArray(entries) || !entries[0].entryType) {
throw new Error('This doesnt look like measures/marks');
}
const traceEvents = generateTraceEvents(entries);
const jsonStr = `{"traceEvents":[
${traceEvents.map(evt => JSON.stringify(evt)).join(',\n')}
]}`;
return jsonStr;
}
// CLI direct invocation?
if (import.meta.url.endsWith(process.argv[1])) {
cli();
}
async function cli() {
const filename = process.argv[2] && path.resolve(process.cwd(), process.argv[2]);
if (!filename || !fs.existsSync(filename)) {
throw new Error(`File not found: ${filename}`);
}
const mark = performance.mark.bind(performance);
mark('1');
const entries = JSON.parse(fs.readFileSync(filename, 'utf8'));
mark('2');
const jsonStr = createTraceString(entries);
mark('3');
const pathObj = path.parse(filename);
const traceFilePath = path.join(pathObj.dir, `${pathObj.name}.trace.json`);
fs.writeFileSync(traceFilePath, jsonStr, 'utf8');
mark('4');
if (process.env.TIMING) {
performance.measure('all', '1', '4');
performance.measure('read json', '1', '2');
performance.measure('craft trace', '2', '3');
performance.measure('write file', '3', '4');
fs.writeFileSync('./selftimings.json', JSON.stringify(performance.getEntries()), 'utf8');
}
console.log(`
> Timing trace saved to: ${traceFilePath}
> View in DevTools perf panel, https://ui.perfetto.dev or https://trace.cafe
`);
}