-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchart_gen.ts
255 lines (215 loc) · 6.25 KB
/
chart_gen.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
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
import { writeFile, readFile } from 'fs/promises'
import { cpus } from 'os'
import path from 'path'
import { formatLog, Rec } from './utils'
type LibName = string
type LibValues = ReturnType<typeof formatLog>
type IterationName = number
type IterationValues = Record<LibName, LibValues>
type BenchResults = Record<IterationName, IterationValues>
type Iterations = Array<IterationName>['length']
type Point = [number, number]
type ChartData = Array<{
name: string
version: string
color: string
min: Point[]
med: Point[]
max: Point[]
medValue: number
labelY: number
}>
const DOWNLOAD_LIMIT = 500
const CPU = cpus()[0]?.model?.replace(/ /g, '_') ?? 'unknown_cpu'
const POPULAR_CHART_PATH = `./popular_chart_${CPU}.svg`
const ALL_CHART_PATH = `./all_chart_${CPU}.svg`
const CHART_TEMPLATE = './chart_template.svg'
const START_MARK = '<!--CONTENT_START-->'
const END_MARK = '<!--CONTENT_END-->'
const REGEX = new RegExp(`${START_MARK}(.*)${END_MARK}`, 'gms')
const X_START = 100
const X_STEP = 230
const Y_START = 20
const Y_RANGE = 500
const LABEL_HEIGHT = 16
const hsl = (i: number, length: number) => {
const h = Math.round((i * 360) / length) % 360
return `hsl(${h}, 90%, 45%)`
}
const PACKAGE_NAMES: Rec<string> = {
'effector.fork': 'effector',
frpts: '@frp-ts/core',
mol: 'mol_wire_lib',
preact: '@preact/signals-core',
reatom: '@reatom/core',
solid: 'solid-js',
v4: 'v4',
}
export async function genChart(allResults: BenchResults) {
const popularLibs = new Array()
for (const libName of Object.keys(Object.values(allResults)[0]!)) {
const moduleName = PACKAGE_NAMES[libName] ?? libName
const moduleUrlName = moduleName.replace('/', '%2F')
const downloadsUrl = `https://api.npmjs.org/versions/${moduleUrlName}/last-week`
const downloads = await fetch(downloadsUrl)
.then((r) => r.json())
.then(({ downloads }: { downloads: Rec<number> }) =>
Object.values(downloads).reduce((acc, v) => acc + v, 0),
)
.catch((error) => {
console.error(`Failed to fetch downloads for ${name}`)
console.log(error)
return 0
})
if (downloads > DOWNLOAD_LIMIT) popularLibs.push(libName)
}
const popularResults = Object.fromEntries(
Object.entries(allResults).map(([iteration, iterationValues]) => [
iteration,
Object.fromEntries(
Object.entries(iterationValues).filter(([libName]) =>
popularLibs.includes(libName),
),
),
]),
)
const template = await readFile(CHART_TEMPLATE, 'utf8')
const allData = await getChartData(allResults)
const popularData = await getChartData(popularResults)
const svgAll = allData.map(getLibSVG).join('')
const svgPopular = popularData.map(getLibSVG).join('')
await writeFile(
ALL_CHART_PATH,
template.replace(REGEX, START_MARK + svgAll + END_MARK),
)
await writeFile(
POPULAR_CHART_PATH,
template.replace(REGEX, START_MARK + svgPopular + END_MARK),
)
let readme = await readFile('./README.md', 'utf8')
if (readme.includes(CPU)) {
readme = readme.replace(
new RegExp(`### ${CPU}(.|\n)*<!-- ### ${CPU} -->`),
`### ${CPU}
![](${POPULAR_CHART_PATH})
<details>
<summary>all results</summary>
![](${ALL_CHART_PATH})
</details>
<!-- ### ${CPU} -->`,
)
} else {
readme = readme.replace(
'## Results',
`## Results
### ${CPU}
![](${POPULAR_CHART_PATH})
<details>
<summary>all results</summary>
![](${ALL_CHART_PATH})
</details>
<!-- ### ${CPU} -->`,
)
}
await writeFile('./README.md', readme)
}
async function getChartData(results: BenchResults): Promise<ChartData> {
const fastest = {
min: [] as Array<Iterations>,
med: [] as Array<Iterations>,
max: [] as Array<Iterations>,
}
const libsResults: Rec<typeof fastest> = {}
const data: ChartData = []
let i = -1
for (const [iteration, iterationValues] of Object.entries(results)) {
i++
if (!fastest.min[i]) fastest.min[i] = Infinity
if (!fastest.med[i]) fastest.med[i] = Infinity
if (!fastest.max[i]) fastest.max[i] = Infinity
for (const [lib, libValues] of Object.entries(iterationValues)) {
const group = (libsResults[lib] ??= {
min: [],
med: [],
max: [],
})
group.min.push(libValues.min)
group.med.push(libValues.med)
group.max.push(libValues.max)
if (libValues.min < fastest.min[i]!) {
fastest.min[i] = libValues.min
}
if (libValues.med < fastest.med[i]!) {
fastest.med[i] = libValues.med
}
if (libValues.max < fastest.max[i]!) {
fastest.max[i] = libValues.max
}
}
}
const libsNames = Object.keys(libsResults).sort()
const toPoint = (v: number, i: number) => getPoint(v, fastest.med[i]!, i)
for (const [lib, libResults] of Object.entries(libsResults)) {
const moduleName = PACKAGE_NAMES[lib] || lib
const color = hsl(libsNames.indexOf(lib), libsNames.length)
const min = libResults.min.map(toPoint)
const med = libResults.med.map(toPoint)
const max = libResults.max.map(toPoint)
const medValue = libResults.med[0]
const modulePath = path.join(
__dirname,
'node_modules',
moduleName,
'package.json',
)
const file = await readFile(modulePath, 'utf8').catch(
() => '{"version": "0.0.0"}',
)
const version = JSON.parse(file).version
data.push({
name: lib,
version,
color,
min,
med,
max,
medValue,
labelY: 0,
})
}
data.sort((a, b) => a.medValue - b.medValue)
data.forEach((el, i, arr) => {
el.labelY = el.med[0]![1]! + 3
if (i && el.labelY - arr[i - 1]!.labelY < LABEL_HEIGHT) {
el.labelY = arr[i - 1]!.labelY + LABEL_HEIGHT
}
})
return data
}
function getPoint(value: number, minValue: number, step: number): Point {
const x = X_START + X_STEP * step
const y = Y_START + Y_RANGE * (1 - minValue / value)
return [x, y]
}
function getLibSVG({
name: lib,
color,
med,
labelY,
version,
}: ChartData[number]) {
return `<g>
<text
class="lib-label"
x="95"
y="${labelY}"
fill="${color}"
>${lib} ${version}</text>
<polyline
class="line-med"
fill="none"
stroke="${color}"
points="${med.map((point) => point.join()).join(' ')}"
></polyline>
</g>`
}