@playcanvas/inspector 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -4
- package/package.json +1 -1
- package/src/entity-inspector-script.js +10 -0
- package/src/frame-graph-view.js +40 -9
- package/src/inspector.js +555 -24
- package/src/node-model.js +3 -2
- package/src/perf-capture.js +2034 -0
- package/src/perf-chart.js +3989 -0
- package/src/perf-copy.js +367 -0
- package/src/perf-view.js +1224 -0
- package/src/property-view.js +3 -0
- package/src/styles.js +179 -1
- package/types/entity-inspector-script.d.ts +8 -0
- package/types/frame-graph-view.d.ts +7 -0
- package/types/inspector.d.ts +199 -5
- package/types/perf-capture.d.ts +871 -0
- package/types/perf-chart.d.ts +1407 -0
- package/types/perf-copy.d.ts +61 -0
- package/types/perf-view.d.ts +231 -0
package/src/perf-view.js
ADDED
|
@@ -0,0 +1,1224 @@
|
|
|
1
|
+
import { formatBytes, makeSection } from './model.js';
|
|
2
|
+
import { SCRIPT_CALLS, SCRIPT_STRIDE } from './perf-capture.js';
|
|
3
|
+
|
|
4
|
+
/** @import { PerfCapture, PerfEvent, PerfGpuPass, PerfLoad } from './perf-capture.js' */
|
|
5
|
+
/** @import { Described } from './describe.js' */
|
|
6
|
+
/** @import { PropertyRow, PropertySection } from './model.js' */
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} PerfPart
|
|
10
|
+
* @ignore
|
|
11
|
+
* @property {string} key - A key unique among the parts.
|
|
12
|
+
* @property {string} name - The display name.
|
|
13
|
+
* @property {string} color - The CSS color it is drawn in.
|
|
14
|
+
* @property {string} channel - The channel of the capture holding it, which the charts read
|
|
15
|
+
* directly: a call per bar would box every figure it returns.
|
|
16
|
+
* @property {string} [tip] - What it covers.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {object} PerfRangeStats
|
|
21
|
+
* @ignore
|
|
22
|
+
* @property {number} count - How many frames had the figure.
|
|
23
|
+
* @property {number} avg - The mean.
|
|
24
|
+
* @property {number} p95 - The 95th percentile.
|
|
25
|
+
* @property {number} max - The largest.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @typedef {object} PerfSelection
|
|
30
|
+
* @ignore
|
|
31
|
+
* @property {PerfCapture} capture - The capture.
|
|
32
|
+
* @property {number} seq - The frame shown, or -1 for none.
|
|
33
|
+
* @property {{ from: number, to: number }|null} range - The frames selected together, inclusive,
|
|
34
|
+
* or null.
|
|
35
|
+
* @property {{ supported: boolean, enabled: boolean, enable: () => void }} gpu - The GPU profiler:
|
|
36
|
+
* whether the device has one, whether it is on, and how to turn it on.
|
|
37
|
+
* @property {{ enabled: boolean, enable: () => void, rows?: number, pinned?: ReadonlySet<string>, pin?: (name: string) => void }} [scripts] - Script
|
|
38
|
+
* timing: whether it is on and how to turn it on; how many of the longest script types the
|
|
39
|
+
* Scripts chart gives a band of their own, besides those pinned; the names of those pinned; and
|
|
40
|
+
* how to pin or unpin one.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @typedef {object} PerfScriptLegend
|
|
45
|
+
* @ignore
|
|
46
|
+
* @property {{ id: number, name: string, color: string, pinned: boolean }[]} types - The script
|
|
47
|
+
* types with a band of their own in the Scripts chart, top down: those pinned, then the longest of
|
|
48
|
+
* the others, each by its index into {@link PerfCapture#scriptNames}.
|
|
49
|
+
* @property {Map<number, string>} colors - The color of each of them, by id.
|
|
50
|
+
* @property {number} others - How many more types took time, sharing one band.
|
|
51
|
+
* @property {string} signature - The types in order, telling when the bands change.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The main-thread CPU time of a frame, part by part, bottom up as the CPU chart stacks them. They
|
|
56
|
+
* add up to the update and the render, which hold the rest.
|
|
57
|
+
*
|
|
58
|
+
* @type {PerfPart[]}
|
|
59
|
+
*/
|
|
60
|
+
const CPU_PARTS = [
|
|
61
|
+
{
|
|
62
|
+
key: 'systems',
|
|
63
|
+
name: 'Systems',
|
|
64
|
+
color: '#3d8bff',
|
|
65
|
+
tip: 'Component systems updating, with the scripts unless they are timed apart',
|
|
66
|
+
channel: 'cpuSystems'
|
|
67
|
+
},
|
|
68
|
+
{ key: 'scripts', name: 'Scripts', color: '#ffcc00', tip: 'Scripts updating and post-updating, while Script timing is on', channel: 'scripts' },
|
|
69
|
+
{ key: 'physics', name: 'Physics', color: '#ff9500', tip: 'The physics step, part of the systems update', channel: 'physics' },
|
|
70
|
+
{ key: 'anim', name: 'Animation', color: '#00c7be', tip: 'Anim components updating', channel: 'anim' },
|
|
71
|
+
{ key: 'post', name: 'Post-update', color: '#5ac8fa', tip: 'Systems post-updating, with the scripts unless they are timed apart', channel: 'cpuPost' },
|
|
72
|
+
{
|
|
73
|
+
key: 'other',
|
|
74
|
+
name: 'Other update',
|
|
75
|
+
color: '#af52de',
|
|
76
|
+
tip: 'Update event listeners of the app and input devices',
|
|
77
|
+
channel: 'cpuOther'
|
|
78
|
+
},
|
|
79
|
+
{ key: 'render', name: 'Render', color: '#4cd964', tip: 'Culling, sorting and submitting the draws of every camera', channel: 'render' },
|
|
80
|
+
{ key: 'inspector', name: 'Inspector', color: '#8e8e93', tip: 'The inspector itself, inside the update', channel: 'inspector' }
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The render phases a profiler or debug engine times, part of the render.
|
|
85
|
+
*
|
|
86
|
+
* @type {[string, string][]}
|
|
87
|
+
*/
|
|
88
|
+
const RENDER_PHASES = [
|
|
89
|
+
['batching', 'Batching'], ['cull', 'Culling'], ['sort', 'Sorting'], ['skin', 'Skinning'], ['morph', 'Morphing'],
|
|
90
|
+
['shadows', 'Shadow maps'], ['forward', 'Forward'], ['clusters', 'Light clusters']
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @typedef {object} PerfEventKind
|
|
95
|
+
* @ignore
|
|
96
|
+
* @property {string} key - A key unique among the kinds.
|
|
97
|
+
* @property {string} name - The display name.
|
|
98
|
+
* @property {string} [short] - The name beside the charts, where the display name is too long.
|
|
99
|
+
* @property {string} color - The CSS color its marks are drawn in.
|
|
100
|
+
* @property {string} tip - What it marks.
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* What the events of a frame can be, each a row of its own under the charts.
|
|
105
|
+
*
|
|
106
|
+
* @type {PerfEventKind[]}
|
|
107
|
+
*/
|
|
108
|
+
const EVENT_KINDS = [
|
|
109
|
+
{
|
|
110
|
+
key: 'hitch',
|
|
111
|
+
name: 'Hitches',
|
|
112
|
+
color: '#ff3b30',
|
|
113
|
+
tip: 'Frames that took over twice as long as the median frame, whatever held them back: what the user sees stutter. One ' +
|
|
114
|
+
'also marked Thread blocked was held back by work on the main thread, one without it by the GPU, the browser or the ' +
|
|
115
|
+
'display. Hover a hitch to see which'
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
key: 'long',
|
|
119
|
+
name: 'Main thread blocked',
|
|
120
|
+
// what fits beside the charts
|
|
121
|
+
short: 'Thread blocked',
|
|
122
|
+
color: '#ff2d92',
|
|
123
|
+
tip: 'Frames in which the browser\'s main thread was busy for 50 ms or more, with the script that ran longest, as Chromium ' +
|
|
124
|
+
'browsers report them (long animation frames). It says why a frame was slow rather than whether it was: a shorter ' +
|
|
125
|
+
'stall is not reported, and in an app slower than 50 ms a frame can be marked without being a hitch. It covers the ' +
|
|
126
|
+
'work between frames too, as while loading'
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
key: 'bake',
|
|
130
|
+
name: 'Lightmap bakes',
|
|
131
|
+
short: 'Lightmaps',
|
|
132
|
+
color: '#bf5af2',
|
|
133
|
+
tip: 'Lightmaps the app baked with the lightmapper, which blocks the frame it runs in, with how many it baked in how many passes, and the render targets and shaders it made. Reported by the profiler and debug engines'
|
|
134
|
+
},
|
|
135
|
+
{ key: 'compile', name: 'Shaders', color: '#ff9500', tip: 'Shaders compiled or created, and render pipelines created' },
|
|
136
|
+
{ key: 'gc', name: 'Heap shrank', color: '#ffcc00', tip: 'The JavaScript heap shrank: a garbage collection' },
|
|
137
|
+
{ key: 'resource', name: 'Resources', color: '#3d8bff', tip: 'Textures, render targets or buffers created or destroyed' },
|
|
138
|
+
{ key: 'marker', name: 'Measures', color: '#e5e5ea', tip: 'Spans the app measured with performance.measure' }
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
// the bit of each kind of event in a frame's mask
|
|
142
|
+
const EVENT_BITS = Object.fromEntries(EVENT_KINDS.map((kind, i) => [kind.key, 1 << i]));
|
|
143
|
+
|
|
144
|
+
// how much the heap has to shrink between frames to count as collected
|
|
145
|
+
const GC_BYTES = 1024 * 1024;
|
|
146
|
+
|
|
147
|
+
// the resources whose changes are events, with their names
|
|
148
|
+
const RESOURCE_KINDS = [['textures', 'Textures'], ['targets', 'Render targets'], ['buffers', 'Buffers']];
|
|
149
|
+
|
|
150
|
+
// a frame taking this many times the median frame is a hitch
|
|
151
|
+
const HITCH_FACTOR = 2;
|
|
152
|
+
|
|
153
|
+
// the share of the frame interval a processor has to fill for the frame to be bound by it
|
|
154
|
+
const BOUND_SHARE = 0.85;
|
|
155
|
+
|
|
156
|
+
// how long the browser's main thread is busy in a frame before it reports a long animation frame
|
|
157
|
+
const LONG_FRAME = 50;
|
|
158
|
+
|
|
159
|
+
// room to sort the values of a channel for its percentiles in; longer spans are sampled evenly
|
|
160
|
+
const SCRATCH = new Float64Array(4096);
|
|
161
|
+
|
|
162
|
+
// the colors of the script types with a band of their own, top down; later ones cycle through
|
|
163
|
+
// them in lighter and then darker shades. The other types share one
|
|
164
|
+
const SCRIPT_COLORS = ['#ffcc00', '#ff6482', '#64d2ff', '#30d158', '#bf5af2', '#ff9f0a', '#0a84ff', '#ff453a', '#66d4cf', '#ac8e68'];
|
|
165
|
+
const OTHER_SCRIPTS_COLOR = '#8e8e93';
|
|
166
|
+
|
|
167
|
+
// the kinds of asset loading is drawn in a color each, by the asset types of each; red is kept
|
|
168
|
+
// for the assets that failed
|
|
169
|
+
const ASSET_KINDS = [
|
|
170
|
+
{ name: 'Textures', color: '#ff9f0a', types: ['texture', 'textureatlas', 'cubemap'] },
|
|
171
|
+
{ name: 'Models', color: '#30d158', types: ['container', 'model', 'render'] },
|
|
172
|
+
{ name: 'Gaussian splats', color: '#0a84ff', types: ['gsplat'] },
|
|
173
|
+
{ name: 'Scripts', color: '#bf5af2', types: ['script'] },
|
|
174
|
+
{ name: 'Data', color: '#ffd60a', types: ['json', 'template', 'material'] },
|
|
175
|
+
{ name: 'Animations', color: '#66d4cf', types: ['animation'] },
|
|
176
|
+
{ name: 'Audio', color: '#64d2ff', types: ['audio'] },
|
|
177
|
+
{ name: 'Fonts', color: '#e5e5ea', types: ['font'] },
|
|
178
|
+
{ name: 'WebAssembly', color: '#ac8e68', types: ['wasm'] }
|
|
179
|
+
];
|
|
180
|
+
const ASSET_COLORS = Object.fromEntries(ASSET_KINDS.flatMap(kind => kind.types.map(type => [type, kind.color])));
|
|
181
|
+
const ASSET_COLOR = '#8e8e93';
|
|
182
|
+
const FAILED_COLOR = '#ff453a';
|
|
183
|
+
const REQUEST_COLOR = '#64d2ff';
|
|
184
|
+
const MAIN_THREAD_COLOR = '#ff2d92';
|
|
185
|
+
const MILESTONE_COLOR = '#e5e5ea';
|
|
186
|
+
|
|
187
|
+
// how many of the longest script types get a band of their own at first, and how many more each
|
|
188
|
+
// Show more adds
|
|
189
|
+
const SCRIPT_ROWS = 10;
|
|
190
|
+
|
|
191
|
+
// no script types pinned, shared
|
|
192
|
+
const NO_PINS = /** @type {ReadonlySet<string>} */ (new Set());
|
|
193
|
+
|
|
194
|
+
// the most script types the frame details list one by one
|
|
195
|
+
const MAX_SCRIPT_ROWS = 40;
|
|
196
|
+
|
|
197
|
+
// a clock coarser than this, in milliseconds, makes the times of single script calls noisy
|
|
198
|
+
const COARSE_CLOCK = 0.02;
|
|
199
|
+
|
|
200
|
+
// the slowest assets and the longest tasks of the main thread the start of the app lists, and the
|
|
201
|
+
// assets and the requests a frame lists of each
|
|
202
|
+
const STARTUP_ASSETS = 10;
|
|
203
|
+
const STARTUP_BLOCKS = 5;
|
|
204
|
+
const FRAME_LOADS = 20;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The norms of each capture, kept until it records more.
|
|
208
|
+
*
|
|
209
|
+
* @type {WeakMap<PerfCapture, { version: number, norms: { budget: number, median: number } }>}
|
|
210
|
+
*/
|
|
211
|
+
const normsCache = new WeakMap();
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The last legend of the Scripts chart made for each capture, kept while nothing it depends on
|
|
215
|
+
* changes: the frames recorded, the frames it ranks over, how many types it lists and those
|
|
216
|
+
* pinned.
|
|
217
|
+
*
|
|
218
|
+
* @type {WeakMap<PerfCapture, { key: string, legend: PerfScriptLegend }>}
|
|
219
|
+
*/
|
|
220
|
+
const legendCache = new WeakMap();
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @param {number} ms - A duration in milliseconds, which can be long.
|
|
224
|
+
* @returns {string} The duration in milliseconds below a second, in seconds above.
|
|
225
|
+
*/
|
|
226
|
+
function formatDuration(ms) {
|
|
227
|
+
if (!Number.isFinite(ms)) return '–';
|
|
228
|
+
return ms < 1000 ? `${ms.toFixed(0)} ms` : `${(ms / 1000).toFixed(ms < 10000 ? 2 : 1)} s`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @param {number} ms - A duration in milliseconds.
|
|
233
|
+
* @returns {string} The duration, to two decimals below ten.
|
|
234
|
+
*/
|
|
235
|
+
function formatMs(ms) {
|
|
236
|
+
if (!Number.isFinite(ms)) return '–';
|
|
237
|
+
return `${ms < 10 ? ms.toFixed(2) : ms.toFixed(1)} ms`;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @param {number} part - A part.
|
|
242
|
+
* @param {number} whole - The whole.
|
|
243
|
+
* @returns {string} The share, as a whole percentage.
|
|
244
|
+
*/
|
|
245
|
+
function formatShare(part, whole) {
|
|
246
|
+
return whole > 0 && Number.isFinite(part) ? `${Math.round(100 * part / whole)}%` : '';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @param {number} value - A count.
|
|
251
|
+
* @returns {string} The count with thousands separators, or a dash when unknown.
|
|
252
|
+
*/
|
|
253
|
+
function formatCount(value) {
|
|
254
|
+
if (!Number.isFinite(value)) return '–';
|
|
255
|
+
// by hand: toLocaleString goes through Intl, slow for the many counts of a redraw
|
|
256
|
+
const n = Math.round(value);
|
|
257
|
+
const digits = String(Math.abs(n));
|
|
258
|
+
let text = '';
|
|
259
|
+
for (let i = 0; i < digits.length; i++) {
|
|
260
|
+
if (i > 0 && (digits.length - i) % 3 === 0) text += ',';
|
|
261
|
+
text += digits[i];
|
|
262
|
+
}
|
|
263
|
+
return n < 0 ? `-${text}` : text;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @param {number} value - A value the app counts, of a unit of its own.
|
|
268
|
+
* @returns {string} The value: a whole number as a count, others to a few digits.
|
|
269
|
+
*/
|
|
270
|
+
function formatValue(value) {
|
|
271
|
+
if (!Number.isFinite(value)) return '–';
|
|
272
|
+
if (Number.isInteger(value)) return formatCount(value);
|
|
273
|
+
const size = Math.abs(value);
|
|
274
|
+
return value.toFixed(size >= 100 ? 0 : size >= 10 ? 1 : size >= 1 ? 2 : 3);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Sorts the finite values of a channel over a span of frames into the scratch buffer, sampling the
|
|
279
|
+
* span evenly when it holds more frames than the buffer.
|
|
280
|
+
*
|
|
281
|
+
* @param {PerfCapture} capture - The capture.
|
|
282
|
+
* @param {string|Float64Array} key - The channel, or a ring of values like one.
|
|
283
|
+
* @param {number} from - The first frame.
|
|
284
|
+
* @param {number} to - The last frame, inclusive.
|
|
285
|
+
* @returns {Float64Array} The sorted values, a view of the scratch buffer valid until the next call.
|
|
286
|
+
*/
|
|
287
|
+
function sortedValues(capture, key, from, to) {
|
|
288
|
+
const data = typeof key === 'string' ? capture.data[key] : key;
|
|
289
|
+
const span = to - from + 1;
|
|
290
|
+
const step = Math.max(1, span / SCRATCH.length);
|
|
291
|
+
let n = 0;
|
|
292
|
+
for (let i = 0; i < span && n < SCRATCH.length; i += step) {
|
|
293
|
+
const value = data[capture.slot(from + Math.floor(i))];
|
|
294
|
+
if (Number.isFinite(value)) SCRATCH[n++] = value;
|
|
295
|
+
}
|
|
296
|
+
return SCRATCH.subarray(0, n).sort();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* @param {Float64Array} sorted - Sorted values.
|
|
301
|
+
* @param {number} q - The quantile, 0 to 1.
|
|
302
|
+
* @returns {number} The value at that quantile, NaN when there are none.
|
|
303
|
+
*/
|
|
304
|
+
function quantile(sorted, q) {
|
|
305
|
+
if (sorted.length === 0) return NaN;
|
|
306
|
+
return sorted[Math.min(sorted.length - 1, Math.floor(q * sorted.length))];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @param {number} n - A count of calls, or their mean over frames.
|
|
311
|
+
* @returns {string} The count, with a decimal for a mean that is not whole.
|
|
312
|
+
*/
|
|
313
|
+
function callCount(n) {
|
|
314
|
+
return `${Number.isInteger(n) ? formatCount(n) : n.toFixed(1)} ${n === 1 ? 'call' : 'calls'}`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* @param {PerfCapture} capture - The capture.
|
|
319
|
+
* @param {string|Float64Array} key - The channel, or a ring of values like one.
|
|
320
|
+
* @param {number} from - The first frame.
|
|
321
|
+
* @param {number} to - The last frame, inclusive.
|
|
322
|
+
* @returns {PerfRangeStats} Its mean, 95th percentile and largest over the frames.
|
|
323
|
+
*/
|
|
324
|
+
function rangeStats(capture, key, from, to) {
|
|
325
|
+
const data = typeof key === 'string' ? capture.data[key] : key;
|
|
326
|
+
let count = 0;
|
|
327
|
+
let sum = 0;
|
|
328
|
+
let max = -Infinity;
|
|
329
|
+
for (let seq = from; seq <= to; seq++) {
|
|
330
|
+
const value = data[capture.slot(seq)];
|
|
331
|
+
if (!Number.isFinite(value)) continue;
|
|
332
|
+
count++;
|
|
333
|
+
sum += value;
|
|
334
|
+
if (value > max) max = value;
|
|
335
|
+
}
|
|
336
|
+
const sorted = sortedValues(capture, key, from, to);
|
|
337
|
+
return { count, avg: count ? sum / count : NaN, p95: quantile(sorted, 0.95), max: count ? max : NaN };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* The typical figures of a capture, against which a frame is judged: the frame interval the app
|
|
342
|
+
* manages at best, normally the display's, and the median one.
|
|
343
|
+
*
|
|
344
|
+
* @param {PerfCapture} capture - The capture.
|
|
345
|
+
* @returns {{ budget: number, median: number }} The 5th percentile and the median frame interval,
|
|
346
|
+
* NaN while nothing is recorded.
|
|
347
|
+
*/
|
|
348
|
+
function captureNorms(capture) {
|
|
349
|
+
const cached = normsCache.get(capture);
|
|
350
|
+
if (cached?.version === capture.version) return cached.norms;
|
|
351
|
+
const sorted = sortedValues(capture, 'interval', capture.first, capture.end - 1);
|
|
352
|
+
const norms = { budget: quantile(sorted, 0.05), median: quantile(sorted, 0.5) };
|
|
353
|
+
normsCache.set(capture, { version: capture.version, norms });
|
|
354
|
+
return norms;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* @param {number} index - The place of a script type among those with a band of their own.
|
|
359
|
+
* @returns {string} Its color: one of {@link SCRIPT_COLORS}, shaded lighter, then darker, on each
|
|
360
|
+
* time round them.
|
|
361
|
+
*/
|
|
362
|
+
function scriptColorAt(index) {
|
|
363
|
+
const base = SCRIPT_COLORS[index % SCRIPT_COLORS.length];
|
|
364
|
+
const round = Math.floor(index / SCRIPT_COLORS.length);
|
|
365
|
+
if (round === 0) return base;
|
|
366
|
+
const toward = round % 2 ? 255 : 0;
|
|
367
|
+
const amount = Math.min(0.6, Math.ceil(round / 2) * 0.3);
|
|
368
|
+
const channel = i => Math.round(parseInt(base.slice(1 + i * 2, 3 + i * 2), 16) * (1 - amount) + toward * amount);
|
|
369
|
+
return `rgb(${channel(0)}, ${channel(1)}, ${channel(2)})`;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* The script types the Scripts chart gives a band of their own: those pinned, then the ones that
|
|
374
|
+
* took the longest over a span of frames, the frames selected together or the whole capture, so a
|
|
375
|
+
* selection around a spike brings forward the scripts behind it.
|
|
376
|
+
*
|
|
377
|
+
* @param {PerfCapture} capture - The capture.
|
|
378
|
+
* @param {number} from - The first frame the types are ranked over.
|
|
379
|
+
* @param {number} to - The last, inclusive.
|
|
380
|
+
* @param {number} rows - How many of the longest types get a band, besides those pinned.
|
|
381
|
+
* @param {ReadonlySet<string>} pinned - The names of the types pinned.
|
|
382
|
+
* @returns {PerfScriptLegend} The legend.
|
|
383
|
+
*/
|
|
384
|
+
function scriptLegend(capture, from, to, rows, pinned) {
|
|
385
|
+
const key = `${capture.version}|${from}|${to}|${rows}|${[...pinned].join('\n')}`;
|
|
386
|
+
const cached = legendCache.get(capture);
|
|
387
|
+
if (cached?.key === key) return cached.legend;
|
|
388
|
+
const names = capture.scriptNames;
|
|
389
|
+
const totals = new Float64Array(names.length);
|
|
390
|
+
for (let seq = Math.max(from, capture.first); seq <= to && seq < capture.end; seq++) {
|
|
391
|
+
const times = capture.scriptFigures(seq);
|
|
392
|
+
if (!times) continue;
|
|
393
|
+
for (let base = 0, id = 0; base < times.length; base += SCRIPT_STRIDE, id++) {
|
|
394
|
+
totals[id] += times[base] + times[base + 1] + times[base + 2];
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const ids = [];
|
|
398
|
+
for (let id = 0; id < names.length; id++) {
|
|
399
|
+
if (totals[id] > 0 || pinned.has(names[id])) ids.push(id);
|
|
400
|
+
}
|
|
401
|
+
ids.sort((a, b) => totals[b] - totals[a]);
|
|
402
|
+
/** @type {PerfScriptLegend['types']} */
|
|
403
|
+
const types = [];
|
|
404
|
+
for (const id of ids) {
|
|
405
|
+
if (pinned.has(names[id])) types.push({ id, name: names[id], color: '', pinned: true });
|
|
406
|
+
}
|
|
407
|
+
let others = 0;
|
|
408
|
+
let ranked = 0;
|
|
409
|
+
for (const id of ids) {
|
|
410
|
+
if (pinned.has(names[id])) continue;
|
|
411
|
+
if (ranked < rows) {
|
|
412
|
+
types.push({ id, name: names[id], color: '', pinned: false });
|
|
413
|
+
ranked++;
|
|
414
|
+
} else {
|
|
415
|
+
others++;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const colors = new Map();
|
|
419
|
+
types.forEach((type, i) => {
|
|
420
|
+
type.color = scriptColorAt(i);
|
|
421
|
+
colors.set(type.id, type.color);
|
|
422
|
+
});
|
|
423
|
+
const legend = { types, colors, others, signature: types.map(type => type.id).join(',') };
|
|
424
|
+
legendCache.set(capture, { key, legend });
|
|
425
|
+
return legend;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* @param {PerfCapture} capture - The capture.
|
|
430
|
+
* @param {number} seq - A kept frame.
|
|
431
|
+
* @param {number} median - The median frame interval of the capture.
|
|
432
|
+
* @returns {boolean} Whether the frame took far longer than the median frame.
|
|
433
|
+
*/
|
|
434
|
+
function isHitch(capture, seq, median) {
|
|
435
|
+
return capture.get('interval', seq) > HITCH_FACTOR * median;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* @param {PerfCapture} capture - The capture.
|
|
440
|
+
* @param {number} seq - A kept entry.
|
|
441
|
+
* @returns {number} The slot of the frame before it, or -1 where none is kept, or where the entry
|
|
442
|
+
* before it is the start of the app, which has no figures of a frame to compare with.
|
|
443
|
+
*/
|
|
444
|
+
function previousSlot(capture, seq) {
|
|
445
|
+
return capture.has(seq - 1) && !capture.isStartup(seq - 1) ? capture.slot(seq - 1) : -1;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* @param {PerfCapture} capture - The capture.
|
|
450
|
+
* @param {number} seq - A kept frame.
|
|
451
|
+
* @returns {number} The kinds of event the frame had, a bit per kind of {@link EVENT_KINDS} in
|
|
452
|
+
* its order.
|
|
453
|
+
*/
|
|
454
|
+
function eventMask(capture, seq) {
|
|
455
|
+
const d = capture.data;
|
|
456
|
+
const s = capture.slot(seq);
|
|
457
|
+
const p = previousSlot(capture, seq);
|
|
458
|
+
// called for every frame on every redraw of the charts, so it is kept free of garbage
|
|
459
|
+
let mask = isHitch(capture, seq, captureNorms(capture).median) ? EVENT_BITS.hitch : 0;
|
|
460
|
+
const events = capture.events(seq);
|
|
461
|
+
for (let i = 0; i < events.length; i++) {
|
|
462
|
+
const kind = events[i].kind;
|
|
463
|
+
mask |= kind === 'long' ? EVENT_BITS.long : kind === 'bake' ? EVENT_BITS.bake : EVENT_BITS.marker;
|
|
464
|
+
}
|
|
465
|
+
if (p < 0) return mask | (d.compiles[s] > 0 ? EVENT_BITS.compile : 0);
|
|
466
|
+
if (d.compiles[s] > 0 || d.shaders[s] > d.shaders[p] || d.pipelines[s] > d.pipelines[p]) mask |= EVENT_BITS.compile;
|
|
467
|
+
if (d.heap[p] - d.heap[s] > GC_BYTES) mask |= EVENT_BITS.gc;
|
|
468
|
+
for (let i = 0; i < RESOURCE_KINDS.length; i++) {
|
|
469
|
+
const values = d[RESOURCE_KINDS[i][0]];
|
|
470
|
+
if (Number.isFinite(values[s]) && values[s] !== values[p]) {
|
|
471
|
+
mask |= EVENT_BITS.resource;
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return mask;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* @param {PerfEvent} event - A bake of lightmaps.
|
|
480
|
+
* @returns {string[]} How long it took and how many it baked, then the render targets and
|
|
481
|
+
* shaders it made.
|
|
482
|
+
*/
|
|
483
|
+
function bakeLines(event) {
|
|
484
|
+
const bake = event.bake;
|
|
485
|
+
if (!bake) return [`${formatMs(event.duration)} baking lightmaps`];
|
|
486
|
+
return [
|
|
487
|
+
`${formatMs(event.duration)} baking ${formatCount(bake.lightmaps)} ${bake.lightmaps === 1 ? 'lightmap' : 'lightmaps'} in ${formatCount(bake.passes)} passes`,
|
|
488
|
+
`render targets created in ${formatMs(bake.targets)} · ${formatCount(bake.shaders)} shaders linked in ${formatMs(bake.compile)}`
|
|
489
|
+
];
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* @param {PerfCapture} capture - The capture.
|
|
494
|
+
* @param {number} seq - A kept frame.
|
|
495
|
+
* @param {string} kind - The key of one of the {@link EVENT_KINDS}.
|
|
496
|
+
* @returns {string[]} What the frame's events of that kind were, a line each.
|
|
497
|
+
*/
|
|
498
|
+
function eventLines(capture, seq, kind) {
|
|
499
|
+
const d = capture.data;
|
|
500
|
+
const s = capture.slot(seq);
|
|
501
|
+
const p = previousSlot(capture, seq);
|
|
502
|
+
const at = start => `at +${formatMs(start - d.tick[s])}`;
|
|
503
|
+
const lines = [];
|
|
504
|
+
const change = (key, name) => {
|
|
505
|
+
if (p < 0 || !Number.isFinite(d[key][s]) || d[key][s] === d[key][p]) return;
|
|
506
|
+
const delta = d[key][s] - d[key][p];
|
|
507
|
+
lines.push(`${name} ${formatCount(d[key][p])} → ${formatCount(d[key][s])} (${delta > 0 ? '+' : ''}${delta})`);
|
|
508
|
+
};
|
|
509
|
+
switch (kind) {
|
|
510
|
+
case 'hitch': {
|
|
511
|
+
const { median } = captureNorms(capture);
|
|
512
|
+
if (!isHitch(capture, seq, median)) break;
|
|
513
|
+
lines.push(`${formatMs(d.interval[s])}, ${(d.interval[s] / median).toFixed(1)}× the median frame of ${formatMs(median)}`);
|
|
514
|
+
// whether the main thread held it back, where the browser can tell
|
|
515
|
+
const blocked = capture.events(seq).find(event => event.kind === 'long');
|
|
516
|
+
if (blocked) {
|
|
517
|
+
lines.push(`Main thread blocked ${formatMs(blocked.duration)}, longest script ${blocked.name}`);
|
|
518
|
+
} else if (capture.supports.longFrames && d.interval[s] >= LONG_FRAME) {
|
|
519
|
+
lines.push('Main thread not blocked: the GPU, the browser or the display held it back');
|
|
520
|
+
}
|
|
521
|
+
break;
|
|
522
|
+
}
|
|
523
|
+
case 'long':
|
|
524
|
+
for (const event of capture.events(seq)) {
|
|
525
|
+
if (event.kind !== 'long') continue;
|
|
526
|
+
lines.push(`${formatMs(event.duration)} busy, longest script ${event.name}`);
|
|
527
|
+
if (event.detail) lines.push(event.detail);
|
|
528
|
+
}
|
|
529
|
+
break;
|
|
530
|
+
case 'bake':
|
|
531
|
+
for (const event of capture.events(seq)) {
|
|
532
|
+
if (event.kind === 'bake') lines.push(...bakeLines(event));
|
|
533
|
+
}
|
|
534
|
+
break;
|
|
535
|
+
case 'compile':
|
|
536
|
+
if (d.compiles[s] > 0) lines.push(`${formatCount(d.compiles[s])} shader compiles and links, ${formatMs(d.compileTime[s])}`);
|
|
537
|
+
if (p >= 0 && d.shaders[s] > d.shaders[p]) change('shaders', 'Shaders');
|
|
538
|
+
if (p >= 0 && d.pipelines[s] > d.pipelines[p]) change('pipelines', 'Pipelines');
|
|
539
|
+
break;
|
|
540
|
+
case 'gc':
|
|
541
|
+
if (p >= 0 && d.heap[p] - d.heap[s] > GC_BYTES) {
|
|
542
|
+
lines.push(`JavaScript heap ${formatBytes(d.heap[p])} → ${formatBytes(d.heap[s])}, ${formatBytes(d.heap[p] - d.heap[s])} freed`);
|
|
543
|
+
}
|
|
544
|
+
break;
|
|
545
|
+
case 'resource':
|
|
546
|
+
for (const [key, name] of RESOURCE_KINDS) change(key, name);
|
|
547
|
+
break;
|
|
548
|
+
case 'marker':
|
|
549
|
+
for (const event of capture.events(seq)) {
|
|
550
|
+
if (event.kind === 'marker') lines.push(`${event.name}, ${formatMs(event.duration)} ${at(event.start)}`);
|
|
551
|
+
}
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
return lines;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* @param {PerfCapture} capture - The capture.
|
|
559
|
+
* @param {string} kind - The key of one of the {@link EVENT_KINDS}.
|
|
560
|
+
* @returns {string} Why the browser or engine cannot report that kind of event, or an empty string
|
|
561
|
+
* when it can.
|
|
562
|
+
*/
|
|
563
|
+
function eventUnsupported(capture, kind) {
|
|
564
|
+
if (kind === 'long' && !capture.supports.longFrames) return 'This browser does not report when its main thread is blocked: Chromium browsers do';
|
|
565
|
+
if (kind === 'marker' && !capture.supports.measures) return 'This browser does not report measures';
|
|
566
|
+
if (kind === 'gc' && !capture.supports.heap) return 'Only Chromium browsers report the size of the JavaScript heap';
|
|
567
|
+
if (kind === 'bake' && !capture.detailed) return 'Lightmap bakes are reported by the profiler and debug engines';
|
|
568
|
+
return '';
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* What held a frame back: the processor that was busy for most of it, the display, or neither,
|
|
573
|
+
* when the time went outside the engine.
|
|
574
|
+
*
|
|
575
|
+
* @param {PerfCapture} capture - The capture.
|
|
576
|
+
* @param {number} seq - A kept frame.
|
|
577
|
+
* @param {number} budget - The frame interval the app manages at best.
|
|
578
|
+
* @returns {string} The verdict.
|
|
579
|
+
*/
|
|
580
|
+
function frameVerdict(capture, seq, budget) {
|
|
581
|
+
const interval = capture.get('interval', seq);
|
|
582
|
+
const cpu = capture.get('cpu', seq);
|
|
583
|
+
const gpu = capture.get('gpu', seq);
|
|
584
|
+
if (gpu >= BOUND_SHARE * interval && gpu >= cpu) return 'GPU-bound';
|
|
585
|
+
if (cpu >= BOUND_SHARE * interval) return 'CPU-bound';
|
|
586
|
+
const busiest = Math.max(cpu, Number.isFinite(gpu) ? gpu : 0);
|
|
587
|
+
if (interval <= budget * 1.15) {
|
|
588
|
+
return `Waiting for the display, ${formatMs(Math.max(0, interval - busiest))} to spare`;
|
|
589
|
+
}
|
|
590
|
+
return Number.isFinite(gpu) ?
|
|
591
|
+
'Time went outside the engine: browser work, garbage collection or other tasks' :
|
|
592
|
+
'CPU not busy; turn GPU timings on to tell whether the GPU held it back';
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* @param {string} text - The text.
|
|
597
|
+
* @param {string} [cls] - The value class.
|
|
598
|
+
* @param {number} [raw] - The value described, which a copy takes.
|
|
599
|
+
* @returns {Described} A value.
|
|
600
|
+
*/
|
|
601
|
+
function value(text, cls = 'num', raw) {
|
|
602
|
+
return raw === undefined ? { text, cls } : { text, cls, raw };
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* @param {string} text - A note.
|
|
607
|
+
* @returns {Described} The note, muted.
|
|
608
|
+
*/
|
|
609
|
+
function note(text) {
|
|
610
|
+
return { text, cls: 'null' };
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* @param {number} ms - A duration.
|
|
615
|
+
* @param {number} whole - The frame interval it is a share of.
|
|
616
|
+
* @param {string} [swatch] - The color it is drawn in.
|
|
617
|
+
* @returns {Described} The duration with its share of the frame.
|
|
618
|
+
*/
|
|
619
|
+
function timing(ms, whole, swatch) {
|
|
620
|
+
const share = formatShare(ms, whole);
|
|
621
|
+
const described = value(`${formatMs(ms)}${share ? ` · ${share}` : ''}`, 'num', Number.isFinite(ms) ? ms : undefined);
|
|
622
|
+
if (swatch) described.swatch = swatch;
|
|
623
|
+
return described;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* @param {number} now - A resource count.
|
|
628
|
+
* @param {number} before - The count a frame earlier, NaN when unknown.
|
|
629
|
+
* @returns {Described} The count, with how it changed.
|
|
630
|
+
*/
|
|
631
|
+
function countChange(now, before) {
|
|
632
|
+
const delta = now - before;
|
|
633
|
+
const change = Number.isFinite(delta) && delta !== 0 ? ` (${delta > 0 ? '+' : ''}${delta})` : '';
|
|
634
|
+
return value(`${formatCount(now)}${change}`, 'num', Number.isFinite(now) ? now : undefined);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* @param {PerfLoad} load - A load.
|
|
639
|
+
* @returns {string} What it was: an asset or a request with how long it took, its request and
|
|
640
|
+
* its size, or a moment with when it was.
|
|
641
|
+
*/
|
|
642
|
+
function loadText(load) {
|
|
643
|
+
if (load.kind === 'milestone') return `${load.name} at ${formatDuration(load.start)}`;
|
|
644
|
+
const took = Number.isFinite(load.start) ? formatMs(load.end - load.start) : 'began before recording';
|
|
645
|
+
// the type first, so the color is never all that tells it
|
|
646
|
+
const parts = [load.kind === 'asset' && load.type ? `${load.name} · ${load.type} · ${took}` : `${load.name} ${took}`];
|
|
647
|
+
if (load.failed) parts.push('failed');
|
|
648
|
+
if (load.kind === 'asset' && Number.isFinite(load.netEnd)) parts.push(`request ${formatMs(load.netEnd - load.netStart)}`);
|
|
649
|
+
if (load.kind === 'request' && Number.isFinite(load.firstByte) && load.firstByte > 0) parts.push(`first byte after ${formatMs(load.firstByte - load.netStart)}`);
|
|
650
|
+
if (load.bytes > 0) parts.push(`${formatBytes(load.bytes)}${load.cached ? ', cached' : ''}`);
|
|
651
|
+
else if (load.cached) parts.push('cached');
|
|
652
|
+
return parts.join(' · ');
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* @param {PerfLoad} load - A load.
|
|
657
|
+
* @returns {string} The color it is drawn in.
|
|
658
|
+
*/
|
|
659
|
+
function loadColor(load) {
|
|
660
|
+
if (load.failed) return FAILED_COLOR;
|
|
661
|
+
if (load.kind === 'asset') return ASSET_COLORS[load.type] ?? ASSET_COLOR;
|
|
662
|
+
if (load.kind === 'request') return REQUEST_COLOR;
|
|
663
|
+
return MILESTONE_COLOR;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* @param {PerfSelection} selection - The selection.
|
|
668
|
+
* @returns {PropertySection} The figures of the selected range of frames.
|
|
669
|
+
*/
|
|
670
|
+
function rangeSection(selection) {
|
|
671
|
+
const { capture, range } = selection;
|
|
672
|
+
const from = Math.max(capture.first, range.from);
|
|
673
|
+
const to = Math.min(capture.end - 1, range.to);
|
|
674
|
+
const section = makeSection('range', capture.rangeLabel(from, to));
|
|
675
|
+
const interval = rangeStats(capture, 'interval', from, to);
|
|
676
|
+
const { median } = captureNorms(capture);
|
|
677
|
+
let hitches = 0;
|
|
678
|
+
for (let seq = from; seq <= to; seq++) {
|
|
679
|
+
if (isHitch(capture, seq, median)) hitches++;
|
|
680
|
+
}
|
|
681
|
+
const fps = interval.avg > 0 ? ` · ${(1000 / interval.avg).toFixed(1)} fps` : '';
|
|
682
|
+
section.rows.push({ key: 'count', label: 'Frames', value: value(`${formatCount(to - from + 1)}${fps}`, 'num', to - from + 1) });
|
|
683
|
+
section.rows.push({ key: 'hitches', label: 'Hitches', value: value(formatCount(hitches), 'num', hitches) });
|
|
684
|
+
const figure = (key, label, stat, format) => {
|
|
685
|
+
const text = stat.count ? `avg ${format(stat.avg)} · p95 ${format(stat.p95)} · max ${format(stat.max)}` : '–';
|
|
686
|
+
section.rows.push({ key, label, value: stat.count ? { text, cls: 'num', raw: stat } : note('not recorded') });
|
|
687
|
+
};
|
|
688
|
+
figure('interval', 'Frame', interval, formatMs);
|
|
689
|
+
figure('cpu', 'CPU', rangeStats(capture, 'cpu', from, to), formatMs);
|
|
690
|
+
figure('gpu', 'GPU', rangeStats(capture, 'gpu', from, to), formatMs);
|
|
691
|
+
figure('inspector', 'Inspector', rangeStats(capture, 'inspector', from, to), formatMs);
|
|
692
|
+
if (capture.supports.scripts) figure('scripts', 'Scripts', rangeStats(capture, 'scriptsTotal', from, to), formatMs);
|
|
693
|
+
figure('draws', 'Draw calls', rangeStats(capture, 'draws', from, to), formatCount);
|
|
694
|
+
figure('vram', 'Video memory', rangeStats(capture, 'vram', from, to), formatBytes);
|
|
695
|
+
return section;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* @param {PerfLoad} load - An asset, or a request.
|
|
700
|
+
* @returns {number} How long it took; NaN for an asset that began loading before recording did.
|
|
701
|
+
*/
|
|
702
|
+
function loadTime(load) {
|
|
703
|
+
return load.end - load.start;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* @param {PerfLoad} load - An asset, or a request.
|
|
708
|
+
* @param {number} depth - How deep its row is.
|
|
709
|
+
* @returns {PropertyRow} Its row: what it was, how long it took, for an asset its request and the
|
|
710
|
+
* processing after it, and its size.
|
|
711
|
+
*/
|
|
712
|
+
function loadRow(load, depth) {
|
|
713
|
+
const took = loadTime(load);
|
|
714
|
+
const parts = [Number.isFinite(took) ? formatMs(took) : 'began before recording'];
|
|
715
|
+
if (load.kind === 'asset' && load.type) parts.unshift(load.type);
|
|
716
|
+
if (load.failed) parts.push('failed');
|
|
717
|
+
if (load.kind === 'asset' && Number.isFinite(load.netEnd)) {
|
|
718
|
+
parts.push(`request ${formatMs(load.netEnd - load.netStart)}`);
|
|
719
|
+
if (Number.isFinite(load.start)) parts.push(`processing ${formatMs(Math.max(0, load.end - load.netEnd))}`);
|
|
720
|
+
}
|
|
721
|
+
if (load.kind === 'request' && load.firstByte > 0) parts.push(`first byte after ${formatMs(load.firstByte - load.netStart)}`);
|
|
722
|
+
if (load.bytes > 0) parts.push(`${formatBytes(load.bytes)}${load.cached ? ', cached' : ''}`);
|
|
723
|
+
else if (load.cached) parts.push('cached');
|
|
724
|
+
const described = value(parts.join(' · '), load.failed ? 'err' : 'num', Number.isFinite(took) ? took : undefined);
|
|
725
|
+
described.swatch = loadColor(load);
|
|
726
|
+
return { key: `${load.kind}:${load.id ?? load.url ?? load.name}:${load.start}`, label: load.name, value: described, depth };
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* @param {readonly PerfLoad[]} loads - What loaded in an entry.
|
|
731
|
+
* @returns {{ assets: PerfLoad[], requests: PerfLoad[], milestones: PerfLoad[] }} Its assets and
|
|
732
|
+
* its requests, the longest first, and its moments in their order.
|
|
733
|
+
*/
|
|
734
|
+
function splitLoads(loads) {
|
|
735
|
+
const byTime = (a, b) => (loadTime(b) || 0) - (loadTime(a) || 0);
|
|
736
|
+
return {
|
|
737
|
+
assets: loads.filter(load => load.kind === 'asset').sort(byTime),
|
|
738
|
+
requests: loads.filter(load => load.kind === 'request').sort(byTime),
|
|
739
|
+
milestones: loads.filter(load => load.kind === 'milestone').sort((a, b) => a.start - b.start)
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* @param {PerfLoad[]} requests - Requests.
|
|
745
|
+
* @returns {Described} How many there were, their size and how many came from the cache.
|
|
746
|
+
*/
|
|
747
|
+
function requestSummary(requests) {
|
|
748
|
+
const bytes = requests.reduce((sum, request) => sum + (request.bytes || 0), 0);
|
|
749
|
+
const cached = requests.filter(request => request.cached).length;
|
|
750
|
+
return value(`${formatCount(requests.length)} · ${formatBytes(bytes)}${cached ? ` · ${formatCount(cached)} from the cache` : ''}`, 'num', requests.length);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* The start of the app, from the page opening to its first frame: how long it took, what the page
|
|
755
|
+
* requested and the assets it loaded, its moments, the slowest assets and the longest tasks of the
|
|
756
|
+
* main thread.
|
|
757
|
+
*
|
|
758
|
+
* @param {PerfCapture} capture - The capture.
|
|
759
|
+
* @param {number} seq - The entry of the start of the app.
|
|
760
|
+
* @returns {PropertySection[]} The sections.
|
|
761
|
+
*/
|
|
762
|
+
function startupSections(capture, seq) {
|
|
763
|
+
const d = capture.data;
|
|
764
|
+
const s = capture.slot(seq);
|
|
765
|
+
const length = d.width[s];
|
|
766
|
+
const { assets, requests, milestones } = splitLoads(capture.loads(seq));
|
|
767
|
+
/** @type {PropertySection[]} */
|
|
768
|
+
const sections = [];
|
|
769
|
+
|
|
770
|
+
const startup = makeSection('frame', 'Startup');
|
|
771
|
+
startup.rows.push({ key: 'length', label: 'First frame', value: value(`${formatDuration(length)} after the page opened`, 'num', length) });
|
|
772
|
+
startup.rows.push({ key: 'requests', label: 'Requests', value: requestSummary(requests) });
|
|
773
|
+
const failed = assets.filter(asset => asset.failed).length;
|
|
774
|
+
const loading = capture.pendingLoads().filter(load => load.start < length).length;
|
|
775
|
+
const parts = [`${formatCount(assets.length - failed)} loaded`];
|
|
776
|
+
if (failed) parts.push(`${formatCount(failed)} failed`);
|
|
777
|
+
if (loading) parts.push(`${formatCount(loading)} still loading`);
|
|
778
|
+
startup.rows.push({ key: 'assets', label: 'Assets', value: value(parts.join(' · '), failed ? 'err' : 'num', assets.length) });
|
|
779
|
+
if (d.compiles[s] > 0) {
|
|
780
|
+
// WebGL compiles lazily, so the device may give no time for them
|
|
781
|
+
const time = d.compileTime[s] > 0 ? ` · ${formatMs(d.compileTime[s])}` : '';
|
|
782
|
+
startup.rows.push({ key: 'compiles', label: 'Shader compiles', value: value(`${formatCount(d.compiles[s])}${time}`, 'num', d.compiles[s]) });
|
|
783
|
+
} else if (!capture.detailed) {
|
|
784
|
+
startup.rows.push({ key: 'compiles', label: 'Shader compiles', value: note('counted by the profiler and debug engines') });
|
|
785
|
+
}
|
|
786
|
+
const bakes = capture.events(seq).filter(event => event.kind === 'bake');
|
|
787
|
+
if (bakes.length) {
|
|
788
|
+
const time = bakes.reduce((sum, event) => sum + event.duration, 0);
|
|
789
|
+
startup.rows.push({ key: 'bakes', label: 'Lightmap bakes', value: value(`${formatCount(bakes.length)} · ${formatMs(time)}`, 'num', time) });
|
|
790
|
+
}
|
|
791
|
+
sections.push(startup);
|
|
792
|
+
|
|
793
|
+
if (milestones.length) {
|
|
794
|
+
const moments = makeSection('milestones', 'Milestones');
|
|
795
|
+
for (const load of milestones) {
|
|
796
|
+
const described = value(formatDuration(load.start), 'num', load.start);
|
|
797
|
+
described.swatch = loadColor(load);
|
|
798
|
+
moments.rows.push({ key: `milestone:${load.name}`, label: load.name, value: described });
|
|
799
|
+
}
|
|
800
|
+
sections.push(moments);
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
const timed = assets.filter(asset => Number.isFinite(loadTime(asset)));
|
|
804
|
+
if (timed.length) {
|
|
805
|
+
const slowest = makeSection('assets', 'Slowest assets');
|
|
806
|
+
for (const load of timed.slice(0, STARTUP_ASSETS)) slowest.rows.push(loadRow(load, 0));
|
|
807
|
+
if (timed.length > STARTUP_ASSETS) slowest.rows.push({ key: 'more', label: '', value: note(`and ${timed.length - STARTUP_ASSETS} more`) });
|
|
808
|
+
sections.push(slowest);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
const blocks = capture.events(seq).filter(event => event.kind === 'long').sort((a, b) => b.duration - a.duration);
|
|
812
|
+
if (blocks.length) {
|
|
813
|
+
const main = makeSection('main', 'Main thread blocked');
|
|
814
|
+
for (const [n, event] of blocks.slice(0, STARTUP_BLOCKS).entries()) {
|
|
815
|
+
const described = value(`${formatMs(event.duration)} · longest script ${event.name}${event.detail ? ` · ${event.detail}` : ''}`, 'num', event.duration);
|
|
816
|
+
described.swatch = MAIN_THREAD_COLOR;
|
|
817
|
+
main.rows.push({ key: `long:${n}`, label: `At ${formatDuration(event.start)}`, value: described });
|
|
818
|
+
}
|
|
819
|
+
if (blocks.length > STARTUP_BLOCKS) main.rows.push({ key: 'more', label: '', value: note(`and ${blocks.length - STARTUP_BLOCKS} more`) });
|
|
820
|
+
sections.push(main);
|
|
821
|
+
}
|
|
822
|
+
return sections;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* What loaded during a frame: its moments, the assets that finished and the requests that ended.
|
|
827
|
+
*
|
|
828
|
+
* @param {PerfCapture} capture - The capture.
|
|
829
|
+
* @param {number} seq - The frame.
|
|
830
|
+
* @returns {PropertySection|null} The section, or null when nothing loaded.
|
|
831
|
+
*/
|
|
832
|
+
function loadingSection(capture, seq) {
|
|
833
|
+
const loads = capture.loads(seq);
|
|
834
|
+
if (!loads.length) return null;
|
|
835
|
+
const { assets, requests, milestones } = splitLoads(loads);
|
|
836
|
+
const section = makeSection('loading', 'Loading');
|
|
837
|
+
const tick = capture.data.tick[capture.slot(seq)];
|
|
838
|
+
for (const load of milestones) {
|
|
839
|
+
const described = value(`at +${formatMs(load.start - tick)}`, 'num', load.start);
|
|
840
|
+
described.swatch = loadColor(load);
|
|
841
|
+
section.rows.push({ key: `milestone:${load.name}`, label: load.name, value: described });
|
|
842
|
+
}
|
|
843
|
+
if (assets.length) {
|
|
844
|
+
const failed = assets.filter(asset => asset.failed).length;
|
|
845
|
+
section.rows.push({ key: 'assets', label: 'Assets', value: value(`${formatCount(assets.length)} finished${failed ? ` · ${formatCount(failed)} failed` : ''}`, failed ? 'err' : 'num', assets.length) });
|
|
846
|
+
for (const load of assets.slice(0, FRAME_LOADS)) section.rows.push(loadRow(load, 1));
|
|
847
|
+
if (assets.length > FRAME_LOADS) section.rows.push({ key: 'moreAssets', label: '', value: note(`and ${assets.length - FRAME_LOADS} more`), depth: 1 });
|
|
848
|
+
}
|
|
849
|
+
if (requests.length) {
|
|
850
|
+
section.rows.push({ key: 'requests', label: 'Requests', value: requestSummary(requests) });
|
|
851
|
+
for (const load of requests.slice(0, FRAME_LOADS)) section.rows.push(loadRow(load, 1));
|
|
852
|
+
if (requests.length > FRAME_LOADS) section.rows.push({ key: 'moreRequests', label: '', value: note(`and ${requests.length - FRAME_LOADS} more`), depth: 1 });
|
|
853
|
+
}
|
|
854
|
+
return section;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* Builds the property view sections for the frames selected in a capture: the range, when frames
|
|
859
|
+
* are selected together, then everything recorded for the frame shown.
|
|
860
|
+
*
|
|
861
|
+
* @param {PerfSelection} selection - The selection.
|
|
862
|
+
* @returns {PropertySection[]} The sections.
|
|
863
|
+
*/
|
|
864
|
+
function buildPerfModel(selection) {
|
|
865
|
+
const { capture, seq, gpu } = selection;
|
|
866
|
+
/** @type {PropertySection[]} */
|
|
867
|
+
const sections = [];
|
|
868
|
+
if (selection.range) sections.push(rangeSection(selection));
|
|
869
|
+
if (!capture.has(seq)) return sections;
|
|
870
|
+
// the start of the app ran no frame: it has what loaded instead
|
|
871
|
+
if (capture.isStartup(seq)) return sections.concat(startupSections(capture, seq));
|
|
872
|
+
|
|
873
|
+
const d = capture.data;
|
|
874
|
+
const s = capture.slot(seq);
|
|
875
|
+
const before = previousSlot(capture, seq);
|
|
876
|
+
const prev = key => (before >= 0 ? d[key][before] : NaN);
|
|
877
|
+
const interval = d.interval[s];
|
|
878
|
+
const { budget, median } = captureNorms(capture);
|
|
879
|
+
|
|
880
|
+
const frame = makeSection('frame', capture.label(seq));
|
|
881
|
+
const fps = interval > 0 ? ` · ${(1000 / interval).toFixed(1)} fps` : '';
|
|
882
|
+
frame.rows.push({ key: 'interval', label: 'Frame interval', value: value(`${formatMs(interval)}${fps}`, 'num', interval) });
|
|
883
|
+
if (isHitch(capture, seq, median)) {
|
|
884
|
+
frame.rows.push({ key: 'hitch', label: 'Hitch', value: { text: `${(interval / median).toFixed(1)}× the median frame of ${formatMs(median)}`, cls: 'err' } });
|
|
885
|
+
}
|
|
886
|
+
frame.rows.push({ key: 'verdict', label: 'Held back by', value: value(frameVerdict(capture, seq, budget), 'str') });
|
|
887
|
+
frame.rows.push({ key: 'cpu', label: 'CPU', value: timing(d.cpu[s], interval) });
|
|
888
|
+
frame.rows.push({ key: 'gpu', label: 'GPU', value: Number.isFinite(d.gpu[s]) ? timing(d.gpu[s], interval) : note(gpuNote(gpu)) });
|
|
889
|
+
frame.rows.push({ key: 'budget', label: 'Display interval', value: value(`${formatMs(budget)}, the fastest 5% of frames`, 'num', budget) });
|
|
890
|
+
frame.rows.push({ key: 'time', label: 'Time', value: value(`${((d.tick[s] - capture.get('tick', capture.first)) / 1000).toFixed(2)} s into the capture`, 'num') });
|
|
891
|
+
if (Number.isFinite(d.appFrame[s])) frame.rows.push({ key: 'appFrame', label: 'App frame', value: value(formatCount(d.appFrame[s]), 'num', d.appFrame[s]) });
|
|
892
|
+
if (d.version[s] < 0) frame.rows.push({ key: 'rendered', label: 'Rendered', value: note('no, the app skipped rendering this frame') });
|
|
893
|
+
sections.push(frame);
|
|
894
|
+
|
|
895
|
+
const cpu = makeSection('cpu', 'CPU, main thread');
|
|
896
|
+
const part = key => CPU_PARTS.find(p => p.key === key);
|
|
897
|
+
const row = (key, label, ms, depth, swatch) => cpu.rows.push({ key, label, value: timing(ms, interval, swatch), depth });
|
|
898
|
+
row('update', 'Update', d.update[s], 0);
|
|
899
|
+
for (const key of ['systems', 'physics', 'scripts', 'anim', 'post', 'other', 'inspector']) {
|
|
900
|
+
const p = part(key);
|
|
901
|
+
// the scripts only while they were timed apart
|
|
902
|
+
if (key === 'scripts' && !Number.isFinite(d.scripts[s])) continue;
|
|
903
|
+
row(key, p.name, d[p.channel][s], key === 'physics' ? 2 : 1, p.color);
|
|
904
|
+
}
|
|
905
|
+
row('render', 'Render', d.render[s], 0, part('render').color);
|
|
906
|
+
if (capture.detailed) {
|
|
907
|
+
for (const [key, label] of RENDER_PHASES) {
|
|
908
|
+
// the batching only with a batcher, with the batches it created apart
|
|
909
|
+
if (key === 'batching') {
|
|
910
|
+
if (!Number.isFinite(d.batching[s])) continue;
|
|
911
|
+
const described = timing(d.batching[s], interval);
|
|
912
|
+
if (d.batchCreate[s] > 0) described.text += ` · creating batches ${formatMs(d.batchCreate[s])}`;
|
|
913
|
+
cpu.rows.push({ key, label, value: described, depth: 1 });
|
|
914
|
+
continue;
|
|
915
|
+
}
|
|
916
|
+
row(key, label, d[key][s], 1);
|
|
917
|
+
}
|
|
918
|
+
} else {
|
|
919
|
+
cpu.rows.push({ key: 'phases', label: 'Phases', value: note('timed by the profiler and debug engines'), depth: 1 });
|
|
920
|
+
}
|
|
921
|
+
row('outside', 'Outside the engine', Math.max(0, interval - d.cpu[s]), 0);
|
|
922
|
+
if (d.splatSort[s] > 0) {
|
|
923
|
+
cpu.rows.push({ key: 'splatSort', label: 'Splat sort', value: value(`${formatMs(d.splatSort[s])} on a worker`, 'num', d.splatSort[s]) });
|
|
924
|
+
}
|
|
925
|
+
sections.push(cpu);
|
|
926
|
+
if (capture.supports.scripts) sections.push(scriptsSection(selection));
|
|
927
|
+
const counters = countersSection(selection);
|
|
928
|
+
if (counters) sections.push(counters);
|
|
929
|
+
const loading = loadingSection(capture, seq);
|
|
930
|
+
if (loading) sections.push(loading);
|
|
931
|
+
|
|
932
|
+
const gpuSection = makeSection('gpu', 'GPU');
|
|
933
|
+
const passes = capture.passesInOrder(seq);
|
|
934
|
+
if (passes.length) {
|
|
935
|
+
// on WebGPU the frame spans the first pass beginning to the last ending, and pipelined
|
|
936
|
+
// passes overlap, so the passes are shares of their own sum rather than of the frame
|
|
937
|
+
const sum = passes.reduce((total, pass) => total + pass.time, 0);
|
|
938
|
+
gpuSection.rows.push({ key: 'frame', label: 'Frame', value: timing(d.gpu[s], interval) });
|
|
939
|
+
if (sum > d.gpu[s] * 1.05) {
|
|
940
|
+
gpuSection.rows.push({ key: 'sum', label: 'Passes', value: value(`${formatMs(sum)} summed, as passes overlap on the GPU`, 'num', sum) });
|
|
941
|
+
}
|
|
942
|
+
// the effects with the passes they run, and the passes of their own, longest first; passes
|
|
943
|
+
// sharing a name together, with the time of each
|
|
944
|
+
for (const group of gpuGroups(passes)) {
|
|
945
|
+
if (group.effect) {
|
|
946
|
+
gpuSection.rows.push({ key: `effect:${group.key}`, label: effectName(group.effect), value: value(`${formatMs(group.time)} · ${formatShare(group.time, sum)}`, 'num', group.time), depth: 1 });
|
|
947
|
+
}
|
|
948
|
+
for (const [name, times] of group.passes) {
|
|
949
|
+
const time = times.reduce((total, t) => total + t, 0);
|
|
950
|
+
const each = times.length > 1 ? ` · ${times.map(t => t.toFixed(2)).join(', ')}` : '';
|
|
951
|
+
const label = times.length > 1 ? `${name} ×${times.length}` : name;
|
|
952
|
+
gpuSection.rows.push({ key: `pass:${group.key}:${name}`, label, value: value(`${formatMs(time)} · ${formatShare(time, sum)}${each}`, 'num', time), depth: group.effect ? 2 : 1 });
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
} else {
|
|
956
|
+
const off = gpu.supported && !gpu.enabled;
|
|
957
|
+
gpuSection.rows.push({
|
|
958
|
+
key: 'none',
|
|
959
|
+
label: 'Timings',
|
|
960
|
+
value: off ? { ...note(gpuNote(gpu)), actions: [{ text: 'Turn on', title: 'Turn the GPU profiler on', run: gpu.enable }] } : note(gpuNote(gpu))
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
sections.push(gpuSection);
|
|
964
|
+
|
|
965
|
+
const work = makeSection('work', 'Rendering');
|
|
966
|
+
const count = (key, label, depth = 0) => {
|
|
967
|
+
const n = d[key][s];
|
|
968
|
+
work.rows.push({ key, label, value: Number.isFinite(n) ? value(formatCount(n), 'num', n) : note('counted by the profiler and debug engines'), depth });
|
|
969
|
+
};
|
|
970
|
+
count('draws', 'Draw calls');
|
|
971
|
+
if (capture.detailed) {
|
|
972
|
+
count('drawsForward', 'Forward', 1);
|
|
973
|
+
count('drawsShadow', 'Shadow', 1);
|
|
974
|
+
count('drawsMisc', 'Other', 1);
|
|
975
|
+
count('drawsSkinned', 'Skinned', 1);
|
|
976
|
+
count('primitives', 'Primitives');
|
|
977
|
+
count('materialSwitches', 'Material switches');
|
|
978
|
+
count('shaderSwitches', 'Shader switches');
|
|
979
|
+
count('cameras', 'Cameras');
|
|
980
|
+
count('shadowUpdates', 'Shadow map updates');
|
|
981
|
+
count('clusterCount', 'Light clusters');
|
|
982
|
+
} else {
|
|
983
|
+
work.rows.push({ key: 'details', label: 'Details', value: note('counted by the profiler and debug engines') });
|
|
984
|
+
}
|
|
985
|
+
if (Number.isFinite(d.dispatches[s]) && (d.dispatches[s] || d.computePasses[s])) {
|
|
986
|
+
count('dispatches', 'Compute dispatches');
|
|
987
|
+
count('computePasses', 'Compute passes');
|
|
988
|
+
}
|
|
989
|
+
if (d.splats[s] > 0) count('splats', 'Splats');
|
|
990
|
+
sections.push(work);
|
|
991
|
+
|
|
992
|
+
const memory = makeSection('memory', 'Memory');
|
|
993
|
+
const bytes = (key, label, depth = 0) => {
|
|
994
|
+
const n = d[key][s];
|
|
995
|
+
if (Number.isFinite(n)) memory.rows.push({ key, label, value: value(formatBytes(n), 'num', n), depth });
|
|
996
|
+
};
|
|
997
|
+
bytes('vram', 'Video memory');
|
|
998
|
+
bytes('vramTex', 'Textures', 1);
|
|
999
|
+
bytes('vramVb', 'Vertex buffers', 1);
|
|
1000
|
+
bytes('vramIb', 'Index buffers', 1);
|
|
1001
|
+
bytes('vramUb', 'Uniform buffers', 1);
|
|
1002
|
+
bytes('vramSb', 'Storage buffers', 1);
|
|
1003
|
+
if (Number.isFinite(d.heap[s])) {
|
|
1004
|
+
const freed = prev('heap') - d.heap[s];
|
|
1005
|
+
const gc = freed > 0 ? ` (${formatBytes(freed)} less than the frame before)` : '';
|
|
1006
|
+
memory.rows.push({ key: 'heap', label: 'JavaScript heap', value: value(`${formatBytes(d.heap[s])}${gc}`, 'num', d.heap[s]) });
|
|
1007
|
+
} else {
|
|
1008
|
+
memory.rows.push({ key: 'heap', label: 'JavaScript heap', value: note('reported by Chromium browsers only') });
|
|
1009
|
+
}
|
|
1010
|
+
sections.push(memory);
|
|
1011
|
+
|
|
1012
|
+
const resources = makeSection('resources', 'Resources');
|
|
1013
|
+
const resource = (key, label) => {
|
|
1014
|
+
if (Number.isFinite(d[key][s])) resources.rows.push({ key, label, value: countChange(d[key][s], prev(key)) });
|
|
1015
|
+
};
|
|
1016
|
+
resource('textures', 'Textures');
|
|
1017
|
+
resource('targets', 'Render targets');
|
|
1018
|
+
resource('shaders', 'Shaders');
|
|
1019
|
+
resource('buffers', 'Buffers');
|
|
1020
|
+
resource('pipelines', 'Pipelines');
|
|
1021
|
+
sections.push(resources);
|
|
1022
|
+
|
|
1023
|
+
// the same events the rows under the charts mark, each kind with a line per event
|
|
1024
|
+
const events = makeSection('events', 'Events');
|
|
1025
|
+
const mask = eventMask(capture, seq);
|
|
1026
|
+
EVENT_KINDS.forEach((kind) => {
|
|
1027
|
+
// the frame section above already says whether the frame is a hitch
|
|
1028
|
+
if (kind.key === 'hitch' || !(mask & EVENT_BITS[kind.key])) return;
|
|
1029
|
+
eventLines(capture, seq, kind.key).forEach((line, n) => {
|
|
1030
|
+
const described = value(line, kind.key === 'long' ? 'err' : 'str');
|
|
1031
|
+
if (n === 0) described.swatch = kind.color;
|
|
1032
|
+
events.rows.push({ key: `${kind.key}:${n}`, label: n === 0 ? kind.name : '', value: described });
|
|
1033
|
+
});
|
|
1034
|
+
});
|
|
1035
|
+
if (events.rows.length === 0) events.rows.push({ key: 'none', label: 'Events', value: note('none') });
|
|
1036
|
+
sections.push(events);
|
|
1037
|
+
|
|
1038
|
+
return sections;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* The counters the app keeps in `app.stats.user`, as the frame shown left them, or over the
|
|
1043
|
+
* frames selected together their mean, 95th percentile and largest.
|
|
1044
|
+
*
|
|
1045
|
+
* @param {PerfSelection} selection - The selection.
|
|
1046
|
+
* @returns {PropertySection|null} The section, or null where the app set none of them.
|
|
1047
|
+
*/
|
|
1048
|
+
function countersSection(selection) {
|
|
1049
|
+
const { capture, seq, range } = selection;
|
|
1050
|
+
const names = capture.counterNames;
|
|
1051
|
+
if (!names.length) return null;
|
|
1052
|
+
const from = range ? Math.max(capture.first, range.from) : seq;
|
|
1053
|
+
const to = range ? Math.min(capture.end - 1, range.to) : seq;
|
|
1054
|
+
const section = makeSection('counters', range ? `Counters, ${capture.rangeLabel(from, to).toLowerCase()}` : 'Counters');
|
|
1055
|
+
names.forEach((name, id) => {
|
|
1056
|
+
const ring = capture.counterRings[id];
|
|
1057
|
+
/** @type {Described} */
|
|
1058
|
+
let described;
|
|
1059
|
+
if (range) {
|
|
1060
|
+
const stat = rangeStats(capture, ring, from, to);
|
|
1061
|
+
if (!stat.count) return;
|
|
1062
|
+
described = { text: `avg ${formatValue(stat.avg)} · p95 ${formatValue(stat.p95)} · max ${formatValue(stat.max)}`, cls: 'num', raw: stat };
|
|
1063
|
+
} else {
|
|
1064
|
+
const v = ring[capture.slot(seq)];
|
|
1065
|
+
if (!Number.isFinite(v)) return;
|
|
1066
|
+
described = value(formatValue(v), 'num', v);
|
|
1067
|
+
}
|
|
1068
|
+
described.swatch = scriptColorAt(id);
|
|
1069
|
+
section.rows.push({ key: `counter:${name}`, label: name, value: described });
|
|
1070
|
+
});
|
|
1071
|
+
return section.rows.length ? section : null;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* The script types of the frame shown, or of the frames selected together, each with its own time
|
|
1076
|
+
* and the color it is drawn in, the longest first.
|
|
1077
|
+
*
|
|
1078
|
+
* @param {PerfSelection} selection - The selection.
|
|
1079
|
+
* @returns {PropertySection} The section.
|
|
1080
|
+
*/
|
|
1081
|
+
function scriptsSection(selection) {
|
|
1082
|
+
const { capture, seq, range } = selection;
|
|
1083
|
+
const from = range ? Math.max(capture.first, range.from) : seq;
|
|
1084
|
+
const to = range ? Math.min(capture.end - 1, range.to) : seq;
|
|
1085
|
+
const section = makeSection('scripts', range ? `Scripts, ${capture.rangeLabel(from, to).toLowerCase()}` : 'Scripts');
|
|
1086
|
+
const d = capture.data;
|
|
1087
|
+
let timed = 0;
|
|
1088
|
+
for (let f = from; f <= to; f++) {
|
|
1089
|
+
if (capture.scriptFigures(f)) timed++;
|
|
1090
|
+
}
|
|
1091
|
+
if (!timed) {
|
|
1092
|
+
const enabled = !!selection.scripts?.enabled;
|
|
1093
|
+
const text = enabled ? 'not timed in this frame' : 'Script timing is off';
|
|
1094
|
+
const enable = selection.scripts?.enable;
|
|
1095
|
+
section.rows.push({ key: 'none', label: 'Timings', value: !enabled && enable ? { ...note(text), actions: [{ text: 'Turn on', title: 'Time each script type while recording', run: enable }] } : note(text) });
|
|
1096
|
+
return section;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
const names = capture.scriptNames;
|
|
1100
|
+
// the colors the Scripts chart draws the types in, and the way to give one a band of its own
|
|
1101
|
+
const view = selection.scripts;
|
|
1102
|
+
const pinned = view?.pinned ?? NO_PINS;
|
|
1103
|
+
const legend = scriptLegend(capture, range ? from : capture.first, range ? to : capture.end - 1, view?.rows ?? SCRIPT_ROWS, pinned);
|
|
1104
|
+
const colorOf = id => legend.colors.get(id) ?? OTHER_SCRIPTS_COLOR;
|
|
1105
|
+
const pinActions = (name) => {
|
|
1106
|
+
const pin = view?.pin;
|
|
1107
|
+
if (!pin) return undefined;
|
|
1108
|
+
return pinned.has(name) ?
|
|
1109
|
+
[{ text: 'Unpin', title: 'Take its own band in the Scripts chart away', run: () => pin(name) }] :
|
|
1110
|
+
[{ text: 'Pin', title: 'Give it a band of its own in the Scripts chart, whatever its time', run: () => pin(name) }];
|
|
1111
|
+
};
|
|
1112
|
+
if (range) {
|
|
1113
|
+
// each type's own time per frame, over the frames that timed scripts
|
|
1114
|
+
const total = rangeStats(capture, 'scriptsTotal', from, to);
|
|
1115
|
+
section.rows.push({ key: 'total', label: 'All scripts', value: { text: `avg ${formatMs(total.avg)} · p95 ${formatMs(total.p95)} · max ${formatMs(total.max)}`, cls: 'num', raw: total } });
|
|
1116
|
+
// one pass over the frames sums each type and finds its largest; the 95th percentile, which
|
|
1117
|
+
// sorts a type's frames, only for the types listed, as an app can have hundreds
|
|
1118
|
+
const sums = new Float64Array(names.length);
|
|
1119
|
+
const maxes = new Float64Array(names.length);
|
|
1120
|
+
const calls = new Float64Array(names.length);
|
|
1121
|
+
for (let f = from; f <= to; f++) {
|
|
1122
|
+
const times = capture.scriptFigures(f);
|
|
1123
|
+
if (!times) continue;
|
|
1124
|
+
for (let base = 0, id = 0; base < times.length; base += SCRIPT_STRIDE, id++) {
|
|
1125
|
+
const v = times[base] + times[base + 1] + times[base + 2];
|
|
1126
|
+
sums[id] += v;
|
|
1127
|
+
if (v > maxes[id]) maxes[id] = v;
|
|
1128
|
+
calls[id] += times[base + SCRIPT_CALLS] + times[base + SCRIPT_CALLS + 1] + times[base + SCRIPT_CALLS + 2];
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
const ids = [];
|
|
1132
|
+
for (let id = 0; id < names.length; id++) {
|
|
1133
|
+
if (sums[id] > 0) ids.push(id);
|
|
1134
|
+
}
|
|
1135
|
+
ids.sort((a, b) => sums[b] - sums[a]);
|
|
1136
|
+
for (const id of ids.slice(0, MAX_SCRIPT_ROWS)) {
|
|
1137
|
+
let n = 0;
|
|
1138
|
+
for (let f = from; f <= to && n < SCRATCH.length; f++) {
|
|
1139
|
+
const v = capture.scriptTime(f, id);
|
|
1140
|
+
if (Number.isFinite(v)) SCRATCH[n++] = v;
|
|
1141
|
+
}
|
|
1142
|
+
const type = { avg: sums[id] / timed, p95: quantile(SCRATCH.subarray(0, n).sort(), 0.95), max: maxes[id], calls: calls[id] / timed };
|
|
1143
|
+
const described = value(`avg ${formatMs(type.avg)} · p95 ${formatMs(type.p95)} · max ${formatMs(type.max)} · ${callCount(type.calls)} a frame`, 'num', type);
|
|
1144
|
+
described.swatch = colorOf(id);
|
|
1145
|
+
described.actions = pinActions(names[id]);
|
|
1146
|
+
section.rows.push({ key: `type:${names[id]}`, label: names[id], value: described, depth: 1 });
|
|
1147
|
+
}
|
|
1148
|
+
if (ids.length > MAX_SCRIPT_ROWS) section.rows.push({ key: 'more', label: '', value: note(`and ${ids.length - MAX_SCRIPT_ROWS} more`), depth: 1 });
|
|
1149
|
+
} else {
|
|
1150
|
+
const s = capture.slot(seq);
|
|
1151
|
+
const interval = d.interval[s];
|
|
1152
|
+
section.rows.push({ key: 'total', label: 'All scripts', value: timing(d.scriptsTotal[s], interval) });
|
|
1153
|
+
const calls = d.scriptCalls[s];
|
|
1154
|
+
section.rows.push({ key: 'calls', label: 'Calls', value: value(formatCount(calls), 'num', calls) });
|
|
1155
|
+
if (d.scriptsInit[s] > 0) section.rows.push({ key: 'init', label: 'Initializing', value: timing(d.scriptsInit[s], interval) });
|
|
1156
|
+
const types = capture.scripts(seq);
|
|
1157
|
+
for (const type of types.slice(0, MAX_SCRIPT_ROWS)) {
|
|
1158
|
+
const parts = [['update', type.update, type.updateCalls], ['post-update', type.post, type.postCalls], ['initialize', type.init, type.initCalls]];
|
|
1159
|
+
const detail = parts.filter(([, , n]) => n > 0).map(([name, ms, n]) => `${name} ${formatMs(ms)} in ${callCount(n)}`).join(' · ');
|
|
1160
|
+
const own = type.update + type.post + type.init;
|
|
1161
|
+
const described = value(`${formatMs(own)} · ${detail}`, 'num', own);
|
|
1162
|
+
described.swatch = colorOf(type.id);
|
|
1163
|
+
described.actions = pinActions(type.name);
|
|
1164
|
+
section.rows.push({ key: `type:${type.name}`, label: type.name, value: described, depth: 1 });
|
|
1165
|
+
}
|
|
1166
|
+
if (types.length > MAX_SCRIPT_ROWS) section.rows.push({ key: 'more', label: '', value: note(`and ${types.length - MAX_SCRIPT_ROWS} more`), depth: 1 });
|
|
1167
|
+
if (d.scriptCost[s] > 0) {
|
|
1168
|
+
const cost = d.scriptCost[s] < 0.01 ? 'under 0.01 ms' : `about ${formatMs(d.scriptCost[s])}`;
|
|
1169
|
+
section.rows.push({ key: 'cost', label: 'Timing them', value: value(`${cost}, counted as the inspector's time`, 'num', d.scriptCost[s]) });
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
if (capture.clockStep > COARSE_CLOCK) {
|
|
1173
|
+
section.rows.push({ key: 'clock', label: 'Precision', value: note(`this page's clock steps by ${formatMs(capture.clockStep)}, so a short call reads as nothing or a whole step: small scripts read best over many frames`) });
|
|
1174
|
+
}
|
|
1175
|
+
return section;
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
/**
|
|
1179
|
+
* @param {string} name - The profiler name of the pass running an effect, as FramePassBloom.
|
|
1180
|
+
* @returns {string} The name of the effect, as Bloom.
|
|
1181
|
+
*/
|
|
1182
|
+
function effectName(name) {
|
|
1183
|
+
return name.startsWith('FramePass') && name.length > 9 ? name.slice(9) : name;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
/**
|
|
1187
|
+
* @param {PerfGpuPass[]} passes - The GPU passes of a frame.
|
|
1188
|
+
* @returns {{ key: string, effect: string|null, time: number, passes: Map<string, number[]> }[]}
|
|
1189
|
+
* Each effect with the passes it runs by name, and the passes of their own by name, longest first.
|
|
1190
|
+
*/
|
|
1191
|
+
function gpuGroups(passes) {
|
|
1192
|
+
const groups = new Map();
|
|
1193
|
+
for (const pass of passes) {
|
|
1194
|
+
const key = pass.owner ? `${pass.owner}:${pass.ownerOccurrence}` : `:${pass.name}`;
|
|
1195
|
+
let group = groups.get(key);
|
|
1196
|
+
if (!group) groups.set(key, group = { key, effect: pass.owner, time: 0, passes: new Map() });
|
|
1197
|
+
group.time += pass.time;
|
|
1198
|
+
let times = group.passes.get(pass.name);
|
|
1199
|
+
if (!times) group.passes.set(pass.name, times = []);
|
|
1200
|
+
times.push(pass.time);
|
|
1201
|
+
}
|
|
1202
|
+
const total = times => times.reduce((sum, t) => sum + t, 0);
|
|
1203
|
+
const sorted = [...groups.values()].sort((a, b) => b.time - a.time);
|
|
1204
|
+
for (const group of sorted) group.passes = new Map([...group.passes].sort((a, b) => total(b[1]) - total(a[1])));
|
|
1205
|
+
return sorted;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* @param {PerfCapture} capture - The capture.
|
|
1210
|
+
* @param {PerfSelection['gpu']} gpu - The GPU profiler.
|
|
1211
|
+
* @returns {string} Why a frame has no GPU timings.
|
|
1212
|
+
*/
|
|
1213
|
+
function gpuNote(gpu) {
|
|
1214
|
+
if (!gpu.supported) return 'this device has no GPU profiler';
|
|
1215
|
+
if (!gpu.enabled) return 'GPU timings are off';
|
|
1216
|
+
return 'not measured for this frame';
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
export {
|
|
1220
|
+
ASSET_COLOR, ASSET_COLORS, ASSET_KINDS, CPU_PARTS, EVENT_KINDS, FAILED_COLOR, HITCH_FACTOR, MAIN_THREAD_COLOR, MILESTONE_COLOR, OTHER_SCRIPTS_COLOR,
|
|
1221
|
+
RENDER_PHASES, REQUEST_COLOR, SCRIPT_COLORS, SCRIPT_ROWS, buildPerfModel, callCount, captureNorms, effectName, eventLines,
|
|
1222
|
+
formatDuration, eventMask, eventUnsupported, formatCount, formatMs, formatValue, frameVerdict, isHitch, loadColor, loadText, previousSlot, rangeStats,
|
|
1223
|
+
scriptColorAt, scriptLegend
|
|
1224
|
+
};
|