Skip to content

Commit d50df48

Browse files
committed
Add toggle to sort entries in html generated report
In some cases, specially in the bar graph summary view when many benchmarks are present, it can be useful to sort benchmarks based on their results. This commit adds a checkbox labeled `sorted` to the top bar --where the plot chooser is-- that enables sorting the benchmarks by order. Note that sorting the benchmarks changes the color and checkmarks of the traces. It might be better to preserve the initial ones, but I was too lazy to do so--I believe that we would need to manually asign colors and markers instead of relying on plotly auto-asigning them. Here is an example of the results: https://sinusoid.es/misc/nonius/sorting-example8.html
1 parent 668c595 commit d50df48

2 files changed

Lines changed: 65 additions & 13 deletions

File tree

tpl/report.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ body {
7474
display: block;
7575
padding: 0 3em 0 1.5em;
7676
margin: 0.3em;
77+
height: 2em;
7778

7879
transition: border-color 0.2s;
7980
border: 2px solid #aaa;
@@ -92,6 +93,18 @@ body {
9293
color: white;
9394
}
9495

96+
div.is-sorted {
97+
position: absolute;
98+
top: 0em;
99+
right: 1em;
100+
line-height: 2.8em;
101+
}
102+
103+
div.is-sorted input {
104+
position: relative;
105+
top: 3px;
106+
}
107+
95108
#plot {
96109
position: absolute;
97110
min-width: 300px;

tpl/report.tpl.js

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
},{% endfor %}
2626
]
2727
};
28-
28+
var origOrder = data.runs[0].benchmarks.map(function (_, i) { return i; })
29+
var sortOrder = computeSortedOrder();
2930
var plotdiv = document.getElementById("plot");
3031
window.addEventListener("resize", function() {
3132
Plotly.Plots.resize(plotdiv);
@@ -36,28 +37,65 @@
3637
chooser.addEventListener("blur", chooser.focus.bind(chooser));
3738
chooser.focus();
3839

40+
var isSortedBox = document.getElementById("is-sorted");
41+
isSortedBox.addEventListener("change", choosePlot);
42+
3943
var legendStyle = {
4044
font: { family: 'monospace' },
4145
borderwidth: 2,
4246
bordercolor: 'black'
4347
}
4448

49+
function zeroes(count) {
50+
var arr = []
51+
while (count --> 0) arr.push(0)
52+
return arr
53+
}
54+
55+
function computeSortedOrder() {
56+
// We sort each run. Then we compute the "points" of each
57+
// benchmark as the sum of the positions of this benchmkark on
58+
// each run. This gives us a rough indication of which
59+
// benchmark is best -- the lower the points, the better.
60+
var runsOrder = data.runs.map(function (r) {
61+
order = r.benchmarks.map(function (_, i) { return i; })
62+
order.sort(function (a, b) {
63+
return r.benchmarks[a].mean - r.benchmarks[b].mean
64+
})
65+
return order
66+
})
67+
var length = data.runs[0].benchmarks.length
68+
var points = runsOrder.reduce(function (acc, r) {
69+
r.forEach(function (elem, idx) {
70+
acc[elem] += idx
71+
})
72+
return acc
73+
}, zeroes(length))
74+
var order = data.runs[0].benchmarks.map(function (_, i) { return i; })
75+
order.sort(function (a, b) {
76+
return points[a] - points[b]
77+
})
78+
return order
79+
}
80+
4581
function choosePlot() {
46-
var plot = chooser.options[chooser.selectedIndex].value;
82+
var plot = chooser.options[chooser.selectedIndex].value
83+
var order = isSortedBox.checked ? sortOrder : origOrder
4784
if (plot == 'summary') {
4885
if (data.runs.length > 1) {
49-
plotSummary();
86+
plotSummary(order);
5087
} else {
51-
plotSingleSummary();
88+
plotSingleSummary(order);
5289
}
5390
} else {
54-
plotSamples(plot);
91+
plotSamples(plot, order);
5592
}
5693
}
5794

58-
function plotSamples(plot) {
95+
function plotSamples(plot, order) {
5996
var run = data.runs[plot];
60-
var traces = run.benchmarks.map(function (b, i) {
97+
var traces = order.map(function (i) {
98+
var b = run.benchmarks[i]
6199
return {
62100
name: b.name,
63101
type: 'scatter',
@@ -81,10 +119,10 @@
81119
Plotly.newPlot(plotdiv, traces, layout);
82120
}
83121

84-
function plotSummary() {
85-
var traces = data.runs[0].benchmarks.map(function (b, i) {
122+
function plotSummary(order) {
123+
var traces = order.map(function (i) {
86124
return {
87-
name: b.name,
125+
name: data.runs[0].benchmarks[i].name,
88126
type: 'scatter',
89127
marker: { symbol: i },
90128
x: data.runs.map(function (r) { return r.params[data.param]; }),
@@ -114,12 +152,13 @@
114152
Plotly.newPlot(plotdiv, traces, layout);
115153
}
116154

117-
function plotSingleSummary() {
118-
var traces = data.runs[0].benchmarks.map(function (b, i) {
155+
function plotSingleSummary(order) {
156+
var traces = order.map(function (i) {
157+
var b = data.runs[0].benchmarks[i]
119158
return {
120159
type: 'bar',
121160
name: b.name,
122-
x: [ 0 ],
161+
x: [ data.title ],
123162
y: [ b.mean ],
124163
error_y: {
125164
type: 'data',

0 commit comments

Comments
 (0)