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 = {
// General metrics
nodeModulesSize: 'node_modules Size',
swcBinarySize: 'SWC Binary Size',
swcBinaryStrippedSize: 'SWC Binary Size (stripped)',
swcBinaryGzipSize: 'SWC Binary Size (gzip)',
}
// Group configuration for organizing the comment
......@@ -213,6 +215,16 @@ const METRIC_THRESHOLDS = {
// SWC native binary (~tens of MB): deterministic, but smaller baseline
// <10KB AND <0.5%, OR <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
// <2KB AND <1%, OR <0.1%
......@@ -988,33 +1000,51 @@ function getLatestHistoricalValue(history, metricKey) {
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.
function generateNativeBinarySection(mainStats, diffStats, history) {
const mainGeneral = mainStats?.General || {}
const diffGeneral = diffStats?.General || {}
const mainVal = mainGeneral.swcBinarySize
const diffVal = diffGeneral.swcBinarySize
const rows = []
let hasTrend = false
for (const { key, label } of NATIVE_BINARY_METRICS) {
const mainVal = mainGeneral[key]
const diffVal = diffGeneral[key]
if (typeof mainVal !== 'number' && typeof diffVal !== 'number') continue
const mainStr = prettify(mainVal, 'bytes')
const diffStr = prettify(diffVal, 'bytes')
const change = formatChange(mainVal, diffVal, 'bytes', key)
const histValues = getHistoricalValues(history, key)
const sparkline = generateTrendBar(histValues)
if (sparkline) hasTrend = true
// Nothing to show if we don't have any measurement
if (typeof mainVal !== 'number' && typeof diffVal !== 'number') return ''
rows.push({ label, mainStr, diffStr, changeText: change.text, sparkline })
}
const mainStr = prettify(mainVal, 'bytes')
const diffStr = prettify(diffVal, 'bytes')
const change = formatChange(mainVal, diffVal, 'bytes', 'swcBinarySize')
const histValues = getHistoricalValues(history, 'swcBinarySize')
const sparkline = generateTrendBar(histValues)
if (rows.length === 0) return ''
const hasTrend = Boolean(sparkline)
const header = hasTrend
? `| Metric | Canary | PR | Change | Trend |
|:-------|-------:|---:|-------:|:-----:|`
: `| Metric | Canary | PR | Change |
|:-------|-------:|---:|-------:|`
const row = hasTrend
? `| SWC Binary Size | ${mainStr} | ${diffStr} | ${change.text} | ${sparkline} |`
: `| SWC Binary Size | ${mainStr} | ${diffStr} | ${change.text} |`
const body = rows
.map((r) =>
hasTrend
? `| ${r.label} | ${r.mainStr} | ${r.diffStr} | ${r.changeText} | ${r.sparkline} |`
: `| ${r.label} | ${r.mainStr} | ${r.diffStr} | ${r.changeText} |`
)
.join('\n')
return `<details>
<summary><strong>🦀 Native Binary</strong></summary>
......@@ -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.
${header}
${row}
${body}
</details>
......@@ -1078,15 +1108,14 @@ module.exports = async function addComment(
// canary baseline with the last recorded value from KV history so the
// diff is meaningful. Canary runs skip this and keep the measured value.
if (!actionInfo.isRelease && result.mainRepoStats?.General) {
const historicalSwcSize = getLatestHistoricalValue(
history,
'swcBinarySize'
)
if (typeof historicalSwcSize === 'number') {
result.mainRepoStats.General.swcBinarySize = historicalSwcSize
} else {
// No history yet — hide the canary value so the table renders N/A
delete result.mainRepoStats.General.swcBinarySize
for (const { key } of NATIVE_BINARY_METRICS) {
const historical = getLatestHistoricalValue(history, key)
if (typeof historical === 'number') {
result.mainRepoStats.General[key] = historical
} else {
// No history yet — hide the canary value so the table renders N/A
delete result.mainRepoStats.General[key]
}
}
}
......
const path = require('path')
const os = require('os')
const fs = require('fs/promises')
const gzipSize = require('gzip-size')
const glob = require('../util/glob')
const exec = require('../util/exec')
const logger = require('../util/logger')
......@@ -13,25 +15,69 @@ const { calcStats } = require('../util/stats')
// From src/run/index.js → .github/actions/next-stats-action/native
const nativeBinaryDir = path.join(__dirname, '../../native')
// Sum the size of all *.node files in the action's native/ directory.
async function getSwcBinarySize() {
async function listNativeBinaries() {
try {
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 found = 0
for (const file of files) {
const stat = await fs.stat(file)
if (stat.isFile()) {
total += stat.size
found++
}
}
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
let found = 0
for (const entry of entries) {
if (!entry.endsWith('.node')) continue
const stat = await fs.stat(path.join(nativeBinaryDir, entry))
if (stat.isFile()) {
total += stat.size
found++
}
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
}
if (found === 0) return null
return total
} catch (err) {
logger(`Unable to measure SWC binary size: ${err.message}`)
logger(`Unable to measure stripped SWC binary size: ${err.message}`)
return null
} finally {
if (tmpDir) {
await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => {})
}
}
}
......@@ -84,6 +130,8 @@ async function runConfigs(
General: {
nodeModulesSize: null,
swcBinarySize: null,
swcBinaryStrippedSize: null,
swcBinaryGzipSize: null,
},
}
......@@ -110,7 +158,12 @@ async function runConfigs(
curStats.General.nodeModulesSize = await getDirSize(
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
......
......@@ -35,6 +35,9 @@ const mockResults = [
buildDurationWebpack: 14000,
buildDurationCachedWebpack: 13500,
nodeModulesSize: 250000000,
swcBinarySize: 108 * 1024 * 1024, // ~108 MB
swcBinaryStrippedSize: 92 * 1024 * 1024, // ~92 MB (debug info removed)
swcBinaryGzipSize: 38 * 1024 * 1024, // ~38 MB
},
},
diffRepoStats: {
......@@ -53,6 +56,9 @@ const mockResults = [
buildDurationWebpack: 13800,
buildDurationCachedWebpack: 13600,
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,
......@@ -84,6 +90,16 @@ if (withHistory) {
nextDevColdReadyDurationTurbo: 400 + Math.random() * 100, // varies 400-500
nextDevWarmReadyDurationTurbo: 300 + Math.random() * 50,
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