ci(repo): track bundle size and performance of master builds (#12469)

- `Stats` workflow on master: posts the production build's file sizes (raw, gzip, Brotli; initial load vs. lazy; bytes per source package) and, once staging serves the commit, its page-load and pan/zoom measurements to the Excalidraw+ backend; OSS_STATS_TOKEN lives in the master-only `stats` environment
- `scripts/stats/`: bundle-stats Vite plugin and wrapper config (`yarn stats:bundle`), the Playwright runner (`yarn stats:perf`) and the poster
- `yarn stats:compare`: builds the checkout, serves it with `vite preview` and compares it with staging (alternating loads, Mann-Whitney U test); prints a table or writes CSV with `--out`
- `excalidraw:app-start` mark in the app's entry, ending the runs' "HTML + JS" phase
- `playwright` 1.58.2 dev dependency (installs no browsers by itself)

Claude Opus 5.5 (xhigh)
This commit is contained in:
David Luzar authored and GitHub committed 2026-10-27 15:34:46 +01:00
1 parent b7de45e881
commit 4eb1e648aa
8 files changed
+1472

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
name: Stats
# Posts every master commit's production build (bundle size) and, once
# staging.excalidraw.com serves the commit, its page-load measurements to the
# Excalidraw+ admin's Bundle size and Performance pages (scripts/stats/).
# OSS_STATS_TOKEN lives in the `stats` environment, which only master can use.
on:
push:
branches: master
workflow_dispatch:
permissions:
contents: read
jobs:
bundle:
name: Bundle size
runs-on: ubuntu-latest
environment: stats
steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
with:
node-version: 20.x
- name: Install
run: yarn install --frozen-lockfile
- name: Build with bundle stats
run: yarn stats:bundle
env:
VITE_APP_GIT_SHA: ${{ github.sha }}
VITE_APP_ENABLE_ESLINT: "false"
- name: Post to the Excalidraw+ admin
run: node scripts/stats/post.mjs bundle excalidraw-app/build/bundle-stats.json
env:
OSS_STATS_TOKEN: ${{ secrets.OSS_STATS_TOKEN }}
perf:
name: Performance
runs-on: ubuntu-latest
environment: stats
# staging serves only the latest commit, so a newer push takes over
concurrency:
group: stats-perf
cancel-in-progress: true
timeout-minutes: 40
steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
with:
node-version: 20.x
- name: Install
run: |
yarn install --frozen-lockfile
npx playwright install --with-deps chromium
- name: Measure staging once it serves this commit
run: yarn stats:perf --wait-for ${{ github.sha }} --out perf.json
- name: Post to the Excalidraw+ admin
# not measured when staging didn't serve the commit in time
if: hashFiles('perf.json') != ''
run: node scripts/stats/post.mjs perf perf.json
env:
OSS_STATS_TOKEN: ${{ secrets.OSS_STATS_TOKEN }}
+3
View File
@@ -6,6 +6,9 @@ import "../excalidraw-app/sentry";
import ExcalidrawApp from "./App";
// the entry's JavaScript and CSS loaded and ran (`scripts/stats/perf.mjs`)
performance.mark("excalidraw:app-start");
window.__EXCALIDRAW_SHA__ = import.meta.env.VITE_APP_GIT_SHA;
const rootElement = document.getElementById("root")!;
const root = createRoot(rootElement);
+4
View File
@@ -31,6 +31,7 @@
"jsdom": "22.1.0",
"lint-staged": "12.3.7",
"pepjs": "0.5.3",
"playwright": "1.58.2",
"prettier": "2.6.2",
"rewire": "6.0.0",
"rimraf": "^5.0.0",
@@ -60,6 +61,9 @@
"build:laser-pointer": "yarn --cwd ./packages/laser-pointer build:esm",
"build:packages": "yarn build:common && yarn build:fractional-indexing && yarn build:laser-pointer && yarn build:math && yarn build:element && yarn build:excalidraw",
"build:version": "yarn --cwd ./excalidraw-app build:version",
"stats:bundle": "yarn --cwd ./excalidraw-app cross-env VITE_APP_ENABLE_TRACKING=true vite build --config ../scripts/stats/vite.config.mts",
"stats:perf": "node ./scripts/stats/perf.mjs",
"stats:compare": "node ./scripts/stats/perf.mjs --compare",
"build": "yarn --cwd ./excalidraw-app build",
"build:preview": "yarn --cwd ./excalidraw-app build:preview",
"start": "yarn --cwd ./excalidraw-app start",
+196
View File
@@ -0,0 +1,196 @@
import fs from "node:fs";
import path from "node:path";
import zlib from "node:zlib";
import type { Plugin, Rollup } from "vite";
/**
* One file of the build, as `POST /api/oss/bundle-runs` stores it (see
* `scripts/stats/post.mjs`).
*/
type BundleStatsAsset = {
/** the same from build to build: a chunk's entry module or name, an asset's source file */
name: string;
/** the emitted file, e.g. `assets/index-CNToBGx8.js` */
file: string;
type: "js" | "css" | "html" | "font" | "other";
/** loaded with the page: the HTML, the entry chunk, its static imports and their CSS */
initial: boolean;
bytes: number;
gzipBytes: number;
brotliBytes: number;
/** a chunk's source packages → their bytes in it before minification */
packages: Record<string, number>;
};
const BUNDLE_STATS_FILE = "bundle-stats.json";
const REPO_ROOT = path.resolve(__dirname, "../..");
const NODE_MODULES = "/node_modules/";
const getType = (fileName: string): BundleStatsAsset["type"] =>
fileName.endsWith(".js")
? "js"
: fileName.endsWith(".css")
? "css"
: fileName.endsWith(".html")
? "html"
: /\.(woff2?|ttf|otf)$/.test(fileName)
? "font"
: "other";
/** A module id without Rollup's virtual-module prefix and query. */
const cleanId = (id: string) => id.replace(/^\0/, "").split("?")[0];
/** A module's path from the repo root, or from `node_modules` for dependencies. */
const toRepoPath = (id: string) => {
const nodeModules = id.lastIndexOf(NODE_MODULES);
return nodeModules >= 0
? id.slice(nodeModules + 1)
: path.relative(REPO_ROOT, id).split(path.sep).join("/");
};
/**
* The package a module comes from: its npm package, `@excalidraw/<name>` for
* the monorepo's packages, the app, or the bundler's own helpers.
*/
const getPackage = (moduleId: string) => {
const id = cleanId(moduleId);
const nodeModules = id.lastIndexOf(NODE_MODULES);
if (nodeModules >= 0) {
const [scope, name] = id
.slice(nodeModules + NODE_MODULES.length)
.split("/");
return scope.startsWith("@") ? `${scope}/${name}` : scope;
}
if (!path.isAbsolute(id)) {
// e.g. vite/preload-helper, commonjsHelpers
return "(bundler)";
}
const [dir, name] = toRepoPath(id).split("/");
return dir === "packages" ? `@excalidraw/${name}` : dir;
};
const getSizes = (source: string | Uint8Array) => {
const buffer = Buffer.from(source);
return {
bytes: buffer.length,
gzipBytes: zlib.gzipSync(buffer, { level: 9 }).length,
brotliBytes: zlib.brotliCompressSync(buffer, {
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 11 },
}).length,
};
};
/** The chunks loaded with the page: the entries and what they import statically, and their CSS. */
const getInitialFiles = (chunks: Rollup.OutputChunk[]) => {
const chunksByFile = new Map(chunks.map((chunk) => [chunk.fileName, chunk]));
const initial = new Set<string>();
const stack = chunks.filter((chunk) => chunk.isEntry);
while (stack.length) {
const chunk = stack.pop()!;
if (initial.has(chunk.fileName)) {
continue;
}
initial.add(chunk.fileName);
chunk.viteMetadata?.importedCss.forEach((css) => initial.add(css));
for (const fileName of chunk.imports) {
const imported = chunksByFile.get(fileName);
if (imported) {
stack.push(imported);
}
}
}
return initial;
};
/**
* Writes `bundle-stats.json` next to the build: every emitted file (source
* maps aside) with its minified, gzip and Brotli (maximum compression) sizes,
* whether the page loads it initially and, for chunks, the bytes per source
* package. Master CI (`.github/workflows/stats.yml`) posts it to the
* Excalidraw+ admin (`scripts/stats/post.mjs`); applied by
* `scripts/stats/vite.config.mts`, not by the app's own builds.
*/
export const bundleStatsPlugin = (): Plugin => {
// the assets' source files are relative to it
let root = process.cwd();
return {
name: "excalidraw-bundle-stats",
apply: "build",
enforce: "post",
configResolved(config) {
root = config.root;
},
writeBundle(options, bundle) {
const outputs = Object.values(bundle).filter(
(output) => !output.fileName.endsWith(".map"),
);
const chunks = outputs.filter(
(output): output is Rollup.OutputChunk => output.type === "chunk",
);
const initialFiles = getInitialFiles(chunks);
const usedNames = new Map<string, number>();
// unique within the build: Rollup may give shared chunks the same name
const uniqueName = (name: string) => {
const count = (usedNames.get(name) ?? 0) + 1;
usedNames.set(name, count);
return count > 1 ? `${name} (${count})` : name;
};
const assets = outputs.map((output): BundleStatsAsset => {
const type = getType(output.fileName);
if (output.type === "chunk") {
const packages: Record<string, number> = {};
for (const [id, { renderedLength }] of Object.entries(
output.modules,
)) {
if (renderedLength) {
const name = getPackage(id);
packages[name] = (packages[name] ?? 0) + renderedLength;
}
}
return {
name: uniqueName(
output.facadeModuleId
? toRepoPath(cleanId(output.facadeModuleId))
: `${output.name}.js`,
),
file: output.fileName,
type,
initial: initialFiles.has(output.fileName),
...getSizes(output.code),
packages,
};
}
const source = output.originalFileNames?.[0];
return {
name: uniqueName(
source
? toRepoPath(path.resolve(root, source))
: // emitted without a source (the HTML, CSS of chunks): the file without its hash
output.fileName.replace(/-[\w-]{8}(\.[^./]+)$/, "$1"),
),
file: output.fileName,
type,
initial: type === "html" || initialFiles.has(output.fileName),
...getSizes(output.source),
packages: {},
};
});
fs.writeFileSync(
path.join(options.dir!, BUNDLE_STATS_FILE),
JSON.stringify({ assets }, null, 2),
);
},
};
};
File diff suppressed because it is too large. Load diff
+87
View File
@@ -0,0 +1,87 @@
#!/usr/bin/env node
/**
* Posts a performance run (`perf.mjs --out`) or a build's bundle stats
* (`yarn stats:bundle` → `excalidraw-app/build/bundle-stats.json`) to the
* Excalidraw+ api, which stores it for the admin's Performance and Bundle size
* pages. Needs `OSS_STATS_TOKEN`; the commit is `--commit`, `GITHUB_SHA` or
* HEAD, read from this repo.
*
* node scripts/stats/post.mjs perf perf.json
* node scripts/stats/post.mjs bundle excalidraw-app/build/bundle-stats.json
*/
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import { parseArgs } from "node:util";
const {
positionals: [kind, file],
values: args,
} = parseArgs({
allowPositionals: true,
options: {
commit: { type: "string" },
api: {
type: "string",
default:
process.env.OSS_STATS_API_URL || "https://backend.excalidraw.com",
},
},
});
/** The api's `OSS_STATS_TOKEN_HEADER`. */
const TOKEN_HEADER = "x-oss-stats-token";
if (!["perf", "bundle"].includes(kind) || !file) {
console.error(
"Usage: post.mjs <perf|bundle> <file.json> [--commit <sha>] [--api <url>]",
);
process.exit(1);
}
const token = process.env.OSS_STATS_TOKEN;
if (!token) {
console.error("OSS_STATS_TOKEN is not set");
process.exit(1);
}
const git = (...gitArgs) =>
execFileSync("git", gitArgs, { encoding: "utf8" }).trim();
const commitSha = git(
"rev-parse",
args.commit || process.env.GITHUB_SHA || "HEAD",
);
const commit = {
commitSha,
commitTitle: git("log", "-1", "--format=%s", commitSha),
runUrl: process.env.GITHUB_RUN_ID
? `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`
: null,
};
const data = JSON.parse(fs.readFileSync(file, "utf8"));
const body =
kind === "perf"
? { ...commit, ...data }
: {
...commit,
committedAt: git("log", "-1", "--format=%cI", commitSha),
...data,
};
const response = await fetch(`${args.api}/api/oss/${kind}-runs`, {
method: "POST",
headers: { "content-type": "application/json", [TOKEN_HEADER]: token },
body: JSON.stringify(body),
});
if (!response.ok) {
console.error(
`${response.status} ${response.statusText}: ${await response.text()}`,
);
process.exit(1);
}
const { id } = await response.json();
console.info(`Stored the ${kind} run of ${commitSha.slice(0, 7)} as ${id}`);
+17
View File
@@ -0,0 +1,17 @@
import { defineConfig, mergeConfig } from "vite";
import appConfig from "../../excalidraw-app/vite.config.mts";
import { bundleStatsPlugin } from "./bundle-stats-plugin.mts";
/**
* The app's production build plus `bundle-stats.json` (`yarn stats:bundle`,
* run from `excalidraw-app/` like the app's own build). The stats aren't part
* of the deployed builds, so they only cost CI the extra compression.
*/
export default defineConfig(async (env) => {
const config =
typeof appConfig === "function" ? await appConfig(env) : await appConfig;
return mergeConfig(config, { plugins: [bundleStatsPlugin()] });
});
+19
View File
@@ -6605,6 +6605,11 @@ fs.realpath@^1.0.0:
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==
fsevents@2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
fsevents@~2.3.2, fsevents@~2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"
@@ -8423,6 +8428,20 @@ pkg-types@^1.3.1:
mlly "^1.7.4"
pathe "^2.0.1"
playwright-core@1.58.2:
version "1.58.2"
resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.58.2.tgz#ac5f5b4b10d29bcf934415f0b8d133b34b0dcb13"
integrity sha512-yZkEtftgwS8CsfYo7nm0KE8jsvm6i/PTgVtB8DL726wNf6H2IMsDuxCpJj59KDaxCtSnrWan2AeDqM7JBaultg==
playwright@1.58.2:
version "1.58.2"
resolved "https://registry.yarnpkg.com/playwright/-/playwright-1.58.2.tgz#afe547164539b0bcfcb79957394a7a3fa8683cfd"
integrity sha512-vA30H8Nvkq/cPBnNw4Q8TWz1EJyqgpuinBcHET0YVJVFldr8JDNiU9LaWAE1KqSkRYazuaBhTpB5ZzShOezQ6A==
dependencies:
playwright-core "1.58.2"
optionalDependencies:
fsevents "2.3.2"
png-chunk-text@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/png-chunk-text/-/png-chunk-text-1.0.0.tgz#1c6006d8e34ba471d38e1c9c54b3f53e1085e18f"