-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathgraph.ts
181 lines (154 loc) · 4.45 KB
/
graph.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { writeFileSync } from 'node:fs';
import { optimize } from 'svgo';
import { parse, View } from 'vega';
import { compile } from 'vega-lite';
import { availableBenchmarks, type AvailableBenchmarksIds } from './register';
import type { BenchmarkResult } from './types';
interface PreviewGraphParams {
values: BenchmarkResult[];
filename: string;
}
export async function writePreviewGraph(params: PreviewGraphParams) {
const svg = await previewGraph(params);
writeFileSync(params.filename, svg);
}
function assertNever(x: never): never {
throw new Error(`assert-never: unknown value: ${x}`);
}
function getBenchmarkLabel(benchmark: AvailableBenchmarksIds) {
switch (benchmark) {
case 'assertLoose':
return 'Loose Assertion';
case 'assertStrict':
return 'Strict Assertion';
case 'parseSafe':
return 'Safe Parsing';
case 'parseStrict':
return 'Strict Parsing';
default:
assertNever(benchmark);
}
}
type BenchmarkLabel = ReturnType<typeof getBenchmarkLabel>;
function median(values: number[]) {
if (!values.length) {
return NaN;
}
if (values.length % 2) {
return values[(values.length - 1) / 2];
}
return (values[values.length / 2 - 1] + values[values.length / 2]) / 2;
}
interface PreparedResult extends Partial<Record<BenchmarkLabel, number>> {
name: string;
}
// Cheap aggregation of benchmark data.
// For the repeated bar chart, vega-lite expects a numeric value for each
// repeated field (the benchmark label) for each benchmarked library (`name`).
function prepareData(values: BenchmarkResult[], resultCountToInclude = 4) {
const bins = new Map<string, BenchmarkResult[]>();
values.forEach(result => {
let bin = bins.get(result.benchmark);
if (!bin) {
bins.set(result.benchmark, []);
bin = [];
}
bin.push(result);
});
const preparedResult: PreparedResult[] = [];
function updateResult(
name: string,
benchmarkLabel: BenchmarkLabel,
ops: number,
) {
const existing = preparedResult.find(v => v.name === name);
if (existing) {
existing[benchmarkLabel] = ops;
} else {
preparedResult.push({ name, [benchmarkLabel]: ops });
}
}
bins.forEach(v => {
if (!v.length) {
throw new Error('no results in this bin');
}
const sorted = v.sort((a, b) => b.ops - a.ops);
// the N fasted benchmarks
sorted
.slice(0, resultCountToInclude)
.forEach(r =>
updateResult(
r.name,
getBenchmarkLabel(r.benchmark as AvailableBenchmarksIds),
r.ops,
),
);
});
// add median last to make it appear at the bottom of each individual
// barchart
bins.forEach(v => {
const sorted = v.sort((a, b) => b.ops - a.ops);
// median of the rest as a comparison
updateResult(
'(median)',
getBenchmarkLabel(v[0].benchmark as AvailableBenchmarksIds),
median(sorted.map(x => x.ops)),
);
});
return preparedResult;
}
// generate a nice preview graph
async function previewGraph({ values }: PreviewGraphParams): Promise<string> {
const vegaSpec = compile({
repeat: Object.keys(availableBenchmarks).map(b =>
getBenchmarkLabel(b as AvailableBenchmarksIds),
),
columns: 2,
title: {
anchor: 'middle',
offset: 20,
text: 'Top 3 packages for each benchmark + median, (ops count, better ⯈)',
fontWeight: 'normal',
fontSize: 16,
},
spec: {
data: {
values: prepareData(values, 3),
},
height: { step: 15 },
mark: 'bar',
encoding: {
x: {
field: { repeat: 'repeat' },
type: 'quantitative',
sort: 'ascending',
},
y: {
field: 'name',
type: 'nominal',
title: null,
// do not sort by name to keep the preparedValues sorting by ops
// instead
// also, we cannot use `sort: '-x'` because that will include every
// top 3 library in every repeated bar chart, regardless whether it
// is in the top 3 of the current benchmark
sort: null,
},
color: {
field: 'name',
type: 'nominal',
legend: null,
scale: { scheme: 'tableau10' },
},
},
},
});
const view = new View(parse(vegaSpec.spec), { renderer: 'none' });
const svg = await view.toSVG();
const optimizeSvg = await optimize(svg, {
js2svg: {
pretty: true,
},
});
return optimizeSvg.data;
}