Commit 438bccad authored by Tobias Koppers's avatar Tobias Koppers

CI: Also track stripped and gzipped size of the SWC native binary

Addresses PR feedback: the raw size of the unstripped .node binary is
dominated by debug info, so small code changes are hard to see. The
Native Binary section now reports raw, stripped (via \`strip
--strip-unneeded\`), and gzipped size side by side. Canary baselines for
all three metrics are sourced from KV history the same way as the raw
size, and are persisted on canary runs.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent bb2a4457
...@@ -56,6 +56,8 @@ const METRIC_LABELS = { ...@@ -56,6 +56,8 @@ const METRIC_LABELS = {
// General metrics // General metrics
nodeModulesSize: 'node_modules Size', nodeModulesSize: 'node_modules Size',
swcBinarySize: 'SWC Binary Size', swcBinarySize: 'SWC Binary Size',
swcBinaryStrippedSize: 'SWC Binary Size (stripped)',
swcBinaryGzipSize: 'SWC Binary Size (gzip)',
} }
// Group configuration for organizing the comment // Group configuration for organizing the comment
...@@ -213,6 +215,16 @@ const METRIC_THRESHOLDS = { ...@@ -213,6 +215,16 @@ const METRIC_THRESHOLDS = {
// SWC native binary (~tens of MB): deterministic, but smaller baseline // SWC native binary (~tens of MB): deterministic, but smaller baseline
// <10KB AND <0.5%, OR <0.05% // <10KB AND <0.5%, OR <0.05%
swcBinarySize: { absoluteMin: 10240, percentMin: 0.5, percentOnly: 0.05 }, swcBinarySize: { absoluteMin: 10240, percentMin: 0.5, percentOnly: 0.05 },
swcBinaryStrippedSize: {
absoluteMin: 10240,
percentMin: 0.5,
percentOnly: 0.05,
},
swcBinaryGzipSize: {
absoluteMin: 10240,
percentMin: 0.5,
percentOnly: 0.05,
},
// Bundle sizes (KB-MB): deterministic // Bundle sizes (KB-MB): deterministic
// <2KB AND <1%, OR <0.1% // <2KB AND <1%, OR <0.1%
...@@ -988,33 +1000,51 @@ function getLatestHistoricalValue(history, metricKey) { ...@@ -988,33 +1000,51 @@ function getLatestHistoricalValue(history, metricKey) {
return undefined return undefined
} }
// Metric keys reported in the Native Binary section.
const NATIVE_BINARY_METRICS = [
{ key: 'swcBinarySize', label: 'Raw' },
{ key: 'swcBinaryStrippedSize', label: 'Stripped' },
{ key: 'swcBinaryGzipSize', label: 'Gzipped' },
]
// Generate the dedicated Native Binary section shown after Bundle Sizes. // Generate the dedicated Native Binary section shown after Bundle Sizes.
function generateNativeBinarySection(mainStats, diffStats, history) { function generateNativeBinarySection(mainStats, diffStats, history) {
const mainGeneral = mainStats?.General || {} const mainGeneral = mainStats?.General || {}
const diffGeneral = diffStats?.General || {} const diffGeneral = diffStats?.General || {}
const mainVal = mainGeneral.swcBinarySize const rows = []
const diffVal = diffGeneral.swcBinarySize let hasTrend = false
// Nothing to show if we don't have any measurement for (const { key, label } of NATIVE_BINARY_METRICS) {
if (typeof mainVal !== 'number' && typeof diffVal !== 'number') return '' const mainVal = mainGeneral[key]
const diffVal = diffGeneral[key]
if (typeof mainVal !== 'number' && typeof diffVal !== 'number') continue
const mainStr = prettify(mainVal, 'bytes') const mainStr = prettify(mainVal, 'bytes')
const diffStr = prettify(diffVal, 'bytes') const diffStr = prettify(diffVal, 'bytes')
const change = formatChange(mainVal, diffVal, 'bytes', 'swcBinarySize') const change = formatChange(mainVal, diffVal, 'bytes', key)
const histValues = getHistoricalValues(history, 'swcBinarySize') const histValues = getHistoricalValues(history, key)
const sparkline = generateTrendBar(histValues) const sparkline = generateTrendBar(histValues)
if (sparkline) hasTrend = true
rows.push({ label, mainStr, diffStr, changeText: change.text, sparkline })
}
if (rows.length === 0) return ''
const hasTrend = Boolean(sparkline)
const header = hasTrend const header = hasTrend
? `| Metric | Canary | PR | Change | Trend | ? `| Metric | Canary | PR | Change | Trend |
|:-------|-------:|---:|-------:|:-----:|` |:-------|-------:|---:|-------:|:-----:|`
: `| Metric | Canary | PR | Change | : `| Metric | Canary | PR | Change |
|:-------|-------:|---:|-------:|` |:-------|-------:|---:|-------:|`
const row = hasTrend const body = rows
? `| SWC Binary Size | ${mainStr} | ${diffStr} | ${change.text} | ${sparkline} |` .map((r) =>
: `| SWC Binary Size | ${mainStr} | ${diffStr} | ${change.text} |` hasTrend
? `| ${r.label} | ${r.mainStr} | ${r.diffStr} | ${r.changeText} | ${r.sparkline} |`
: `| ${r.label} | ${r.mainStr} | ${r.diffStr} | ${r.changeText} |`
)
.join('\n')
return `<details> return `<details>
<summary><strong>🦀 Native Binary</strong></summary> <summary><strong>🦀 Native Binary</strong></summary>
...@@ -1022,7 +1052,7 @@ function generateNativeBinarySection(mainStats, diffStats, history) { ...@@ -1022,7 +1052,7 @@ function generateNativeBinarySection(mainStats, diffStats, history) {
Size of the native SWC binary (\`packages/next-swc/native/*.node\`). The Canary column is the most recent value recorded on the canary branch. Size of the native SWC binary (\`packages/next-swc/native/*.node\`). The Canary column is the most recent value recorded on the canary branch.
${header} ${header}
${row} ${body}
</details> </details>
...@@ -1078,15 +1108,14 @@ module.exports = async function addComment( ...@@ -1078,15 +1108,14 @@ module.exports = async function addComment(
// canary baseline with the last recorded value from KV history so the // canary baseline with the last recorded value from KV history so the
// diff is meaningful. Canary runs skip this and keep the measured value. // diff is meaningful. Canary runs skip this and keep the measured value.
if (!actionInfo.isRelease && result.mainRepoStats?.General) { if (!actionInfo.isRelease && result.mainRepoStats?.General) {
const historicalSwcSize = getLatestHistoricalValue( for (const { key } of NATIVE_BINARY_METRICS) {
history, const historical = getLatestHistoricalValue(history, key)
'swcBinarySize' if (typeof historical === 'number') {
) result.mainRepoStats.General[key] = historical
if (typeof historicalSwcSize === 'number') {
result.mainRepoStats.General.swcBinarySize = historicalSwcSize
} else { } else {
// No history yet — hide the canary value so the table renders N/A // No history yet — hide the canary value so the table renders N/A
delete result.mainRepoStats.General.swcBinarySize delete result.mainRepoStats.General[key]
}
} }
} }
......
const path = require('path') const path = require('path')
const os = require('os')
const fs = require('fs/promises') const fs = require('fs/promises')
const gzipSize = require('gzip-size')
const glob = require('../util/glob') const glob = require('../util/glob')
const exec = require('../util/exec') const exec = require('../util/exec')
const logger = require('../util/logger') const logger = require('../util/logger')
...@@ -13,25 +15,69 @@ const { calcStats } = require('../util/stats') ...@@ -13,25 +15,69 @@ const { calcStats } = require('../util/stats')
// From src/run/index.js → .github/actions/next-stats-action/native // From src/run/index.js → .github/actions/next-stats-action/native
const nativeBinaryDir = path.join(__dirname, '../../native') const nativeBinaryDir = path.join(__dirname, '../../native')
// Sum the size of all *.node files in the action's native/ directory. async function listNativeBinaries() {
async function getSwcBinarySize() {
try { try {
const entries = await fs.readdir(nativeBinaryDir) const entries = await fs.readdir(nativeBinaryDir)
return entries
.filter((e) => e.endsWith('.node'))
.map((e) => path.join(nativeBinaryDir, e))
} catch {
return []
}
}
// Raw file size on disk (sum across all *.node files).
async function getSwcBinarySize(files) {
let total = 0 let total = 0
let found = 0 let found = 0
for (const entry of entries) { for (const file of files) {
if (!entry.endsWith('.node')) continue const stat = await fs.stat(file)
const stat = await fs.stat(path.join(nativeBinaryDir, entry))
if (stat.isFile()) { if (stat.isFile()) {
total += stat.size total += stat.size
found++ found++
} }
} }
if (found === 0) return null return found === 0 ? null : total
}
// Gzipped size of the binary (sum across all *.node files).
async function getSwcBinaryGzipSize(files) {
if (files.length === 0) return null
let total = 0
for (const file of files) {
total += await gzipSize.file(file)
}
return total
}
// Stripped size: copy the binary, run `strip --strip-unneeded`, then stat it.
// Returns null if `strip` is unavailable or fails.
async function getSwcBinaryStrippedSize(files) {
if (files.length === 0) return null
let tmpDir
try {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'swc-strip-'))
let total = 0
for (const file of files) {
const dest = path.join(tmpDir, path.basename(file))
await fs.copyFile(file, dest)
await exec.spawnPromise(
`strip --strip-unneeded ${JSON.stringify(dest)}`,
{
stdio: 'pipe',
}
)
const stat = await fs.stat(dest)
total += stat.size
}
return total return total
} catch (err) { } catch (err) {
logger(`Unable to measure SWC binary size: ${err.message}`) logger(`Unable to measure stripped SWC binary size: ${err.message}`)
return null return null
} finally {
if (tmpDir) {
await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => {})
}
} }
} }
...@@ -84,6 +130,8 @@ async function runConfigs( ...@@ -84,6 +130,8 @@ async function runConfigs(
General: { General: {
nodeModulesSize: null, nodeModulesSize: null,
swcBinarySize: null, swcBinarySize: null,
swcBinaryStrippedSize: null,
swcBinaryGzipSize: null,
}, },
} }
...@@ -110,7 +158,12 @@ async function runConfigs( ...@@ -110,7 +158,12 @@ async function runConfigs(
curStats.General.nodeModulesSize = await getDirSize( curStats.General.nodeModulesSize = await getDirSize(
path.join(statsAppDir, 'node_modules') path.join(statsAppDir, 'node_modules')
) )
curStats.General.swcBinarySize = await getSwcBinarySize() const nativeBinaries = await listNativeBinaries()
curStats.General.swcBinarySize = await getSwcBinarySize(nativeBinaries)
curStats.General.swcBinaryStrippedSize =
await getSwcBinaryStrippedSize(nativeBinaries)
curStats.General.swcBinaryGzipSize =
await getSwcBinaryGzipSize(nativeBinaries)
} }
// Run builds for selected bundler(s) and collect stats separately // Run builds for selected bundler(s) and collect stats separately
......
...@@ -35,6 +35,9 @@ const mockResults = [ ...@@ -35,6 +35,9 @@ const mockResults = [
buildDurationWebpack: 14000, buildDurationWebpack: 14000,
buildDurationCachedWebpack: 13500, buildDurationCachedWebpack: 13500,
nodeModulesSize: 250000000, nodeModulesSize: 250000000,
swcBinarySize: 108 * 1024 * 1024, // ~108 MB
swcBinaryStrippedSize: 92 * 1024 * 1024, // ~92 MB (debug info removed)
swcBinaryGzipSize: 38 * 1024 * 1024, // ~38 MB
}, },
}, },
diffRepoStats: { diffRepoStats: {
...@@ -53,6 +56,9 @@ const mockResults = [ ...@@ -53,6 +56,9 @@ const mockResults = [
buildDurationWebpack: 13800, buildDurationWebpack: 13800,
buildDurationCachedWebpack: 13600, buildDurationCachedWebpack: 13600,
nodeModulesSize: 251000000, nodeModulesSize: 251000000,
swcBinarySize: 115 * 1024 * 1024, // ~115 MB (+7 MB, significant regression)
swcBinaryStrippedSize: 98 * 1024 * 1024, // ~98 MB
swcBinaryGzipSize: 40 * 1024 * 1024, // ~40 MB
}, },
}, },
diffs: null, diffs: null,
...@@ -84,6 +90,16 @@ if (withHistory) { ...@@ -84,6 +90,16 @@ if (withHistory) {
nextDevColdReadyDurationTurbo: 400 + Math.random() * 100, // varies 400-500 nextDevColdReadyDurationTurbo: 400 + Math.random() * 100, // varies 400-500
nextDevWarmReadyDurationTurbo: 300 + Math.random() * 50, nextDevWarmReadyDurationTurbo: 300 + Math.random() * 50,
buildDurationTurbo: 4000 + Math.random() * 1000, buildDurationTurbo: 4000 + Math.random() * 1000,
// Slowly-growing binary sizes across recent canary runs
swcBinarySize: Math.round(
(100 + (9 - i) * 0.9) * 1024 * 1024 + Math.random() * 512 * 1024
),
swcBinaryStrippedSize: Math.round(
(85 + (9 - i) * 0.8) * 1024 * 1024 + Math.random() * 512 * 1024
),
swcBinaryGzipSize: Math.round(
(35 + (9 - i) * 0.3) * 1024 * 1024 + Math.random() * 256 * 1024
),
}, },
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment