-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (169 loc) · 5.13 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
"use strict";
(function () {
// Colors from https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
const toolColors = {
cargo: "#dea584",
go: "#00add8",
benchmarkjs: "#f1e05a",
pytest: "#3572a5",
googlecpp: "#f34b7d",
catch2: "#f34b7d",
benchmarkluau: "#000080",
roblox: "#000080",
_: "#333333",
};
function init(data) {
function collectBenchesPerTestCase(entries) {
const map = new Map();
for (const entry of entries) {
const { commit, date, tool, benches } = entry;
for (const bench of benches) {
const result = { commit, date, tool, bench };
const arr = map.get(bench.name);
if (arr === undefined) {
map.set(bench.name, [result]);
} else {
arr.push(result);
}
}
}
return map;
}
// Render header
document.getElementById("last-update").textContent = new Date(
data.lastUpdate
).toString();
const repoLink = document.getElementById("repository-link");
repoLink.href = data.repoUrl;
repoLink.textContent = data.repoUrl;
// Render footer
document.getElementById("dl-button").onclick = () => {
const dataUrl = "data:," + JSON.stringify(data, null, 2);
const a = document.createElement("a");
a.href = dataUrl;
a.download = "benchmark_data.json";
a.click();
};
// Prepare data points for charts
return Object.keys(data.entries).map((name) => ({
name,
dataSet: collectBenchesPerTestCase(data.entries[name]),
}));
}
function renderAllChars(dataSets) {
function renderGraph(parent, name, dataset) {
const canvas = document.createElement("canvas");
canvas.className = "benchmark-chart";
parent.appendChild(canvas);
const color = toolColors[dataset.length > 0 ? dataset[0].tool : "_"];
const data = {
labels: dataset.map((d) => d.commit.id.slice(0, 7)),
datasets: [
{
label: name,
data: dataset.map((d) => d.bench.value),
borderColor: color,
backgroundColor: color + "60", // Add alpha for #rrggbbaa
cubicInterpolationMode: 'monotone'
},
],
};
const options = {
scales: {
xAxes: [
{
scaleLabel: {
display: true,
labelString: "commit",
},
},
],
yAxes: [
{
scaleLabel: {
display: true,
labelString: dataset.length > 0 ? dataset[0].bench.unit : "",
},
ticks: {
beginAtZero: true,
},
},
],
},
animation: {
duration: 0
},
tooltips: {
callbacks: {
afterTitle: (items) => {
const { index } = items[0];
const data = dataset[index];
return (
"\n" +
data.commit.message +
"\n\n" +
data.commit.timestamp +
" committed by @" +
data.commit.committer.username +
"\n"
);
},
label: (item) => {
let label = item.value;
const { range, unit } = dataset[item.index].bench;
label += " " + unit;
if (range) {
label += " (" + range + ")";
}
return label;
},
afterLabel: (item) => {
const { extra } = dataset[item.index].bench;
return extra ? "\n" + extra : "";
},
},
},
onClick: (_mouseEvent, activeElems) => {
if (activeElems.length === 0) {
return;
}
// XXX: Undocumented. How can we know the index?
const index = activeElems[0]._index;
const url = dataset[index].commit.url;
window.open(url, "_blank");
},
};
new Chart(canvas, {
type: "line",
data,
options,
});
}
function renderBenchSet(name, benchSet, main) {
const setElem = document.createElement("div");
setElem.className = "benchmark-set";
main.appendChild(setElem);
const nameElem = document.createElement("h1");
nameElem.className = "benchmark-title";
nameElem.textContent = name;
setElem.appendChild(nameElem);
const graphsElem = document.createElement("div");
graphsElem.className = "benchmark-graphs";
setElem.appendChild(graphsElem);
for (const [benchName, benches] of benchSet.entries()) {
renderGraph(graphsElem, benchName, benches);
}
}
const main = document.getElementById("main");
for (const { name, dataSet } of dataSets) {
renderBenchSet(name, dataSet, main);
}
}
const search = window.location.search;
const name = search.startsWith("?") ? search.substring(1) : "bench";
fetch("./" + name + ".json")
.then((response) => response.json())
.then((data) => {
renderAllChars(init(data));
});
})();