Debian Codegraph @

browsertests/testdata/testflame.js

raw symbols SCIP
1 function TestFlame() {
2 const PADDING = 2; // Matches PADDING in stackViewer
3
4 const test = new TestFixture();
5 const chart = document.getElementById("stack-chart");
6 if (!chart) {
7 test.err("could not find stack-chart");
8 return;
9 }
10 const chartRect = chart.getBoundingClientRect();
11
12 // Create map from box text to DOM element.
13 // TODO: Generalize to support multiple boxes for a given piece of text.
14 let boxMap;
15 fetchBoxes();
16 function fetchBoxes() {
17 boxMap = new Map();
18 const boxes = document.querySelectorAll(".boxbg");
19 for (let box of boxes) {
20 const text = box.innerText;
21 boxMap.set(text, box);
22 }
23 }
24
25 // rect gets the bounding box for box with text t.
26 function rect(t) {
27 const elem = boxMap.get(t);
28 if (!elem) {
29 test.err("did not find", t);
30 return null;
31 }
32 return elem.getBoundingClientRect();
33 }
34
35 // checkCalls checks that box with text a is positioned w.r.t. box with
36 // text b to indicate a call from a to b. Expect a gap of the specified
37 // number of rows.
38 function checkCalls(a, b, gap = 0) {
39 test.setContext("checkCalls", a, b, gap);
40 const ra = rect(a);
41 const rb = rect(b);
42 if (!ra || !rb) return;
43
44 const pixelGap = gap * rb.height;
45 if (rb.top != ra.bottom + pixelGap) {
46 test.err("not above");
47 }
48 // TODO: Allow checking boxes above pivots.
49 if (rb.left < ra.left || rb.right > ra.right) {
50 test.err("horizontal span of", a, "is not nested inside horizontal span of", b);
51 }
52 }
53
54 // checkWidth checks that the width of the box with text t is approximately
55 // the specified fraction of the total width.
56 function checkWidth(t, fraction) {
57 test.setContext("checkWidth", t, fraction);
58 const r = rect(t);
59 if (!r) return;
60 const expect = (chartRect.width - 2*PADDING) * fraction;
61 if (r.width < expect*0.95 || r.width > expect*1.05) {
62 test.err("bad width", r.width, "expecting ~", expect);
63 }
64 }
65
66 // Fake profile has the following stacks:
67 // 100 F1 F2 F3
68 // 200 F1 F2
69 test.run("initial", function() {
70 checkCalls("root", "F1");
71 checkCalls("F1", "F2");
72 checkCalls("F2", "F3");
73 checkWidth("root", 300/300);
74 checkWidth("F1", 300/300);
75 checkWidth("F2", 300/300);
76 checkWidth("F3", 100/300);
77 });
78
79 test.run("Pivot F3", function() {
80 boxMap.get("F3").click();
81 fetchBoxes();
82 checkCalls("root", "F1");
83 checkCalls("F1", "F2");
84 checkCalls("F2", "F3", 1);
85 checkWidth("root", 100/100);
86 checkWidth("F1", 100/100);
87 checkWidth("F2", 100/100);
88 checkWidth("F3", 100/100);
89 });
90
91 test.run("NavigateWithoutPivot", function() {
92 // Clear pivot
93 boxMap.get("root").click();
94
95 // Trigger link update.
96 const btn = document.getElementById("graphbtn");
97 if (!btn) {
98 test.err("no graph button on page");
99 return;
100 }
101 const event = new Event("mouseenter");
102 btn.dispatchEvent(event);
103
104 // Check that URL does not contain a focus parameter.
105 test.log(btn.href);
106 const url = new URL(btn.href);
107 if (url.searchParams.has('f')) {
108 test.err("unexpected focus parameter in URL", btn.href);
109 }
110 });
111
112 test.run("Units", function() {
113 function checkUnitText(unit, v, expect) {
114 const result = pprofUnitText(v, unit);
115 if (result != expect) {
116 test.err("bad text for", v, unit, ":", result, "expecting:", expect);
117 }
118 }
119
120 // Time units, plus logic tests.
121 checkUnitText("s", 0.51e-9, "0.51ns");
122 checkUnitText("s", 3e-9, "3ns");
123 checkUnitText("s", 1.23e-6, "1.23us");
124 checkUnitText("s", 0.04, "40ms");
125 checkUnitText("s", 1, "1s");
126 checkUnitText("s", 3599, "3599s");
127 checkUnitText("s", 3600, "1hrs");
128
129 // Sanity check for byte units.
130 checkUnitText("B", 2*1048576, "2MB");
131
132 // Unknown unit.
133 checkUnitText("cm", 100, "100cm");
134 });
135
136 return test.result;
137 }
138 TestFlame();