-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprofiler.js
More file actions
521 lines (430 loc) · 15.6 KB
/
Copy pathprofiler.js
File metadata and controls
521 lines (430 loc) · 15.6 KB
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
// GLOBAL STATE
let calls = {};
let deadBeat = {};
let idMap = {};
let aggregates = {};
let processedBatches = 0;
const renderEvery = 5;
let currentRoot = null;
// processing
function finalizeProcessing() {
const forest = buildForest(calls);
renderForest(forest);
renderTimeline(Object.values(calls));
renderSummary(aggregates);
addToFlamegraph(forest);
renderFlamegraph(flamegraphRoot);
if (forest) showFunctionDetails(forest[0]);
}
function maybeRender() {
if (processedBatches % renderEvery === 0) {
requestSummaryRender();
renderForestSnapshot = buildForest(calls);
requestTreeRender(renderForestSnapshot);
if (flamegraphActive){ addToFlamegraph(renderForestSnapshot); requestFlamegraphRender();}
// renderTimeline(Object.values(calls));
}
}
function processBatch(batch) {
for (const entry of batch) {
const id = entry.call_id;
var cCall;
// Create or update call
if (!calls[id]) {
calls[id] = {
call_id: id,
parent_call_id: entry.parent_call_id,
function: entry.function,
type: entry.is_async == 1 ? 'async' : 'sync',
children: new Set(),
expanded: false,
start: entry.start,
duration: entry.duration
};
}
cCall = calls[id];
// update aggregates
if (!aggregates[entry.function]) {
aggregates[entry.function] = { count: 0, total: 0 };
}
aggregates[entry.function].count += 1;
aggregates[entry.function].total += entry.duration;
// Link to parent and child
if (cCall.parent_call_id) {
if (!calls[cCall.parent_call_id]) {
if (!deadBeat[cCall.parent_call_id])
deadBeat[cCall.parent_call_id] = [];
deadBeat[cCall.parent_call_id].push(cCall.call_id)
} else {
calls[cCall.parent_call_id].children.add(cCall.call_id);
}
}
if (deadBeat[cCall.call_id]) {
deadBeat[cCall.call_id].forEach(element => {
cCall.children.add(element);
});
deadBeat[cCall.call_id] = null;
}
}
processedBatches++;
}
// details handler ===============================================================================================================================================================================================================
function showFunctionDetails(data) {
let html = "<table class='profiler-table'>";
html += "<thead><tr><th colspan='2'>Function Details</th></tr></thead><tbody>";
html += `<tr><td>Function:</td><td>${data.function}</td></tr>`;
html += `<tr><td>Call ID:</td><td>${data.call_id}</td></tr>`;
if (data.parent_call_id && calls[data.parent_call_id]) {
html += `<tr><td>Parent:</td><td>${calls[data.parent_call_id].function} (${data.parent_call_id})</td></tr>`;
}
html += `<tr><td>Start:</td><td>${data.start || "missing"}</td></tr>`;
html += `<tr><td>End:</td><td>${data.start + data.duration || "missing"}</td></tr>`;
html += `<tr><td>Duration:</td><td>${data.duration || "missing"} ms</td></tr>`;
html += "</tbody></table>";
document.getElementById("functionInfo").innerHTML = html;
}
// timeline handler ===============================================================================================================================================================================================================
const MAX_TIMELINE = 5000;
function renderTimeline(callArray) {
const limited = callArray.slice(0, MAX_TIMELINE);
const bars = limited.map(call => {
const startSec = call.start / 1000
const endSec = (call.start + call.duration) / 1000;
const durationSec = call.duration / 1000;
if (durationSec < 1)
t = `<br>Duration: ${durationSec* 1000} ms</br><extra></extra>`
else
t = `<br>Duration: ${durationSec} s</br><extra></extra>`
return {
x: [startSec, endSec],
y: [call.function + ` [${call.call_id}]`, call.function + ` [${call.call_id}]`],
mode: 'lines',
type: 'scatter',
name: `${call.function} [${call.call_id}]`,
line: { width: 15 }, // increased from
call_id: call.call_id,
hovertemplate: t
};
});
Plotly.newPlot('timeline', bars, {
title: 'Global Timeline',
xaxis: {
title: 'Time (s)',
tickformat: '.1f',
zeroline: false
},
paper_bgcolor: '#1e1e1e',
plot_bgcolor: '#1e1e1e',
font: { color: '#dcdcdc' },
height: document.getElementById("timeline").clientHeight,
hovermode: 'closest'
});
}
// Summary handler ===============================================================================================================================================================================================================
let summaryLastRender = 0;
const summaryRenderInterval = 100;
function requestSummaryRender() {
const now = performance.now();
if (now - summaryLastRender >= summaryRenderInterval) {
summaryLastRender = now;
renderSummary(aggregates);
}
}
function renderSummary(aggregates) {
let html = "<table class='profiler-table'>";
html += "<thead><tr><th>Function</th><th>Calls</th><th>Total Time (ms)</th></tr></thead><tbody>";
const sorted = Object.entries(aggregates).sort((a, b) => b[1].total - a[1].total);
for (const [func, stat] of sorted) {
html += `<tr><td>${func}</td><td>${stat.count}</td><td>${stat.total}</td></tr>`;
}
html += "</tbody></table>";
document.getElementById("summaryTable").innerHTML = html;
}
// Flame graph handler ===============================================================================================================================================================================================================
let flamegraphLastRender = 0;
const flamegraphRenderInterval = 1000;
let resizeObserver;
let flamegraphRenderTimeout = null;
let flamegraphSVG = null;
let flamegraphG = null;
let flamegraphPendingRender = false;
let flamegraphRoot = { name: "__ROOT__", value: 0, children: [] };
const color = d3.scaleOrdinal(d3.schemeTableau10);
function getNodePath(d) {
return d.ancestors().map(n => n.data.name).reverse().join("/");
}
function renderFlamegraph(data) {
const container = document.getElementById("flamegraphView");
const width = container.clientWidth;
const height = container.clientHeight;
if (width === 0 || height === 0) return;
const partition = d3.partition().size([width, height]);
const root = d3.hierarchy(data).sum(d => d.value);
partition(root);
// Save for semantic zoom
currentRoot = data;
const MIN_WIDTH = 0;
const nodes = root.descendants().filter(d => (d.x1 - d.x0) > MIN_WIDTH);
if (!flamegraphSVG) {
flamegraphSVG = d3.select("#flamegraphView").append("svg")
.attr("width", width).attr("height", height);
flamegraphG = flamegraphSVG.append("g");
const zoom = d3.zoom().scaleExtent([0.5, 10])
.on("zoom", (event) => flamegraphG.attr("transform", event.transform));
flamegraphSVG.call(zoom);
const button = document.createElement("button");
button.innerText = "Zoom Out";
button.style.position = "absolute";
button.style.top = "10px";
button.style.right = "20px";
button.style.zIndex = 1000;
button.style.padding = "8px 15px";
button.style.background = "orange";
button.style.border = "none";
button.style.color = "#fff";
button.style.fontWeight = "bold";
button.style.cursor = "pointer";
document.body.appendChild(button);
button.onclick = () => {
renderFlamegraph(flamegraphRoot);
};
} else {
flamegraphSVG.attr("width", width).attr("height", height);
}
const rects = flamegraphG.selectAll("g.node").data(nodes, getNodePath);
rects.exit().remove();
const enter = rects.enter().append("g").attr("class", "node");
enter.append("rect");
enter.append("title");
const merged = enter.merge(rects);
merged.select("rect")
.attr("x", d => d.x0).attr("y", d => d.y0)
.attr("width", d => d.x1 - d.x0).attr("height", d => d.y1 - d.y0)
.attr("fill", d => color(d.data.name))
.on("click", (event, d) => zoomIntoNode(d))
.on("mouseover", function (event, d) {
d3.select(this).attr("stroke", "#fff").attr("stroke-width", 2);
showTooltip(event, d);
})
.on("mouseout", function () {
d3.select(this).attr("stroke", null);
hideTooltip();
});
merged.select("title").text(d => `${d.data.name}: ${Math.round(d.value)} ms`);
}
function zoomIntoNode(target, partition, root) {
const container = document.getElementById("flamegraphView");
const width = container.clientWidth;
const height = container.clientHeight;
const x = d3.scaleLinear().domain([target.x0, target.x1]).range([0, width]);
const y = d3.scaleLinear().domain([target.y0, root.y1]).range([0, height]);
flamegraphG.selectAll("g.node").select("rect").transition().duration(750)
.attr("x", d => x(d.x0))
.attr("y", d => y(d.y0))
.attr("width", d => x(d.x1) - x(d.x0))
.attr("height", d => y(d.y1) - y(d.y0));
}
const tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("padding", "8px")
.style("background", "#333")
.style("color", "#fff")
.style("border-radius", "5px")
.style("visibility", "hidden");
function showTooltip(event, d) {
tooltip.style("visibility", "visible")
.html(`<b>${d.data.name}</b><br>${Math.round(d.value)} ms`)
.style("top", (event.pageY - 10) + "px")
.style("left", (event.pageX + 10) + "px");
}
function hideTooltip() {
tooltip.style("visibility", "hidden");
}
function addToFlamegraph(call) {
flamegraphRoot = { name: "__ROOT__", value: 0, children: [] };
call.forEach(element => {
flamegraphRoot.children.push(get_path_from_root(element));
});
}
function get_path_from_root(call) {
const node = {
name: call.function,
value: call.duration,
children: []
};
// Recurse over children, if any
if (call.children && call.children.size > 0) {
for (const childCall of call.children) {
node.children.push(get_path_from_root(calls[childCall]));
}
}
return node;
}
function requestFlamegraphRender() {
const now = performance.now();
if (now - flamegraphLastRender >= flamegraphRenderInterval) {
summaryLastRender = now;
currentRoot = flamegraphRoot;
renderFlamegraph(flamegraphRoot);
}
}
function zoomIntoNode(d) {
renderFlamegraph(d.data); // drill into this node
}
// tree Handler ===============================================================================================================================================================================================================
let treeLastRender = 0;
const treeRenderInterval = 100;
const BUFFER_ROWS = 10; // render slightly more than viewport to avoid blank areas
let rowPool = [];
function requestTreeRender(renderForestSnapshot) {
const now = performance.now();
if (now - treeLastRender >= treeRenderInterval) {
treeLastRender = now;
renderVirtualTree(renderForestSnapshot);
}
}
function buildForest(calls) {
const roots = [];
for (const id in calls) {
const node = calls[id];
if (!node.parent_call_id || !calls[node.parent_call_id]) {
roots.push(node);
}
}
return roots;
}
function flattenVisibleTree(node, result = [], depth = 0) {
result.push({ node, depth });
if (node.expanded) {
for (const child of node.children) {
flattenVisibleTree(calls[child], result, depth + 1);
}
}
return result;
}
function renderForest(forest) {
const container = document.getElementById("tree");
container.innerHTML = "";
const table = document.createElement("table");
table.className = "profiler-table";
container.appendChild(table);
for (const root of forest) {
renderSubtree(root, table, 0);
}
}
function renderSubtree(node, table, depth) {
const row = table.insertRow();
const cell = row.insertCell();
cell.style.paddingLeft = (depth * 20) + "px";
if (node.children.size > 0) {
const toggle = document.createElement("span");
toggle.textContent = node.expanded ? "▼ " : "▶ ";
toggle.style.cursor = "pointer";
toggle.onclick = () => {
node.expanded = !node.expanded;
renderForest(buildForest(calls));
};
cell.appendChild(toggle);
} else {
cell.textContent = "• ";
}
const label = document.createElement("span");
label.textContent = `${node.function} (${node.call_id})`;
label.style.cursor = "pointer";
label.onclick = () => showFunctionDetails(node);
cell.appendChild(label);
if (node.expanded) {
for (const child of node.children) {
renderSubtree(calls[child], table, depth + 1);
}
}
}
const ROW_HEIGHT = 25;
function renderVirtualTree(forest) {
const container = document.getElementById("tree");
const visibleNodes = [];
for (const root of forest) {
flattenVisibleTree(root, visibleNodes);
}
const totalHeight = visibleNodes.length * ROW_HEIGHT;
let scroller;
if (rowPool.length === 0) {
const viewportHeight = container.clientHeight;
const poolSize = Math.ceil(viewportHeight / ROW_HEIGHT) + BUFFER_ROWS;
scroller = initRowPool(container, poolSize);
} else {
scroller = container.firstChild;
}
scroller.style.height = totalHeight + "px";
// Attach scroll handler
container.onscroll = () => {
updateVirtualTree(container, scroller, visibleNodes);
};
updateVirtualTree(container, scroller, visibleNodes);
}
function updateVirtualTree(container, scroller, visibleNodes) {
const scrollTop = container.scrollTop;
const firstIndex = Math.floor(scrollTop / ROW_HEIGHT);
const poolSize = rowPool.length;
for (let i = 0; i < poolSize; i++) {
const nodeIndex = firstIndex + i;
const row = rowPool[i];
if (nodeIndex >= visibleNodes.length) {
row.style.display = "none";
continue;
}
row.style.display = "flex";
row.style.top = (nodeIndex * ROW_HEIGHT) + "px";
const { node, depth } = visibleNodes[nodeIndex];
row.innerHTML = ""; // Replace with smart row reuse later
row.style.paddingLeft = (depth * 20) + "px";
if (node.children.size > 0) {
const toggle = document.createElement("span");
toggle.textContent = node.expanded ? "▼ " : "▶ ";
toggle.style.marginRight = "5px";
toggle.onclick = (e) => {
e.stopPropagation();
node.expanded = !node.expanded;
renderVirtualTree(buildForest(calls));
};
row.appendChild(toggle);
} else {
row.appendChild(document.createTextNode("• "));
}
const label = document.createElement("span");
label.textContent = `${node.function} (${node.call_id})`;
label.onclick = () => showFunctionDetails(node);
row.appendChild(label);
}
}
function initRowPool(container, poolSize) {
const scroller = document.createElement("div");
scroller.style.position = "relative";
scroller.style.width = "100%";
container.innerHTML = "";
container.appendChild(scroller);
rowPool = [];
for (let i = 0; i < poolSize; i++) {
const row = document.createElement("div");
row.style.position = "absolute";
row.style.left = "0";
row.style.right = "0";
row.style.height = ROW_HEIGHT + "px";
row.style.display = "flex";
row.style.alignItems = "center";
row.style.paddingLeft = "0px";
row.style.cursor = "pointer";
rowPool.push(row);
scroller.appendChild(row);
}
return scroller;
}
// Helper functions ===============================================================================================================================================================================================================
function parseTime(timeStr) {
const [h, m, s] = timeStr.split(':');
const [sec, ms = "0"] = s.split('.');
const date = new Date();
date.setHours(parseInt(h), parseInt(m), parseInt(sec), parseInt(ms));
return date;
}