summaryrefslogtreecommitdiff
path: root/internal/flamegraph/svgwriter_js.go
blob: bf8bfd27a959b5d8d11932b0d47f02779dbe768b (plain)
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
package flamegraph

const flamegraphJS = `
const fg = {
  frames: [],
  info: null,
  matchColor: "rgb(220, 30, 70)",
  zoomStack: [],
  zoomRange: null,
  rootWidth: 0,
};

function fgInit() {
  fg.frames = Array.from(document.querySelectorAll("g.frame"));
  fg.info = document.getElementById("fg-info");
  fg.rootWidth = fgDetectRootWidth();
  fg.frames.forEach((frame) => {
    fgSnapshotOriginalGeometry(frame);
    frame.addEventListener("click", (ev) => {
      if (ev.detail > 1) return;
      ev.stopPropagation();
      fgZoom(ev.currentTarget);
    });
    frame.addEventListener("dblclick", (ev) => {
      ev.preventDefault();
      ev.stopPropagation();
      fgResetZoom();
    });
    frame.addEventListener("mouseenter", (ev) => fgHover(ev.currentTarget));
  });
  document.addEventListener("dblclick", (ev) => {
    ev.preventDefault();
    fgResetZoom();
  });
}

function fgHover(frame) {
  if (!fg.info) return;
  const title = frame.querySelector("title");
  fg.info.textContent = title ? title.textContent : "";
}

function fgZoom(frame) {
  const x = fgOriginalX(frame);
  const w = fgOriginalW(frame);
  if (w <= 0) return;
  if (fg.zoomRange) {
    fg.zoomStack.push(fg.zoomRange);
  }
  fg.zoomRange = { x: x, w: w, depth: Number(frame.dataset.depth || "0") };
  fgApplyZoom();
}

function fgApplyZoom() {
  if (!fg.zoomRange) {
    fg.frames.forEach((frame) => {
      frame.style.display = "";
    });
    return;
  }
  const x = fg.zoomRange.x;
  const end = x + fg.zoomRange.w;
  const width = fg.zoomRange.w;
  const minDepth = fg.zoomRange.depth;
  const eps = 1e-6;
  const scale = fg.rootWidth / width;
  fg.frames.forEach((other) => {
    const ox = fgOriginalX(other);
    const ow = fgOriginalW(other);
    const depth = Number(other.dataset.depth || "0");
    const inSelectedRange = ox >= x-eps && ox+ow <= end+eps;
    const isAncestor = depth < minDepth && ox <= x+eps && ox+ow >= end-eps;

    if (isAncestor || (depth >= minDepth && inSelectedRange)) {
      if (isAncestor) {
        fgSetFrameGeometry(other, 0, fg.rootWidth);
      } else {
        fgSetFrameGeometry(other, (ox-x)*scale, ow*scale);
      }
      other.style.display = "";
    } else {
      other.style.display = "none";
    }
  });
}

function fgUndoZoom() {
  if (fg.zoomStack.length === 0) {
    fgResetZoom();
    return;
  }
  fg.zoomRange = fg.zoomStack.pop();
  fgApplyZoom();
}

function fgResetZoom() {
  fg.zoomStack = [];
  fg.zoomRange = null;
  fg.frames.forEach((frame) => {
    fgRestoreFrameGeometry(frame);
    frame.style.display = "";
  });
}

function fgSearch() {
  const needle = prompt("Search frames (substring):", "");
  if (needle === null) return;
  const q = needle.trim().toLowerCase();
  fg.frames.forEach((frame) => {
    const rect = frame.querySelector("rect");
    const base = frame.dataset.baseFill || "";
    const name = (frame.dataset.name || "").toLowerCase();
    if (!rect) return;
    if (q !== "" && name.includes(q)) {
      rect.style.fill = fg.matchColor;
    } else {
      rect.style.fill = base;
    }
  });
}

function fgResetSearch() {
  fg.frames.forEach((frame) => {
    const rect = frame.querySelector("rect");
    if (!rect) return;
    rect.style.fill = frame.dataset.baseFill || "";
  });
}

function fgDetectRootWidth() {
  let maxEnd = 0;
  fg.frames.forEach((frame) => {
    const x = Number(frame.dataset.x || "0");
    const w = Number(frame.dataset.w || "0");
    maxEnd = Math.max(maxEnd, x + w);
  });
  return maxEnd;
}

function fgSnapshotOriginalGeometry(frame) {
  const rect = frame.querySelector("rect");
  const text = frame.querySelector("text");
  frame.dataset.ox = frame.dataset.x || "0";
  frame.dataset.ow = frame.dataset.w || "0";
  if (rect) {
    rect.dataset.ox = rect.getAttribute("x") || "0";
    rect.dataset.ow = rect.getAttribute("width") || "0";
  }
  if (text) {
    text.dataset.ox = text.getAttribute("x") || "0";
    text.dataset.hidden = text.style.display === "none" ? "1" : "0";
    text.dataset.full = text.textContent || frame.dataset.name || "";
  }
}

function fgOriginalX(frame) {
  return Number(frame.dataset.ox || frame.dataset.x || "0");
}

function fgOriginalW(frame) {
  return Number(frame.dataset.ow || frame.dataset.w || "0");
}

function fgSetFrameGeometry(frame, x, w) {
  const rect = frame.querySelector("rect");
  const text = frame.querySelector("text");
  if (rect) {
    rect.setAttribute("x", String(x));
    rect.setAttribute("width", String(w));
  }
  if (text) {
    text.setAttribute("x", String(x + 3));
    fgFitLabel(text, w);
  }
}

function fgRestoreFrameGeometry(frame) {
  const rect = frame.querySelector("rect");
  const text = frame.querySelector("text");
  if (rect) {
    rect.setAttribute("x", rect.dataset.ox || "0");
    rect.setAttribute("width", rect.dataset.ow || "0");
  }
  if (text) {
    text.setAttribute("x", text.dataset.ox || "0");
    if (text.dataset.hidden === "1") {
      text.style.display = "none";
      text.textContent = text.dataset.full || "";
    } else {
      fgFitLabel(text, Number(rect ? (rect.dataset.ow || "0") : "0"));
    }
  }
}

function fgFitLabel(text, width) {
  const full = text.dataset.full || text.textContent || "";
  const maxChars = Math.floor((width - 6) / 7);
  if (maxChars < 3) {
    text.style.display = "none";
    text.textContent = full;
    return;
  }
  text.style.display = "";
  if (full.length <= maxChars) {
    text.textContent = full;
    return;
  }
  text.textContent = full.slice(0, maxChars - 1) + "…";
}

window.addEventListener("DOMContentLoaded", fgInit);
`