-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
384 lines (346 loc) · 13.2 KB
/
index.js
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
function main() {
// last-n slider
const last_n_slider = document.querySelector(".last-n-slider");
const last_n = document.querySelector(".last-n");
if (last_n_slider.value == 0) {
last_n.textContent = "All";
} else {
last_n.textContent = Number(last_n_slider.value) +
Number(last_n_slider.step);
}
// connect to websocket
const socket = new WebSocket("wss://tinymod.dev:10000");
// const socket = new WebSocket("ws://localhost:10000");
console.log("Connecting to websocket");
// reload the page when the websocket errors or closes
socket.onerror = (error) => {
console.error("WebSocket error: ", error);
// reload the page
setTimeout(() => {
window.location.reload();
}, 1000);
};
socket.onclose = (event) => {
console.log("WebSocket closed: ", event);
// reload the page
setTimeout(() => {
window.location.reload();
}, 1000);
};
// state
const charts = {};
let currCommit = "";
let lastUpdateTime = Date.now();
let runCommitMap = {};
function reload_charts() {
for (const card of document.querySelectorAll(".stat-card")) {
if (card.hasAttribute("data-charted")) {
observer.unobserve(card);
observer.observe(card);
card.removeAttribute("data-charted");
}
}
lastUpdateTime = Date.now();
}
// last-n slider event listener
last_n_slider.addEventListener("input", (event) => {
if (event.target.value == last_n_slider.max) {
last_n.textContent = "All";
} else {
last_n.textContent = Number(last_n_slider.value) +
Number(last_n_slider.step);
}
reload_charts();
});
socket.onmessage = (event) => {
// split event.data on first space
// first part is status code
// second part is data
const data = JSON.parse(event.data);
if ("error" in data) {
console.error("ws api error: ", data.error);
return;
}
if ("benchmarks" in data) {
console.log(data);
// generate integer only chart ticks for the x axis
const x_ticks = [];
let lowest_x = Infinity;
let highest_x = -Infinity;
for (const benchmark of data.benchmarks) {
if (benchmark.length == 0) continue;
if (benchmark[0].x < lowest_x) {
lowest_x = benchmark[0].x;
}
if (benchmark[benchmark.length - 1].x > highest_x) {
highest_x = benchmark[benchmark.length - 1].x;
}
}
const low = Math.floor(lowest_x / 10) * 10;
const high = Math.ceil(highest_x / 10) * 10;
const divisor = (high - low) / 10;
for (let i = low; i < high; i += divisor) {
const i_10 = i;
if (i_10 < lowest_x || i_10 > highest_x) continue;
x_ticks.push(i_10);
}
// modify data if we want 1 over data
//if (data.filename.includes("llama")) {
// for (const benchmark of data.benchmarks) {
// for (const point of benchmark) {
// point.y = 1 / point.y * 1000;
// point.y = Math.round(point.y * 100) / 100;
// }
// }
//}
// update chart
charts[`${data.filename}-${data.system}`].update({
series: data.benchmarks,
}, {
showPoint: true,
showLine: true,
showArea: true,
lineSmooth: false,
axisX: {
type: Chartist.FixedScaleAxis,
ticks: x_ticks,
high: highest_x,
low: lowest_x,
},
});
} else if ("curr-commit" in data) {
const lastUpdated = document.querySelector("#last-updated");
lastUpdated.textContent = new Date(Date.now() - lastUpdateTime)
.toISOString().slice(11, 19);
if (currCommit === data["curr-commit"]) return;
const commitElem = document.querySelector("#curr-commit");
commitElem.textContent = data["curr-commit"].slice(0, 7);
commitElem.href = `https://github.com/tinygrad/tinygrad/commit/${
data["curr-commit"]
}`;
currCommit = data["curr-commit"];
reload_charts();
socket.send("get-run-commit-map");
} else if ("run-commit-map" in data) {
runCommitMap = data["run-commit-map"];
console.log(runCommitMap);
}
};
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
const filename = entry.target.getAttribute("data-filename");
const system = entry.target.getAttribute("data-system");
if (entry.isIntersecting) {
// check if there is a chart already
if (!entry.target.hasAttribute("data-charted")) {
charts[`${filename}-${system}`] = new Chartist.Line(
`#chart-${filename.replace(/\.[^/.]+$/, "")}-${system}`,
[],
{
plugins: [
Chartist.plugins.hoverline(),
],
},
);
let last_n_v = Number(last_n_slider.value) +
Number(last_n_slider.step);
if (last_n_v > Number(last_n_slider.max)) {
last_n_v = 0;
}
socket.send(
`get-benchmark ${filename} ${system} ${last_n_v}`,
);
entry.target.setAttribute("data-charted", true);
}
}
});
}, {
threshold: 0.25,
});
socket.onopen = () => {
console.log("Connected to websocket");
socket.send("get-curr-commit");
setInterval(() => {
socket.send("get-curr-commit");
}, 2000);
for (const card of document.querySelectorAll(".stat-card")) {
observer.observe(card);
}
};
(function (window, document, Chartist) {
"use strict";
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.hoverline = function () {
return function hoverline(chart) {
const $chart = chart.container;
let $lineIsShown = false;
let $lineFrozen = false;
let $line = $chart.querySelector(".chartist-hoverline");
if (!$line) {
$line = document.createElement("div");
$line.className = "chartist-hoverline";
const $box = document.querySelector(".hoverline-box");
$line.appendChild($box.cloneNode(true));
$chart.appendChild($line);
}
hide($line);
$chart.addEventListener("mouseover", function () {
show($line);
});
$chart.addEventListener("mouseout", function () {
if (!$lineFrozen) {
hide($line);
}
});
$chart.addEventListener("mousemove", function (event) {
if ($lineIsShown && !$lineFrozen) {
// locate the closest point on the x-axis
const eventX = event.layerX || event.offsetX;
let target, points;
if (chart instanceof Chartist.Line) {
target = event.target.ownerSVGElement || event.target;
points = target.querySelectorAll(".ct-point");
}
let closest;
if (points.length > 0) {
closest = Array.from(points).reduce((prev, curr) => {
if (!prev) return curr;
const prevDelta = Math.abs(prev.x1.baseVal.value - eventX);
const currDelta = Math.abs(curr.x1.baseVal.value - eventX);
return prevDelta < currDelta ? prev : curr;
});
}
// find all the points with the same x
let sameX = [];
if (points.length > 1) {
sameX = Array.from(points).filter((point) => {
return point.x1.baseVal.value === closest.x1.baseVal.value;
});
}
// locate the first points on the x-axis
let firstPoints = [];
if (points.length > 0) {
firstPoints = Array.from(points).filter((point) => {
return point.x1.baseVal.value === points[0].x1.baseVal.value;
});
}
setPosition(event, sameX, firstPoints);
}
});
$chart.addEventListener("click", function () {
if ($lineFrozen) {
thaw($line);
} else {
freeze($line);
}
});
function setPosition(event, points, firstPoints) {
const width = $line.offsetWidth;
const offsetX = -width / 2;
const offsetBox = $chart.getBoundingClientRect();
const allOffsetLeft = -offsetBox.left - window.pageXOffset + offsetX;
if (points.length > 0) {
const anchorLeft = points[0].x2.baseVal.value + offsetBox.left +
window.pageXOffset;
$line.style.left = anchorLeft + allOffsetLeft + "px";
const commitElem = $line.querySelector(".hoverline-commit");
const commit =
runCommitMap[points[0].getAttribute("ct:value").split(",")[0]];
if (commit === undefined) {
commitElem.textContent = "unknown";
} else {
commitElem.textContent = commit.slice(0, 7);
commitElem.href =
`https://github.com/tinygrad/tinygrad/commit/${commit}`;
}
let firstName = "red";
const chartSystem = $chart.parentElement.getAttribute("data-system");
if (chartSystem == "comma") {
firstName = "comma";
} else if (chartSystem == "usage") {
firstName = "usage";
}
$line.querySelector(".hoverline-run").textContent =
points[0].getAttribute("ct:value").split(",")[0];
// get the absolute values
$line.querySelector("#delta-value").textContent = "";
for (let i = 0; i < points.length; i++) {
const value = points[i].getAttribute("ct:value").split(",")[1];
if (i === 0) {
$line.querySelector("#delta-value").textContent += `${firstName}: ${value}\n`;
} else if (i === 1) {
$line.querySelector("#delta-value").textContent += `green: ${value}\n`;
} else if (i === 2) {
$line.querySelector("#delta-value").textContent += `mac: ${value}`;
}
}
// get the deltas from the previous points
$line.querySelector("#delta-from-prev").textContent = "";
for (let i = 0; i < points.length; i++) {
const curr = points[i].getAttribute("ct:value").split(",")[1];
const prev = (points[i].previousElementSibling.getAttribute("ct:value") !== null) ? points[i].previousElementSibling.getAttribute("ct:value").split(",")[1] : curr;
const delta = curr - prev;
const deltaFromPrev = ((delta / prev) * 100).toFixed(2);
if (i === 0) {
$line.querySelector("#delta-from-prev").textContent += `${firstName}: ${deltaFromPrev}%\n`;
} else if (i === 1) {
$line.querySelector("#delta-from-prev").textContent += `green: ${deltaFromPrev}%\n`;
} else if (i === 2) {
$line.querySelector("#delta-from-prev").textContent += `mac: ${deltaFromPrev}%`;
}
}
// get the deltas from the first points
$line.querySelector("#delta-from-first").textContent = "";
for (let i = 0; i < points.length; i++) {
const curr = points[i].getAttribute("ct:value").split(",")[1];
const prev = firstPoints[i].getAttribute("ct:value").split(",")[1];
const delta = curr - prev;
const deltaFromPrev = ((delta / prev) * 100).toFixed(2);
if (i === 0) {
$line.querySelector("#delta-from-first").textContent += `${firstName}: ${deltaFromPrev}%\n`;
} else if (i === 1) {
$line.querySelector("#delta-from-first").textContent += `green: ${deltaFromPrev}%\n`;
} else if (i === 2) {
$line.querySelector("#delta-from-first").textContent += `mac: ${deltaFromPrev}%`;
}
}
} else {
$line.style.left = event.pageX + allOffsetLeft + "px";
$line.querySelector(".hoverline-commit").textContent = "";
$line.querySelector(".hoverline-run").textContent = "";
$line.querySelector("#delta-from-prev").textContent = "";
$line.querySelector("#delta-from-first").textContent = "";
}
}
function show(element) {
$lineIsShown = true;
if (!hasClass(element, "hoverline-show")) {
element.className = element.className + " hoverline-show";
}
}
function hide(element) {
$lineIsShown = false;
const regex = new RegExp("hoverline-show" + "\\s*", "gi");
element.className = element.className.replace(regex, "").trim();
}
function freeze(element) {
$lineFrozen = true;
if (!hasClass(element, "hoverline-frozen")) {
element.className = element.className + " hoverline-frozen";
}
}
function thaw(element) {
$lineFrozen = false;
const regex = new RegExp("hoverline-frozen" + "\\s*", "gi");
element.className = element.className.replace(regex, "").trim();
}
};
};
function hasClass(element, className) {
return (" " + element.getAttribute("class") + " ").indexOf(
" " + className + " ",
) > -1;
}
})(window, document, Chartist);
}
main();