Fix(bench): wrong location (#2932)

* Update commentBenchResult.yml

* ci: fix ext

* refactor(bench): better table
This commit is contained in:
Jonathan Ho
2023-04-01 16:27:54 -07:00
committed by GitHub
parent 6359972e38
commit 9c7b2b832e
5 changed files with 124 additions and 186 deletions

View File

@@ -1,78 +1,54 @@
import fs from 'fs/promises'
import fs from 'node:fs/promises'
const benchmarkData = await fetch(`https://raw.githubusercontent.com/discordeno/discordeno/benchies/benchmarksResult/data.js`)
.then(async (res) => await res.text())
.then((text) => JSON.parse(text.slice(24)))
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
const commitSha = await fs.readFile('./sha', 'utf-8')
const results = JSON.parse(await fs.readFile('./data.json', 'utf-8'))
const benchmarks = results.entries.Benchmark.slice(-2)
const latestHeadBenchmarks = benchmarks.length === 2 ? benchmarks[1] : benchmarks[0]
const lastHeadBenchmarks = benchmarks.length === 2 ? benchmarks[0] : undefined
const latestBaseBenchmarks = JSON.parse(JSON.stringify(benchmarkData.entries.Benchmark)).slice(-1)[0]
const benchmarks = results.entries.Benchmark
benchmarks.reverse()
const compareWithHead = {}
const compareWithBase = {}
if (lastHeadBenchmarks) {
for (const benchmark of lastHeadBenchmarks.benches) {
compareWithHead[benchmark.name] = {
previous: benchmark,
current: {},
}
}
}
const latestBaseBenchmarks = benchmarkData.entries.Benchmark.slice(-1)[0]
for (const benchmark of latestBaseBenchmarks.benches) {
compareWithBase[benchmark.name] = {
previous: benchmark,
current: {},
compareWithHead[benchmark.name] = {
[latestBaseBenchmarks.commit.id]: benchmark,
}
}
for (const benchmark of latestHeadBenchmarks.benches) {
compareWithBase[benchmark.name] = {
// @ts-expect-error it should work
previous: {},
...compareWithBase[benchmark.name],
current: benchmark,
}
compareWithHead[benchmark.name] = {
// @ts-expect-error it should work
previous: {},
...compareWithHead[benchmark.name],
current: benchmark,
for (let i = 0; i < benchmarks.length; i++) {
for (const bench of benchmarks[i].benches) {
if (compareWithHead[bench.name]) {
compareWithHead[bench.name][benchmarks[i].commit.id] = bench
} else {
compareWithHead[bench.name] = {
[benchmarks[i].commit.id]: bench,
}
}
}
}
let message = '<!-- benchmark comment by ci -->\n'
const compareTableInfo = [
{
name: 'last head',
commit: lastHeadBenchmarks ? lastHeadBenchmarks.commit.id : '',
},
{
name: 'base',
commit: latestBaseBenchmarks.commit.id,
},
]
for (const benchmarkType of ['Performance', 'Memory']) {
message += `# ${benchmarkType} Benchmark\n\n`
for (const [index, compare] of [compareWithHead, compareWithBase].entries()) {
message += `## Compared with ${compareTableInfo[index].name}\n`
message += '<details><summary>Detail results of benchmarks</summary>\n\n'
message += `| Benchmark suite | Current: ${latestHeadBenchmarks.commit.id} | Previous: ${compareTableInfo[index].commit} | Ratio |\n | -| -| -| -|\n`
for (const field of Object.keys(compare).filter((key) =>
benchmarkType === 'Performance' ? !key.startsWith('[Cache Plugin]') : key.startsWith('[Cache Plugin]'),
)) {
message += `| \`${field}\` | ${compare[field].current.value ? `\`${compare[field].current.value}\`` : ''} ${
compare[field].current.unit ?? ''
} ${compare[field].current.range ? `(\`${compare[field].current.range ?? ''}\`)` : ''} | ${
compare[field].previous.value ? `\`${compare[field].previous.value}\`` : ''
} ${compare[field].previous.unit ?? ''} ${compare[field].previous.range ? `(\`${compare[field].previous.range ?? ''}\`)` : ''} | ${
compare[field].previous.value && compare[field].current.value
? `\`${
// @ts-expect-error it work
Math.round((parseFloat(compare[field].previous.value) / parseFloat(compare[field].current.value)) * 100) / 100
}\``
: ''
} |\n`
}
message += '</details>\n\n'
}
message += `## Benchmark\n\n`
message += '<details><summary>Detail results of benchmarks</summary>\n\n'
let header1 = `| Benchmark suite | Base (${latestBaseBenchmarks.commit.id}) |`
let header2 = `|-|-|`
for (const [index, commitId] of benchmarks.map((benchmark) => benchmark.commit.id).entries()) {
header1 += index === 0 ? ` Latest Head (${commitId}) |` : ` ${commitId} |`
header2 += '-|'
}
message += `${header1}\n`
message += `${header2}\n`
for (const benchName of Object.keys(compareWithHead)) {
let benchData = `| ${benchName} |`
benchData += compareWithHead[benchName][latestBaseBenchmarks.commit.id]
? ` ${`\`${compareWithHead[benchName][latestBaseBenchmarks.commit.id].value}\` ${
compareWithHead[benchName][latestBaseBenchmarks.commit.id].unit
} \`${compareWithHead[benchName][latestBaseBenchmarks.commit.id].range}\``} |`
: '|'
for (const commitId of benchmarks.map((benchmark) => benchmark.commit.id)) {
benchData += compareWithHead[benchName][commitId]
? ` \`${compareWithHead[benchName][commitId].value}\` ${compareWithHead[benchName][commitId].unit} \`${compareWithHead[benchName][commitId].range}\`|`
: '|'
}
message += `${benchData}\n`
}
message += '</details>\n\n'
console.log(message.replaceAll('`', '\\`'))