Files
nitro/scripts/performance/github-report.ts
T
Marc RousavyandGitHub fe50d8fcde ci: add dedicated Release benchmark app and performance reporting (#1583)
* ci: add dedicated Release benchmark app and performance reporting

* chore: refresh example pod checksums after moving benchmark code

* perf: align benchmark samples around 150ms with bounded allocation chunks

* perf: yield outside timed chunks for native cleanup

* perf: bound native cleanup groups without yielding for each tiny chunk

* perf: let the native run loop drain outside measured chunks

* perf: isolate benchmark cases in fresh app processes

* perf: bound Kotlin buffer-copy chunks for small Java heaps

* perf: collect Java buffer allocations outside measured chunks

* perf: release Java Promise references between measured chunks

* ci: publish measured Bencher baselines for stacked PR reports

* ci: report trusted same-repository performance PRs before merge

* ci: preserve both testbeds when publishing Bencher reports

* ci: pin the multi-platform performance publisher fix

* ci: verify the reviewed Bencher CLI binary before publishing

* perf: report decisive changes despite noisy absolute timings

* ci: pin the decisive performance reporter

* ci: simplify performance PR report

* ci: pin simplified performance reporter

* fix: render performance tables as HTML

* ci: pin HTML performance reporter
2026-09-05 15:11:25 +03:00

120 lines
3.8 KiB
TypeScript

import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { parseArguments, requiredArgument } from './args'
import { isSafeSha } from './schema'
const COMMENT_MARKER = '<!-- nitro-performance-paired-comparison -->'
interface PullRequestReport {
repository: string
pullRequestNumber: number
baseSha: string
headSha: string
markdown: string
}
type GitHubRequest = (
endpoint: string,
method?: 'GET' | 'POST' | 'PATCH',
body?: { body: string }
) => Promise<unknown>
export async function postPerformanceComment(
report: PullRequestReport,
request: GitHubRequest
): Promise<'created' | 'updated' | 'stale'> {
if (
!/^[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.-]+$/.test(report.repository) ||
!Number.isSafeInteger(report.pullRequestNumber) ||
report.pullRequestNumber < 1 ||
!isSafeSha(report.baseSha) ||
!isSafeSha(report.headSha) ||
report.markdown.length > 60_000
) {
throw new Error('Invalid validated PR report metadata or size.')
}
const root = `/repos/${report.repository}`
// A PR may have advanced while validation or Bencher publishing was running.
const pullRequest = (await request(
`${root}/pulls/${report.pullRequestNumber}`
)) as { state: string; base: { sha: string }; head: { sha: string } }
if (
pullRequest.state !== 'open' ||
pullRequest.base.sha !== report.baseSha ||
pullRequest.head.sha !== report.headSha
) {
return 'stale'
}
const body = { body: `${COMMENT_MARKER}\n${report.markdown}` }
for (let page = 1; page <= 10; page++) {
const comments = (await request(
`${root}/issues/${report.pullRequestNumber}/comments?per_page=100&page=${page}`
)) as {
id: number
body: string
user: { login: string; type: string }
}[]
const existing = comments.find(
(comment) =>
comment.user.login === 'github-actions[bot]' &&
comment.user.type === 'Bot' &&
comment.body.startsWith(COMMENT_MARKER)
)
if (existing != null) {
await request(`${root}/issues/comments/${existing.id}`, 'PATCH', body)
return 'updated'
}
if (comments.length < 100) {
await request(
`${root}/issues/${report.pullRequestNumber}/comments`,
'POST',
body
)
return 'created'
}
}
throw new Error('Comment pagination exceeded its safety limit.')
}
if (import.meta.main) {
const argumentsMap = parseArguments(Bun.argv.slice(2))
const directory = requiredArgument(argumentsMap, 'directory')
const metadata = JSON.parse(
await readFile(path.join(directory, 'metadata.json'), 'utf8')
) as Omit<PullRequestReport, 'markdown'>
if (metadata.repository !== process.env.GITHUB_REPOSITORY) {
throw new Error('Report repository does not match the trusted workflow.')
}
if (metadata.pullRequestNumber != null) {
const token = process.env.GITHUB_TOKEN
if (token == null) throw new Error('GITHUB_TOKEN is required.')
const markdown = await readFile(
path.join(directory, 'performance-summary.md'),
'utf8'
)
const status = await postPerformanceComment(
{ ...metadata, markdown },
async (endpoint, method = 'GET', body) => {
const response = await fetch(`https://api.github.com${endpoint}`, {
method,
signal: AbortSignal.timeout(30_000),
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${token}`,
'X-GitHub-Api-Version': '2026-03-10',
'Content-Type': 'application/json',
},
body: body == null ? undefined : JSON.stringify(body),
})
if (!response.ok) {
throw new Error(`GitHub report request failed: ${response.status}.`)
}
return response.json()
}
)
console.info(`Paired performance PR comment: ${status}.`)
}
}