-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.ts
160 lines (149 loc) · 3.66 KB
/
index.ts
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
import * as Plotly from "plotly.js/lib/core";
import * as _ from "lodash";
import * as R from "ramda";
import * as Benchmark from "benchmark";
import * as chroma from "chroma-js";
// @ts-ignore
import * as data from "./data.json";
// @ts-ignore
import * as tableView from "./view.handlebars";
const colormap = chroma
.scale(["rgb(50, 213, 119)", "rgb(255, 84, 84)"])
.mode("hsl");
function scale(min: number, max: number, n: number): number {
return (n - min) / (max - min);
}
function createDt(text: string) {
const dt = document.createElement("td");
dt.textContent = text;
return dt;
}
function createTh(text: string) {
const dt = document.createElement("th");
dt.textContent = text;
return dt;
}
type TableData = {
name: string;
description: string;
input: any;
tableRows: {
name: string;
data: { color: string; n: number }[];
}[];
};
function createTableData(plot): TableData {
const getMean = R.path(["stats", "mean"]);
const lowHigh: [number, number][] = R.pipe(
R.pluck("result"),
// @ts-ignore
R.map(R.map(getMean)),
R.transpose,
R.map(
R.reduce(([low, high], n: number) => [R.min(low, n), R.max(high, n)], [
Infinity,
0
])
)
)(plot.data);
const tableRows = plot.data.map(
(entry: { result: any; testName: string }) => {
const data = _.map(
_.zip<any, [number, number]>(entry.result, lowHigh),
([r, [low, high]]) => {
const color = colormap(scale(low, high, r.stats.mean));
const n = (r.stats.mean / low).toFixed(2);
return { color, n, fastest: getMean(r) === low };
}
);
return {
name: entry.testName,
data
};
}
);
return {
name: plot.name,
description: plot.description,
tableRows,
input: plot.input
};
}
function createData(data) {
return {
benchmarks: R.map(createTableData, data)
};
}
function plotData(name: string, ns: number[], stats: Benchmark[]): any {
return {
name: name,
x: ns,
y: stats.map(s => s.stats.mean),
type: "scatter",
error_y: {
type: "data",
visible: true,
array: stats.map(s => s.stats.moe)
},
text: stats.map(s => s.hz.toFixed(2) + " op/s")
};
}
function insertGraphs(): void {
for (const plot of data) {
const plotElm = document.createElement("div");
const input = plot.input;
const dataForPlot = plot.data.map(({ testName, result }) =>
plotData(testName, input, result)
);
const sortedData = _.reverse(_.sortBy(dataForPlot, d => _.last(d.y)));
Plotly.plot(
plotElm,
sortedData,
{
yaxis: {
title: "Time spent"
},
xaxis: {
title: "Number of elements"
},
font: {
size: 14,
family: "'Source Sans Pro', sans-serif"
},
// autosize: false,
width: 600,
height: 350,
margin: {
l: 65,
r: 100,
b: 40,
t: 0
},
paper_bgcolor: "transparent",
plot_bgcolor: "transparent"
},
{
modeBarButtons: [
[
"zoom2d",
"pan2d",
"zoomIn2d",
"zoomOut2d",
"autoScale2d",
"resetScale2d",
"hoverClosestCartesian",
"hoverCompareCartesian"
]
],
displaylogo: false
}
);
document.getElementById(plot.name + "-graph").appendChild(plotElm);
// document.body.appendChild(createTable(plot));
}
}
const div = document.createElement("div");
const viewData = createData(data);
div.innerHTML = tableView(viewData);
document.body.appendChild(div);
insertGraphs();