diff --git a/.github/workflows/generate-stats.yml b/.github/workflows/generate-stats.yml index 29d6886a..49df35bd 100644 --- a/.github/workflows/generate-stats.yml +++ b/.github/workflows/generate-stats.yml @@ -64,7 +64,6 @@ jobs: name: Prepare benchmarks runs-on: depot-ubuntu-24.04 outputs: - browserVersion: ${{ steps.chrome.outputs.version }} install: ${{ steps.set-matrix.outputs.install }} build: ${{ steps.set-matrix.outputs.build }} devServer: ${{ steps.set-matrix.outputs.devServer }} @@ -99,6 +98,18 @@ jobs: - name: Validate first-party dependency manifests run: pnpm --filter @framework-performance/stats-generator validate:first-party-dependencies + - name: Prepare starting CI results + env: + BROWSER_VERSION: ${{ steps.chrome.outputs.version }} + run: node packages/stats-generator/src/scripts/prepare-ci-results.ts prepared-results + + - name: Upload starting CI results + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: prepared-results + path: prepared-results/*.json + if-no-files-found: error + - name: Set matrices from TypeScript config id: set-matrix run: | @@ -163,8 +174,6 @@ jobs: path: artifacts - name: Validate all measurement artifacts and assembled results - env: - BROWSER_VERSION: ${{ needs.setup.outputs.browserVersion }} run: pnpm --filter @framework-performance/stats-generator validate:ci-stats "$GITHUB_WORKSPACE/artifacts" generate-stats: @@ -200,8 +209,6 @@ jobs: run: find artifacts -type f -name "*.json" 2>/dev/null || echo "No artifacts found" - name: Save CI stats - env: - BROWSER_VERSION: ${{ needs.setup.outputs.browserVersion }} run: pnpm --filter @framework-performance/stats-generator save:ci-stats "$GITHUB_WORKSPACE/artifacts" - name: Format diff --git a/packages/results/src/benchmark-schemas.ts b/packages/results/src/benchmark-schemas.ts index b97f759c..3e65d001 100644 --- a/packages/results/src/benchmark-schemas.ts +++ b/packages/results/src/benchmark-schemas.ts @@ -346,7 +346,7 @@ export const HTMLBaselineBenchmarkResult = z.object({ frameworkVersion: z.string(), // For HTML Baseline this is Node Version release date it is using - frameworkReleasedAt: z.string(), + frameworkReleasedAt: z.string().min(1), // Benchmarks ssrThroughput: SSRThroughputBenchmark.optional(), @@ -356,10 +356,8 @@ export const HTMLBaselineBenchmarkResult = z.object({ configuration: z.record(z.string(), z.unknown()), }) -/** - * Final Benchmark Result schema for all Meta-frameworks results - */ -export const MetaFrameworkBenchmarkResults = z.object({ +/** Validates framework metadata and any benchmarks collected during CI. */ +export const MetaFrameworkBenchmarkCIResults = z.object({ framework: z.enum( FrameworkOptions.filter((option) => option !== 'baseline-html'), ), @@ -368,7 +366,7 @@ export const MetaFrameworkBenchmarkResults = z.object({ runner: z.string(), browserVersion: z.string(), frameworkVersion: z.string(), - frameworkReleasedAt: z.string(), + frameworkReleasedAt: z.string().min(1), // Benchmarks (should always be options to support new benchmarks, removing old ones and frameworks that might not be able to complete one) dependencyCounts: DependencyCountBenchmark.optional(), @@ -384,11 +382,20 @@ export const MetaFrameworkBenchmarkResults = z.object({ ssrThroughput: SSRThroughputBenchmark.optional(), ssrLoad: SSRLoadBenchmark.optional(), ssrLoadRouterLinks: SSRLoadRouterLinksBenchmark.optional(), - - // Comes from packages/results/src/measurements-config.ts so leave as flexible type as this can change and is only needed as record - configuration: z.record(z.string(), z.unknown()), }) +// Validates a stored meta-framework result, including the configuration used for the run. +export const MetaFrameworkBenchmarkResults = + MetaFrameworkBenchmarkCIResults.extend({ + // Comes from packages/results/src/measurements-config.ts so leave as flexible type as this can change and is only needed as record + configuration: z.record(z.string(), z.unknown()), + }) + +export const BenchmarkCIResultSchema = z.discriminatedUnion('framework', [ + HTMLBaselineBenchmarkResult.omit({ configuration: true }), + MetaFrameworkBenchmarkCIResults, +]) + export const BenchmarkResultSchema = z.discriminatedUnion('framework', [ HTMLBaselineBenchmarkResult, MetaFrameworkBenchmarkResults, diff --git a/packages/stats-generator/src/initial-load-persistence.test.ts b/packages/stats-generator/src/initial-load-persistence.test.ts index 86850f04..50568b1a 100644 --- a/packages/stats-generator/src/initial-load-persistence.test.ts +++ b/packages/stats-generator/src/initial-load-persistence.test.ts @@ -19,7 +19,6 @@ test('CI requires, validates, and preserves initial load artifacts', async () => app: { ...framework.app!, measurements: ['ssrInitialLoad' as const] }, } const options = { - browserVersion: 'Google Chrome 156', validateOnly: true, frameworks: [configured], } @@ -33,8 +32,9 @@ test('CI requires, validates, and preserves initial load artifacts', async () => }, ]), ) + const previous = readBenchmarkResult('astro')! const artifact = { - frameworkVersion: readBenchmarkResult('astro')?.frameworkVersion, + frameworkVersion: previous.frameworkVersion, ssrInitialLoad: { name: 'ssrInitialLoad', project: 'app', @@ -42,6 +42,19 @@ test('CI requires, validates, and preserves initial load artifacts', async () => }, } try { + mkdirSync(join(directory, 'prepared-results')) + writeFileSync( + join(directory, 'prepared-results', 'astro.json'), + JSON.stringify({ + framework: 'astro', + frameworkVersion: previous.frameworkVersion, + frameworkReleasedAt: previous.frameworkReleasedAt, + projects: previous.projects, + timingMeasuredAt: previous.timingMeasuredAt, + runner: previous.runner, + browserVersion: 'Google Chrome 156', + }), + ) await assert.rejects( processCIStats(directory, options), /Missing or invalid measurement artifact/, diff --git a/packages/stats-generator/src/save-ci-stats.ts b/packages/stats-generator/src/save-ci-stats.ts index c78330f3..0b42be3a 100644 --- a/packages/stats-generator/src/save-ci-stats.ts +++ b/packages/stats-generator/src/save-ci-stats.ts @@ -3,6 +3,7 @@ import { saveBenchmarkResult, } from '@framework-performance/results/storage' import { + BenchmarkCIResultSchema, BenchmarkResultSchema, DependencyCountBenchmark, type BenchmarkResult, @@ -13,40 +14,22 @@ import { getSSRLoadPath } from './ssrLoad/config.ts' import { join } from 'node:path' import { getFrameworks } from './get-frameworks.ts' import { readFileSync } from 'node:fs' -import { getPublishTimes } from './release-dates.ts' -import { getPackageJsonDeps } from './utils.ts' import { benchmarkArtifacts, type BenchmarkArtifact, } from './benchmark-artifacts.ts' import type { FrameworkConfig } from './types.ts' -const publishTimesCache = new Map>>() - -async function getReleasedAt(packageName: string, version: string) { - if (!publishTimesCache.has(packageName)) { - publishTimesCache.set(packageName, getPublishTimes(packageName)) - } - const releasedAt = (await publishTimesCache.get(packageName)!)[version] - if (releasedAt == null) - throw new Error(`No publish time for ${packageName}@${version}`) - return releasedAt -} - export async function processCIStats( artifactsDir: string, { validateOnly = false, frameworks: configuredFrameworks, - browserVersion = process.env.BROWSER_VERSION, }: { validateOnly?: boolean frameworks?: FrameworkConfig[] - browserVersion?: string } = {}, ) { - const timingMeasuredAt = new Date().toISOString() - const runner = process.env.RUNNER_LABEL || 'local' const frameworks = configuredFrameworks ?? (await getFrameworks()) const results = new Map() @@ -54,19 +37,22 @@ export async function processCIStats( const previous = readBenchmarkResult(framework.name) const fresh: Record = {} const dependencyCounts: Record = {} - const artifactVersions = new Map() - const projects: Partial< - Record<'starter' | 'app', BenchmarkResult['projects']['app']> - > = { ...previous?.projects } + const prepared = BenchmarkCIResultSchema.parse( + JSON.parse( + readFileSync( + join(artifactsDir, 'prepared-results', `${framework.name}.json`), + 'utf8', + ), + ), + ) + if (prepared.framework !== framework.name) + throw new Error( + `Expected prepared result for ${framework.name}, got ${prepared.framework}`, + ) for (const project of ['starter', 'app'] as const) { const target = framework[project] if (!target) continue - projects[project] = { - path: `packages/${target.package}` as BenchmarkResult['projects']['app']['path'], - packageJson: getPackageJsonDeps(target.package), - } - for (const [ measurement, folder, @@ -89,9 +75,12 @@ export async function processCIStats( } for (const [key, value] of Object.entries(artifact)) { - if (key === 'frameworkVersion') - artifactVersions.set(folder, String(value)) - else if (key === 'dependencyCounts') + if (key === 'frameworkVersion') { + if (value !== prepared.frameworkVersion) + throw new Error( + `Framework version mismatch in ${file}: expected ${prepared.frameworkVersion}, got ${value}`, + ) + } else if (key === 'dependencyCounts') Object.assign(dependencyCounts, value) else if (key === 'starterNodeModuleSize') dependencyCounts[key] = value @@ -105,20 +94,6 @@ export async function processCIStats( !framework.starter?.measurements.length ) continue - const versions = new Set(artifactVersions.values()) - if (versions.size > 1) { - const details = [...artifactVersions] - .map(([artifact, version]) => `${artifact}=${version}`) - .join(', ') - throw new Error( - `Measurement artifacts disagree on framework version for ${framework.name}: ${details}`, - ) - } - const frameworkVersion = [...versions][0] ?? previous?.frameworkVersion - if (!frameworkVersion || frameworkVersion === 'unknown') - throw new Error(`No measured version for ${framework.name}`) - - const sameVersion = frameworkVersion === previous?.frameworkVersion if (framework.starter?.measurements.includes('dependencies')) { fresh.dependencyCounts = DependencyCountBenchmark.parse({ ...dependencyCounts, @@ -139,16 +114,8 @@ export async function processCIStats( }, } const assembled = BenchmarkResultSchema.parse({ + ...prepared, ...fresh, - framework: framework.name, - frameworkVersion, - frameworkReleasedAt: sameVersion - ? previous.frameworkReleasedAt - : await getReleasedAt(framework.frameworkPackage, frameworkVersion), - browserVersion, - timingMeasuredAt, - runner, - projects, configuration, }) results.set( diff --git a/packages/stats-generator/src/scripts/prepare-ci-results.ts b/packages/stats-generator/src/scripts/prepare-ci-results.ts new file mode 100644 index 00000000..964c0817 --- /dev/null +++ b/packages/stats-generator/src/scripts/prepare-ci-results.ts @@ -0,0 +1,62 @@ +import { mkdirSync, writeFileSync } from 'node:fs' +import { join } from 'node:path' +import { BenchmarkCIResultSchema } from '@framework-performance/results/benchmark-schemas' +import { readBenchmarkResult } from '@framework-performance/results/storage' +import { frameworksConfig } from '../frameworks.ts' +import { getPublishTimes } from '../release-dates.ts' +import { getPackageJsonDeps } from '../utils.ts' + +const outputDirectory = process.argv[2] +mkdirSync(outputDirectory, { recursive: true }) +const timingMeasuredAt = new Date().toISOString() + +for (const framework of frameworksConfig) { + const projects: Record = {} + let frameworkVersion: string | undefined + for (const project of ['starter', 'app'] as const) { + const target = framework[project] + if (!target) continue + const packageJson = getPackageJsonDeps(target.package) + const version = + framework.frameworkPackage === 'node' + ? process.versions.node + : (packageJson.dependencies[framework.frameworkPackage] ?? + packageJson.devDependencies[framework.frameworkPackage]) + if (!version) + throw new Error(`Missing framework version for ${target.package}`) + if (frameworkVersion && version !== frameworkVersion) + throw new Error( + `Framework versions differ between projects for ${framework.name}: ${frameworkVersion} and ${version}`, + ) + frameworkVersion = version + projects[project] = { + path: `packages/${target.package}`, + packageJson, + } + } + if (!frameworkVersion) + throw new Error(`Missing project for ${framework.name}`) + const previous = readBenchmarkResult(framework.name) + const frameworkReleasedAt = + previous?.frameworkVersion === frameworkVersion + ? previous.frameworkReleasedAt + : (await getPublishTimes(framework.frameworkPackage))[frameworkVersion] + if (!frameworkReleasedAt) + throw new Error( + `No publish time for ${framework.frameworkPackage}@${frameworkVersion}`, + ) + + const result = BenchmarkCIResultSchema.parse({ + framework: framework.name, + frameworkVersion, + frameworkReleasedAt, + projects, + timingMeasuredAt, + runner: process.env.RUNNER_LABEL || 'local', + browserVersion: process.env.BROWSER_VERSION, + }) + writeFileSync( + join(outputDirectory, `${framework.name}.json`), + JSON.stringify(result), + ) +}