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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
!!! THIS FILE IS COPIED FROM https://github.com/fitzgen/servo-trace-dump !!!
Make sure to upstream changes, or they will get lost!
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
"use strict";
(function (exports, window) {
/*** State ******************************************************************/
const COLORS = exports.COLORS = [
"#0088cc",
"#5b5fff",
"#b82ee5",
"#ed2655",
"#f13c00",
"#d97e00",
"#2cbb0f",
"#0072ab",
];
// A class containing the cleaned up trace state.
const State = exports.State = (function () {
return class State {
constructor(rawTraces, windowWidth) {
// The traces themselves.
this.traces = null;
// Only display traces that take at least this long. Default is .1 ms.
this.minimumTraceTime = 100000;
// Maximimum and minimum times seen in traces. These get normalized to be
// relative to 0, so after initialization minTime is always 0.
this.minTime = Infinity;
this.maxTime = 0;
// The current start and end of the viewport selection.
this.startSelection = 0;
this.endSelection = 0;
// The current width of the window.
this.windowWidth = windowWidth;
// Whether the user is actively grabbing the left or right grabby, or the
// viewport slider.
this.grabbingLeft = false;
this.grabbingRight = false;
this.grabbingSlider = false;
// Maps category labels to a persistent color so that they are always
// rendered the same color.
this.colorIndex = 0;
this.categoryToColor = Object.create(null);
this.initialize(rawTraces);
}
// Clean up and massage the trace data.
initialize(rawTraces) {
this.traces = rawTraces.filter(t => t.endTime - t.startTime >= this.minimumTraceTime);
this.traces.sort((t1, t2) => {
let cmp = t1.startTime - t2.startTime;
if (cmp !== 0) {
return cmp;
}
return t1.endTime - t2.endTime;
});
this.findMinTime();
this.normalizeTimes();
this.removeIdleTime();
this.findMaxTime();
this.startSelection = 3 * this.maxTime / 8;
this.endSelection = 5 * this.maxTime / 8;
}
// Find the minimum timestamp.
findMinTime() {
this.minTime = this.traces.reduce((min, t) => Math.min(min, t.startTime),
Infinity);
}
// Find the maximum timestamp.
findMaxTime() {
this.maxTime = this.traces.reduce((max, t) => Math.max(max, t.endTime),
0);
}
// Normalize all times to be relative to the minTime and then reset the
// minTime to 0.
normalizeTimes() {
for (let i = 0; i < this.traces.length; i++) {
let trace = this.traces[i];
trace.startTime -= this.minTime;
trace.endTime -= this.minTime;
}
this.minTime = 0;
}
// Remove idle time between traces. It isn't useful to see and makes
// visualizing the data more difficult.
removeIdleTime() {
let totalIdleTime = 0;
let lastEndTime = null;
for (let i = 0; i < this.traces.length; i++) {
let trace = this.traces[i];
if (lastEndTime !== null && trace.startTime > lastEndTime) {
totalIdleTime += trace.startTime - lastEndTime;
}
lastEndTime = trace.endTime;
trace.startTime -= totalIdleTime;
trace.endTime -= totalIdleTime;
}
}
// Get the color for the given category, or assign one if no such color
// exists yet.
getColorForCategory(category) {
let result = this.categoryToColor[category];
if (!result) {
result = COLORS[this.colorIndex++ % COLORS.length];
this.categoryToColor[category] = result;
}
return result;
}
// Translate pixels into nanoseconds.
pxToNs(px) {
return px / this.windowWidth * this.maxTime;
}
// Translate nanoseconds into pixels.
nsToPx(ns) {
return ns / this.maxTime * this.windowWidth
}
// Translate nanoseconds into pixels in the zoomed viewport region.
nsToSelectionPx(ns) {
return ns / (this.endSelection - this.startSelection) * this.windowWidth;
}
// Update the start selection to the given position's time.
updateStartSelection(position) {
this.startSelection = clamp(this.pxToNs(position),
this.minTime,
this.endSelection);
}
// Update the end selection to the given position's time.
updateEndSelection(position) {
this.endSelection = clamp(this.pxToNs(position),
this.startSelection,
this.maxTime);
}
// Move the start and end selection by the given delta movement.
moveSelection(movement) {
let delta = clamp(this.pxToNs(movement),
-this.startSelection,
this.maxTime - this.endSelection);
this.startSelection += delta;
this.endSelection += delta;
}
// Widen or narrow the selection based on the given zoom.
zoomSelection(zoom) {
const increment = this.maxTime / 1000;
this.startSelection = clamp(this.startSelection - zoom * increment,
this.minTime,
this.endSelection);
this.endSelection = clamp(this.endSelection + zoom * increment,
this.startSelection,
this.maxTime);
}
// Get the set of traces that overlap the current selection.
getTracesInSelection() {
const tracesInSelection = [];
for (let i = 0; i < state.traces.length; i++) {
let trace = state.traces[i];
if (trace.endTime < state.startSelection) {
continue;
}
if (trace.startTime > state.endSelection) {
break;
}
tracesInSelection.push(trace);
}
return tracesInSelection;
}
};
}());
/*** Utilities **************************************************************/
// Ensure that min <= value <= max
const clamp = exports.clamp = (value, min, max) => {
return Math.max(Math.min(value, max), min);
};
// Get the closest power of ten to the given number.
const closestPowerOfTen = exports.closestPowerOfTen = n => {
let powerOfTen = 1;
let diff = Math.abs(n - powerOfTen);
while (true) {
let nextPowerOfTen = powerOfTen * 10;
let nextDiff = Math.abs(n - nextPowerOfTen);
if (nextDiff > diff) {
return powerOfTen;
}
diff = nextDiff;
powerOfTen = nextPowerOfTen;
}
};
// Select the tick increment for the given range size and maximum number of
// ticks to show for that range.
const selectIncrement = exports.selectIncrement = (range, maxTicks) => {
let increment = closestPowerOfTen(range / 10);
while (range / increment > maxTicks) {
increment *= 2;
}
return increment;
};
/*** Window Specific Code ***************************************************/
if (!window) {
return;
}
// XXX: Everything below here relies on the presence of `window`! Try to
// minimize this code and factor out the parts that don't explicitly need
// `window` or `document` as much as possible. We can't easily test code that
// relies upon `window`.
const state = exports.state = new State(window.TRACES, window.innerWidth);
/*** Initial Persistent Element Creation ************************************/
window.document.body.innerHTML = "";
const sliderContainer = window.document.createElement("div");
sliderContainer.id = "slider";
window.document.body.appendChild(sliderContainer);
const leftGrabby = window.document.createElement("span");
leftGrabby.className = "grabby";
sliderContainer.appendChild(leftGrabby);
const sliderViewport = window.document.createElement("span");
sliderViewport.id = "slider-viewport";
sliderContainer.appendChild(sliderViewport);
const rightGrabby = window.document.createElement("span");
rightGrabby.className = "grabby";
sliderContainer.appendChild(rightGrabby);
const tracesContainer = window.document.createElement("div");
tracesContainer.id = "traces";
window.document.body.appendChild(tracesContainer);
/*** Listeners *************************************************************/
// Run the given function and render afterwards.
const withRender = fn => function () {
fn.apply(null, arguments);
render();
};
window.addEventListener("resize", withRender(() => {
state.windowWidth = window.innerWidth;
}));
window.addEventListener("mouseup", () => {
state.grabbingSlider = state.grabbingLeft = state.grabbingRight = false;
});
leftGrabby.addEventListener("mousedown", () => {
state.grabbingLeft = true;
});
rightGrabby.addEventListener("mousedown", () => {
state.grabbingRight = true;
});
sliderViewport.addEventListener("mousedown", () => {
state.grabbingSlider = true;
});
window.addEventListener("mousemove", event => {
if (state.grabbingSlider) {
state.moveSelection(event.movementX);
event.preventDefault();
render();
} else if (state.grabbingLeft) {
state.updateStartSelection(event.clientX);
event.preventDefault();
render();
} else if (state.grabbingRight) {
state.updateEndSelection(event.clientX);
event.preventDefault();
render();
}
});
sliderContainer.addEventListener("wheel", withRender(event => {
state.zoomSelection(event.deltaY);
}));
/*** Rendering **************************************************************/
// Create a function that calls the given function `fn` only once per animation
// frame.
const oncePerAnimationFrame = fn => {
let animationId = null;
return () => {
if (animationId !== null) {
return;
}
animationId = window.requestAnimationFrame(() => {
fn();
animationId = null;
});
};
};
// Only call the given function once per window width resize.
const oncePerWindowWidth = fn => {
let lastWidth = null;
return () => {
if (state.windowWidth !== lastWidth) {
fn();
lastWidth = state.windowWidth;
}
};
};
// Top level entry point for rendering. Renders the current `window.state`.
const render = oncePerAnimationFrame(() => {
renderSlider();
renderTraces();
});
// Render the slider at the top of the screen.
const renderSlider = () => {
let selectionDelta = state.endSelection - state.startSelection;
// -6px because of the 3px width of each grabby.
sliderViewport.style.width = state.nsToPx(selectionDelta) - 6 + "px";
leftGrabby.style.marginLeft = state.nsToPx(state.startSelection) + "px";
rightGrabby.style.rightMargin = state.nsToPx(state.maxTime - state.endSelection) + "px";
renderSliderTicks();
};
// Render the ticks along the slider overview.
const renderSliderTicks = oncePerWindowWidth(() => {
let oldTicks = Array.from(window.document.querySelectorAll(".slider-tick"));
for (let tick of oldTicks) {
tick.remove();
}
let increment = selectIncrement(state.maxTime, 20);
let px = state.nsToPx(increment);
let ms = 0;
for (let i = 0; i < state.windowWidth; i += px) {
let tick = window.document.createElement("div");
tick.className = "slider-tick";
tick.textContent = ms + " ms";
tick.style.left = i + "px";
window.document.body.appendChild(tick);
ms += increment / 1000000;
}
});
// Render the individual traces.
const renderTraces = () => {
renderTracesTicks();
let tracesToRender = state.getTracesInSelection();
// Ensure that we have exactly enough trace row elements. If we have more
// elements than traces we are going to render, then remove some. If we have
// fewer elements than traces we are going to render, then add some.
let rows = Array.from(tracesContainer.querySelectorAll(".outer"));
while (rows.length > tracesToRender.length) {
rows.pop().remove();
}
while (rows.length < tracesToRender.length) {
let elem = makeTraceTemplate();
tracesContainer.appendChild(elem);
rows.push(elem);
}
for (let i = 0; i < tracesToRender.length; i++) {
renderTrace(tracesToRender[i], rows[i]);
}
};
// Render the ticks behind the traces.
const renderTracesTicks = () => {
let oldTicks = Array.from(tracesContainer.querySelectorAll(".traces-tick"));
for (let tick of oldTicks) {
tick.remove();
}
let selectionDelta = state.endSelection - state.startSelection;
let increment = selectIncrement(selectionDelta, 10);
let px = state.nsToPx(increment);
let offset = state.startSelection % increment;
let time = state.startSelection - offset + increment;
while (time < state.endSelection) {
let tick = document.createElement("div");
tick.className = "traces-tick";
tick.textContent = Math.round(time / 1000000) + " ms";
tick.style.left = state.nsToSelectionPx(time - state.startSelection) + "px";
tracesContainer.appendChild(tick);
time += increment;
}
};
// Create the DOM structure for an individual trace.
const makeTraceTemplate = () => {
let outer = window.document.createElement("div");
outer.className = "outer";
let inner = window.document.createElement("div");
inner.className = "inner";
let tooltip = window.document.createElement("div");
tooltip.className = "tooltip";
let header = window.document.createElement("h3");
header.className = "header";
tooltip.appendChild(header);
let duration = window.document.createElement("h4");
duration.className = "duration";
tooltip.appendChild(duration);
let pairs = window.document.createElement("dl");
let timeStartLabel = window.document.createElement("dt");
timeStartLabel.textContent = "Start:"
pairs.appendChild(timeStartLabel);
let timeStartValue = window.document.createElement("dd");
timeStartValue.className = "start";
pairs.appendChild(timeStartValue);
let timeEndLabel = window.document.createElement("dt");
timeEndLabel.textContent = "End:"
pairs.appendChild(timeEndLabel);
let timeEndValue = window.document.createElement("dd");
timeEndValue.className = "end";
pairs.appendChild(timeEndValue);
let urlLabel = window.document.createElement("dt");
urlLabel.textContent = "URL:";
pairs.appendChild(urlLabel);
let urlValue = window.document.createElement("dd");
urlValue.className = "url";
pairs.appendChild(urlValue);
let iframeLabel = window.document.createElement("dt");
iframeLabel.textContent = "iframe?";
pairs.appendChild(iframeLabel);
let iframeValue = window.document.createElement("dd");
iframeValue.className = "iframe";
pairs.appendChild(iframeValue);
let incrementalLabel = window.document.createElement("dt");
incrementalLabel.textContent = "Incremental?";
pairs.appendChild(incrementalLabel);
let incrementalValue = window.document.createElement("dd");
incrementalValue.className = "incremental";
pairs.appendChild(incrementalValue);
tooltip.appendChild(pairs);
outer.appendChild(tooltip);
outer.appendChild(inner);
return outer;
};
// Render `trace` into the given `elem`. We reuse the trace elements and modify
// them with the new trace that will populate this particular `elem` rather than
// clearing the DOM out and rebuilding it from scratch. Its a bit of a
// performance win when there are a lot of traces being rendered. Funnily
// enough, iterating over the complete set of traces hasn't been a performance
// problem at all and the bottleneck seems to be purely rendering the subset of
// traces we wish to show.
const renderTrace = (trace, elem) => {
let inner = elem.querySelector(".inner");
inner.style.width = state.nsToSelectionPx(trace.endTime - trace.startTime) + "px";
inner.style.marginLeft = state.nsToSelectionPx(trace.startTime - state.startSelection) + "px";
let category = trace.category;
inner.textContent = category;
inner.style.backgroundColor = state.getColorForCategory(category);
let header = elem.querySelector(".header");
header.textContent = category;
let duration = elem.querySelector(".duration");
duration.textContent = (trace.endTime - trace.startTime) / 1000000 + " ms";
let timeStartValue = elem.querySelector(".start");
timeStartValue.textContent = trace.startTime / 1000000 + " ms";
let timeEndValue = elem.querySelector(".end");
timeEndValue.textContent = trace.endTime / 1000000 + " ms";
if (trace.metadata) {
let urlValue = elem.querySelector(".url");
urlValue.textContent = trace.metadata.url;
urlValue.removeAttribute("hidden");
let iframeValue = elem.querySelector(".iframe");
iframeValue.textContent = trace.metadata.iframe.RootWindow ? "No" : "Yes";
iframeValue.removeAttribute("hidden");
let incrementalValue = elem.querySelector(".incremental");
incrementalValue.textContent = trace.metadata.incremental.Incremental ? "Yes" : "No";
incrementalValue.removeAttribute("hidden");
} else {
elem.querySelector(".url").setAttribute("hidden", "");
elem.querySelector(".iframe").setAttribute("hidden", "");
elem.querySelector(".incremental").setAttribute("hidden", "");
}
};
render();
}(typeof exports === "object" ? exports : window,
typeof window === "object" ? window : null));
|