Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Bundle-size: apples-for-apples PR check + delta-aware stats logger (#76409)

Romeo Van Snick91bdbd3f2120
diff --git a/.github/actions/sdk-bundle-size-check/action.yml b/.github/actions/sdk-bundle-size-check/action.yml
index cf80c0b31c1..a992bc3e375 100644
--- a/.github/actions/sdk-bundle-size-check/action.yml
+++ b/.github/actions/sdk-bundle-size-check/action.yml
@@ -1,15 +1,17 @@
name: Embedding SDK Bundle Size Check
description: |
- Downloads the current branch's uberjar and builds the base ref's
- embedding-sdk.js from source, compares their sizes,
- and sets output if the size change exceeds the threshold.
+ Measures app + embedding SDK bundle sizes from the current and base-ref
+ uberjars (reusing the builds that already ran) plus their bundle-stats
+ artifacts, and flags changes to the chunked SDK's reachable, gzipped size
+ beyond the threshold. Skips when the base-ref artifacts aren't available
+ rather than rebuilding from source (which would compare across pipelines).
inputs:
edition:
description: 'Metabase edition (ee or oss)'
required: true
base-ref-commit:
- description: 'Commit SHA of the base ref (e.g. merge base).'
+ description: 'Commit SHA of the base ref.'
required: true
threshold-percent:
description: 'Percent threshold for bundle size change'
@@ -26,51 +28,74 @@ outputs:
runs:
using: "composite"
steps:
- - name: Prepare backend
- uses: ./.github/actions/prepare-backend
- - name: Prepare frontend
- uses: ./.github/actions/prepare-frontend
- name: Download current uberjar
uses: ./.github/actions/fetch-artifact
with:
commit: ${{ github.event.pull_request.head.sha || github.sha }}
edition: ${{ inputs.edition }}
extract-path: temp/current-uberjar
- - name: Extract embedding-sdk bundles from current uberjar
- shell: bash
- run: |
- # Extract preserving the jar's internal paths under resources/ so the
- # shared measure script sees the same layout as a source build. Both the
- # new (embedding-sdk/legacy + chunks) and old (embedding-sdk.js) layouts
- # are pulled; whichever exists is what gets measured.
- mkdir -p temp/current/resources
- unzip -o temp/current-uberjar/metabase.jar \
- "frontend_client/app/embedding-sdk/*" \
- -d temp/current/resources 2>/dev/null || true
- unzip -o temp/current-uberjar/metabase.jar \
- "frontend_client/app/embedding-sdk.js" \
- -d temp/current/resources 2>/dev/null || true
- - name: Checkout base ref
- uses: actions/checkout@v6
+ - name: Download base-ref uberjar
+ id: base-jar
+ continue-on-error: true
+ uses: ./.github/actions/fetch-artifact
with:
- ref: ${{ inputs.base-ref-commit }}
- path: temp/base-ref-src
- - name: Build base-ref embedding-sdk.js
- shell: bash
- run: |
- cd temp/base-ref-src
- bun install
- MB_EDITION=${{ inputs.edition }} bun run build-release
- - name: Measure current and base bundle sizes
- shell: bash
- env:
- MEASURE_BUNDLES: embedding-sdk-legacy,embedding-sdk-chunked
- run: |
- MEASURE_ROOT=temp/current node ./.github/scripts/measure-bundle-sizes.js > temp/current-sizes.json
- MEASURE_ROOT=temp/base-ref-src node ./.github/scripts/measure-bundle-sizes.js > temp/base-sizes.json
- - name: Compare sizes and set output
+ commit: ${{ inputs.base-ref-commit }}
+ edition: ${{ inputs.edition }}
+ extract-path: temp/base-uberjar
+ - name: Measure and compare bundle sizes
id: compare
shell: bash
env:
+ GH_TOKEN: ${{ github.token }}
+ EDITION: ${{ inputs.edition }}
+ HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
+ BASE_SHA: ${{ inputs.base-ref-commit }}
THRESHOLD: ${{ inputs.threshold-percent }}
- run: node ./.github/actions/sdk-bundle-size-check/compare.js temp/current-sizes.json temp/base-sizes.json
+ MEASURE_BUNDLES: app,embedding-sdk-legacy,embedding-sdk-chunked
+ run: |
+ set -uo pipefail
+
+ skip() {
+ echo "::notice::Embedding SDK bundle-size check skipped: $1"
+ echo "status=stable" >> "$GITHUB_OUTPUT"
+ echo "size_change_percent=0" >> "$GITHUB_OUTPUT"
+ exit 0
+ }
+
+ # The base ref needs an already-built uberjar. Rebuilding it from source
+ # would compare across two build pipelines (the bug this check used to
+ # have), so skip instead when it — or its stats — isn't available.
+ [ -f temp/base-uberjar/metabase.jar ] || skip "base-ref uberjar not found for ${BASE_SHA}"
+
+ extract_js() { # $1=jar $2=dest-root
+ mkdir -p "$2/resources"
+ unzip -o "$1" \
+ "frontend_client/app/dist/*" \
+ "frontend_client/app/embed.js" \
+ "frontend_client/app/embedding-sdk/*" \
+ "frontend_client/app/embedding-sdk.js" \
+ -d "$2/resources" >/dev/null || true
+ }
+
+ fetch_stats() { # $1=sha $2=dest-dir
+ local name="metabase-bundle-stats-${EDITION}-$1"
+ local id
+ id=$(gh api "/repos/${GITHUB_REPOSITORY}/actions/artifacts?name=${name}" --jq '.artifacts[0].id // empty' 2>/dev/null)
+ [ -n "${id}" ] || return 1
+ mkdir -p "$2"
+ gh api "/repos/${GITHUB_REPOSITORY}/actions/artifacts/${id}/zip" > "$2.zip" 2>/dev/null || return 1
+ unzip -o "$2.zip" -d "$2" >/dev/null || return 1
+ }
+
+ extract_js temp/current-uberjar/metabase.jar temp/current
+ extract_js temp/base-uberjar/metabase.jar temp/base
+
+ fetch_stats "${HEAD_SHA}" temp/current/artifacts || skip "bundle stats not found for current ${HEAD_SHA}"
+ fetch_stats "${BASE_SHA}" temp/base/artifacts || skip "bundle stats not found for base ${BASE_SHA}"
+
+ MEASURE_ROOT=temp/current node ./.github/scripts/measure-bundle-sizes.js > temp/current-sizes.json \
+ || { echo "::error::Failed to measure current bundle sizes"; exit 1; }
+ MEASURE_ROOT=temp/base node ./.github/scripts/measure-bundle-sizes.js > temp/base-sizes.json \
+ || { echo "::error::Failed to measure base bundle sizes"; exit 1; }
+
+ node ./.github/actions/sdk-bundle-size-check/compare.js temp/current-sizes.json temp/base-sizes.json
diff --git a/.github/actions/sdk-bundle-size-check/compare.js b/.github/actions/sdk-bundle-size-check/compare.js
index a2c0cd40414..902dd6b6438 100644
--- a/.github/actions/sdk-bundle-size-check/compare.js
+++ b/.github/actions/sdk-bundle-size-check/compare.js
@@ -1,16 +1,72 @@
/* eslint-disable import/no-commonjs */
-// Diffs the embedding SDK bundle sizes produced by measure-bundle-sizes.js for
-// the current build and the base ref. The gate is driven by the legacy
-// (monolithic) bundle's raw bytes; the chunked delivery is reported for context.
+// Diffs the bundle sizes produced by measure-bundle-sizes.js for the current
+// build and the base ref. Every (bundle, kind) is compared strictly against the
+// same (bundle, kind) — never one bundle type against another — using gzipped
+// (served) bytes. The gate is driven by the chunked SDK's reachable total, which
+// is what the SDK actually delivers.
+//
+// compareBundles is pure (data in, data out) so it can be unit-tested; main()
+// is the thin I/O wrapper that reads the size files, prints the report and sets
+// the step outputs / exit code.
const fs = require("fs");
-const [, , currentPath, basePath] = process.argv;
-const threshold = Number(process.env.THRESHOLD ?? 2);
+const pick = (rows, bundle, kind) => rows.find(row => row.bundle === bundle && row.kind === kind);
+const keyOf = row => `${row.bundle}/${row.kind}`;
+const mb = bytes => (bytes / 1024 / 1024).toFixed(2);
+const percentOf = (currentBytes, baseBytes) =>
+ baseBytes ? Math.trunc(((currentBytes - baseBytes) * 100) / baseBytes) : 0;
-const current = JSON.parse(fs.readFileSync(currentPath, "utf8"));
-const base = JSON.parse(fs.readFileSync(basePath, "utf8"));
+/**
+ * Compare two measurement sets and decide the gate. Returns a human-readable
+ * `report` (always), and exactly one of:
+ * - `gate` { status, percent }: the chunked SDK total moved by `percent`.
+ * - `skip` reason: the gate is non-actionable (report stable, no comment).
+ * - `error` reason: the gate inputs are missing (hard failure).
+ */
+function compareBundles({ current, base, threshold }) {
+ const report = ["=== Bundle sizes (gzipped, current vs base) ==="];
+ const keys = [...new Set([...current, ...base].map(keyOf))].sort();
+ for (const key of keys) {
+ const [bundle, kind] = key.split("/");
+ const currentRow = pick(current, bundle, kind);
+ const baseRow = pick(base, bundle, kind);
+ if (!currentRow || !baseRow) {
+ report.push(`${key}: present only in ${currentRow ? "current" : "base"} build`);
+ continue;
+ }
+ const diff = currentRow.gzipBytes - baseRow.gzipBytes;
+ const percent = percentOf(currentRow.gzipBytes, baseRow.gzipBytes);
+ report.push(`${key}: ${mb(currentRow.gzipBytes)}MB vs ${mb(baseRow.gzipBytes)}MB (${percent}%, ${diff} bytes)`);
+ }
-const pick = (rows, bundle, kind) => rows.find(row => row.bundle === bundle && row.kind === kind);
+ // Gate on the chunked SDK's reachable total (gzipped) — what the SDK ships.
+ const gateCurrent = pick(current, "embedding-sdk-chunked", "total");
+ const gateBase = pick(base, "embedding-sdk-chunked", "total");
+ if (!gateCurrent || !gateBase || !gateBase.gzipBytes) {
+ return { report, error: "Could not find embedding-sdk-chunked total (gzip) in both builds" };
+ }
+
+ // Both sides must measure "total" the same way. A base ref built before the
+ // reachableAssets enrichment collapses its total to the initial set, so
+ // comparing it against the current reachable total reports a phantom jump
+ // (~30%). Skip until the base ref also carries reachable stats (i.e. once
+ // this change is on the base).
+ if (Boolean(gateCurrent.reachable) !== Boolean(gateBase.reachable)) {
+ return {
+ report,
+ skip:
+ `chunked "total" measured differently on each side ` +
+ `(current reachable=${Boolean(gateCurrent.reachable)}, base reachable=${Boolean(gateBase.reachable)}); ` +
+ `the base ref predates the reachable-chunk stats. Resolves once this change is on the base ref.`,
+ };
+ }
+
+ const percent = percentOf(gateCurrent.gzipBytes, gateBase.gzipBytes);
+ const status = percent > threshold ? "increased" : percent < -threshold ? "decreased" : "stable";
+ return { report, gate: { status, percent } };
+}
+
+module.exports = { compareBundles };
const setOutput = (name, value) => {
if (process.env.GITHUB_OUTPUT) {
@@ -18,37 +74,37 @@ const setOutput = (name, value) => {
}
};
-const mb = bytes => (bytes / 1024 / 1024).toFixed(2);
-
-const legacyCurrent = pick(current, "embedding-sdk-legacy", "total");
-const legacyBase = pick(base, "embedding-sdk-legacy", "total");
+function main() {
+ const [, , currentPath, basePath] = process.argv;
+ const threshold = Number(process.env.THRESHOLD ?? 2);
-if (!legacyCurrent || !legacyBase || !legacyBase.rawBytes) {
- console.error("::error::Could not find embedding-sdk-legacy size in both builds");
- process.exit(1);
-}
+ const current = JSON.parse(fs.readFileSync(currentPath, "utf8"));
+ const base = JSON.parse(fs.readFileSync(basePath, "utf8"));
-const diff = legacyCurrent.rawBytes - legacyBase.rawBytes;
-const percent = Math.trunc((diff * 100) / legacyBase.rawBytes);
+ const { report, gate, skip, error } = compareBundles({ current, base, threshold });
+ console.log(report.join("\n"));
-const status = percent > threshold ? "increased" : percent < -threshold ? "decreased" : "stable";
+ if (error) {
+ console.error(`::error::${error}`);
+ process.exit(1);
+ }
-console.log("=== Embedding SDK legacy (monolithic) bundle ===");
-console.log(`Current: ${legacyCurrent.rawBytes} bytes raw / ${mb(legacyCurrent.gzipBytes)}MB gzip`);
-console.log(`Base ref: ${legacyBase.rawBytes} bytes raw / ${mb(legacyBase.gzipBytes)}MB gzip`);
-console.log(`Difference: ${diff} bytes (${percent}%), threshold ${threshold}%`);
-console.log(`Status: ${status}`);
+ if (skip) {
+ // Treat the gate as non-actionable: report stable so no regression comment
+ // is posted, mirroring the shell skip() for missing artifacts.
+ console.log(`::notice::Embedding SDK bundle-size gate skipped: ${skip}`);
+ setOutput("status", "stable");
+ setOutput("size_change_percent", "0");
+ process.exit(0);
+ }
-const chunkedCurrent = pick(current, "embedding-sdk-chunked", "total");
-const chunkedBase = pick(base, "embedding-sdk-chunked", "total");
-if (chunkedCurrent && chunkedBase) {
- const chunkedDiff = chunkedCurrent.rawBytes - chunkedBase.rawBytes;
console.log("");
- console.log("=== Embedding SDK chunked (informational) ===");
- console.log(`Current: ${chunkedCurrent.rawBytes} bytes raw / ${mb(chunkedCurrent.gzipBytes)}MB gzip`);
- console.log(`Base ref: ${chunkedBase.rawBytes} bytes raw / ${mb(chunkedBase.gzipBytes)}MB gzip`);
- console.log(`Difference: ${chunkedDiff} bytes`);
+ console.log(`Gate: embedding-sdk-chunked total ${gate.percent}% (threshold ${threshold}%) → ${gate.status}`);
+
+ setOutput("status", gate.status);
+ setOutput("size_change_percent", gate.percent);
}
-setOutput("status", status);
-setOutput("size_change_percent", percent);
+if (require.main === module) {
+ main();
+}
diff --git a/.github/actions/sdk-bundle-size-check/compare.unit.spec.js b/.github/actions/sdk-bundle-size-check/compare.unit.spec.js
new file mode 100644
index 00000000000..fd5499d0a94
--- /dev/null
+++ b/.github/actions/sdk-bundle-size-check/compare.unit.spec.js
@@ -0,0 +1,65 @@
+const { compareBundles } = require("./compare");
+
+const total = (gzipBytes, over = {}) => ({
+ bundle: "embedding-sdk-chunked",
+ kind: "total",
+ gzipBytes,
+ reachable: true,
+ ...over,
+});
+
+describe("compareBundles", () => {
+ it("gates on the chunked SDK total and flags an increase past the threshold", () => {
+ const { gate, report } = compareBundles({
+ current: [total(110)],
+ base: [total(100)],
+ threshold: 2,
+ });
+
+ expect(gate).toEqual({ status: "increased", percent: 10 });
+ expect(report[0]).toContain("current vs base");
+ });
+
+ it("flags a decrease and stays stable within the threshold", () => {
+ expect(compareBundles({ current: [total(90)], base: [total(100)], threshold: 2 }).gate).toEqual({
+ status: "decreased",
+ percent: -10,
+ });
+ expect(compareBundles({ current: [total(101)], base: [total(100)], threshold: 2 }).gate).toEqual({
+ status: "stable",
+ percent: 1,
+ });
+ });
+
+ it("skips when the two sides measured total differently (reachable mismatch)", () => {
+ const result = compareBundles({
+ current: [total(130, { reachable: true })],
+ base: [total(100, { reachable: false })],
+ threshold: 2,
+ });
+
+ expect(result.gate).toBeUndefined();
+ expect(result.skip).toContain("predates the reachable-chunk stats");
+ });
+
+ it("errors when the gate bundle is missing on either side", () => {
+ const result = compareBundles({
+ current: [{ bundle: "app", kind: "total", gzipBytes: 1 }],
+ base: [total(100)],
+ threshold: 2,
+ });
+
+ expect(result.gate).toBeUndefined();
+ expect(result.error).toContain("embedding-sdk-chunked total");
+ });
+
+ it("reports a bundle/kind present on only one side without erroring", () => {
+ const { report } = compareBundles({
+ current: [total(100), { bundle: "app", kind: "total", gzipBytes: 50 }],
+ base: [total(100)],
+ threshold: 2,
+ });
+
+ expect(report.some(line => line.includes("present only in current build"))).toBe(true);
+ });
+});
diff --git a/.github/scripts/bundle-size-stats-prepare.js b/.github/scripts/bundle-size-stats-prepare.js
new file mode 100644
index 00000000000..a57adbb5823
--- /dev/null
+++ b/.github/scripts/bundle-size-stats-prepare.js
@@ -0,0 +1,147 @@
+/* eslint-disable import/no-commonjs */
+// Prepares the rows the bundle-size stats logger uploads to stats.metabase.com.
+//
+// For each measured (bundle, kind) it computes the delta — raw, gzipped and
+// brotli (as-served) bytes plus percent — against the previously *plotted* data
+// point (restored from a rolling cache), so the deltas can be charted directly.
+// It also decides whether this commit is worth recording at all: a point is kept
+// when the as-served size moved by at least MIN_DELTA_PERCENT, or when a new
+// bundle/kind appears. Only master builds run through here — releases form
+// non-linear lines whose deltas are meaningless, so they're not recorded (their
+// historical sizes are backfilled separately).
+//
+// buildStatsRows is pure (data in, data out) so it can be unit-tested; main()
+// is the thin I/O wrapper that reads env/files and writes the rows, cache and
+// step outputs.
+const fs = require("fs");
+const path = require("path");
+
+const deltaPercent = (current, base) =>
+ base ? Math.round(((current - base) * 10000) / base) / 100 : "";
+
+/**
+ * Turn the current measurements + the last plotted point into upload rows, the
+ * slim cache rows for the next diff, and the keep/skip decision.
+ *
+ * The significance threshold tracks the as-served size: brotli where the build
+ * ships a precompressed .br, otherwise gzip. Current measurements always carry
+ * brotliBytes (estimated when no .br shipped), but a cached base point recorded
+ * before brotli logging may not, so the delta still falls back to gzip per row.
+ */
+function buildStatsRows({ measurements, previous, threshold, date, commit, version }) {
+ const previousOf = (bundle, kind) => previous.find(row => row.bundle === bundle && row.kind === kind);
+
+ let maxServedDeltaPercent = 0;
+ let hasNewSeries = false;
+
+ const rows = measurements.map(measurement => {
+ const base = previousOf(measurement.bundle, measurement.kind);
+ const hasBrotli = measurement.brotliBytes != null && base?.brotliBytes != null;
+ const gzipDeltaPercent = deltaPercent(measurement.gzipBytes, base?.gzipBytes);
+ const brotliDeltaPercent = hasBrotli ? deltaPercent(measurement.brotliBytes, base.brotliBytes) : "";
+ if (!base) {
+ hasNewSeries = true;
+ } else {
+ const servedDeltaPercent = hasBrotli ? brotliDeltaPercent : gzipDeltaPercent;
+ maxServedDeltaPercent = Math.max(maxServedDeltaPercent, Math.abs(servedDeltaPercent));
+ }
+ return {
+ "Date": date,
+ "Version": version,
+ "Commit": commit,
+ "Bundle": measurement.bundle,
+ "Kind": measurement.kind, // "initial" or "total"
+ "Raw bytes": measurement.rawBytes,
+ "Gzip bytes": measurement.gzipBytes,
+ "Brotli bytes": measurement.brotliBytes, // as-served (shipped .br, else estimated)
+ "File count": measurement.fileCount,
+ "Raw bytes delta": base ? measurement.rawBytes - base.rawBytes : "",
+ "Gzip bytes delta": base ? measurement.gzipBytes - base.gzipBytes : "",
+ "Brotli bytes delta": hasBrotli ? measurement.brotliBytes - base.brotliBytes : "",
+ "Raw delta %": deltaPercent(measurement.rawBytes, base?.rawBytes),
+ "Gzip delta %": gzipDeltaPercent,
+ "Brotli delta %": brotliDeltaPercent,
+ };
+ });
+
+ const firstPoint = previous.length === 0;
+ const significant = firstPoint || hasNewSeries || maxServedDeltaPercent >= threshold;
+
+ // Slim rows the next run diffs against. The workflow only persists this to the
+ // cache when we actually push, so the cached reference always stays the last
+ // *plotted* point (cumulative drift is caught).
+ const cacheRows = measurements.map(({ bundle, kind, rawBytes, gzipBytes, brotliBytes }) => ({
+ bundle,
+ kind,
+ rawBytes,
+ gzipBytes,
+ brotliBytes,
+ }));
+
+ const reason = firstPoint
+ ? "first point"
+ : hasNewSeries
+ ? "new bundle/kind series"
+ : `max served Δ ${maxServedDeltaPercent.toFixed(2)}% (threshold ${threshold}%)`;
+
+ return { rows, cacheRows, significant, reason, maxServedDeltaPercent, firstPoint, hasNewSeries };
+}
+
+module.exports = { buildStatsRows };
+
+const readJson = filePath => (filePath && fs.existsSync(filePath) ? JSON.parse(fs.readFileSync(filePath, "utf8")) : null);
+const writeJson = (filePath, value) => {
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
+ fs.writeFileSync(filePath, JSON.stringify(value));
+};
+const setOutput = (env, name, value) => {
+ if (env.GITHUB_OUTPUT) {
+ fs.appendFileSync(env.GITHUB_OUTPUT, `${name}=${value}\n`);
+ }
+};
+
+// version.properties (extracted from the uberjar) carries the build's tag.
+// Non-release (master) builds report tag=vUNKNOWN, so leave Version empty there.
+function readVersion(versionPropsPath) {
+ const raw =
+ versionPropsPath && fs.existsSync(versionPropsPath)
+ ? (fs.readFileSync(versionPropsPath, "utf8").match(/^tag=(.*)$/m)?.[1]?.trim() ?? "")
+ : "";
+ return raw === "vUNKNOWN" ? "" : raw;
+}
+
+function main() {
+ const env = process.env;
+
+ const measurements = readJson(env.CURRENT);
+ if (!Array.isArray(measurements) || measurements.length === 0) {
+ console.error(`::error::No bundle measurements found at ${env.CURRENT}`);
+ process.exit(1);
+ }
+
+ const result = buildStatsRows({
+ measurements,
+ // The last plotted point, as slim rows: [{ bundle, kind, rawBytes, gzipBytes, brotliBytes }].
+ previous: readJson(env.LAST) || [],
+ threshold: Number(env.MIN_DELTA_PERCENT ?? 1),
+ date: new Date().toISOString().slice(0, 10), // YYYY-MM-DD
+ commit: (env.HEAD_SHA || "").slice(0, 12),
+ version: readVersion(env.VERSION_PROPS),
+ });
+
+ writeJson(env.ROWS_OUT, result.rows);
+ writeJson(env.CACHE_OUT, result.cacheRows);
+
+ console.log(`${result.significant ? "RECORD" : "SKIP"} — ${result.reason}`);
+
+ setOutput(env, "significant", result.significant ? "true" : "false");
+ setOutput(
+ env,
+ "max_delta_percent",
+ result.firstPoint || result.hasNewSeries ? "" : result.maxServedDeltaPercent.toFixed(2),
+ );
+}
+
+if (require.main === module) {
+ main();
+}
diff --git a/.github/scripts/bundle-size-stats-prepare.unit.spec.js b/.github/scripts/bundle-size-stats-prepare.unit.spec.js
new file mode 100644
index 00000000000..2d8a0c44f0c
--- /dev/null
+++ b/.github/scripts/bundle-size-stats-prepare.unit.spec.js
@@ -0,0 +1,86 @@
+const { buildStatsRows } = require("./bundle-size-stats-prepare");
+
+const meta = { date: "2026-06-30", commit: "abc123", version: "" };
+const measurement = (over = {}) => ({
+ bundle: "embedding-sdk-chunked",
+ kind: "total",
+ rawBytes: 1000,
+ gzipBytes: 400,
+ brotliBytes: 300,
+ fileCount: 3,
+ ...over,
+});
+
+describe("buildStatsRows", () => {
+ it("records the first point and leaves every delta empty", () => {
+ const result = buildStatsRows({
+ ...meta,
+ measurements: [measurement()],
+ previous: [],
+ threshold: 1,
+ });
+
+ expect(result.firstPoint).toBe(true);
+ expect(result.significant).toBe(true);
+ expect(result.reason).toBe("first point");
+ expect(result.rows[0]["Gzip bytes delta"]).toBe("");
+ expect(result.rows[0]["Brotli delta %"]).toBe("");
+ expect(result.cacheRows[0]).toEqual({
+ bundle: "embedding-sdk-chunked",
+ kind: "total",
+ rawBytes: 1000,
+ gzipBytes: 400,
+ brotliBytes: 300,
+ });
+ });
+
+ it("records a never-seen bundle/kind as a new series", () => {
+ const result = buildStatsRows({
+ ...meta,
+ measurements: [measurement({ bundle: "app", kind: "initial" })],
+ previous: [{ bundle: "app", kind: "total", rawBytes: 1, gzipBytes: 1, brotliBytes: 1 }],
+ threshold: 99,
+ });
+
+ expect(result.hasNewSeries).toBe(true);
+ expect(result.significant).toBe(true);
+ expect(result.reason).toBe("new bundle/kind series");
+ });
+
+ it("gates significance on the brotli (as-served) delta when both sides have it", () => {
+ const previous = [{ bundle: "embedding-sdk-chunked", kind: "total", rawBytes: 1000, gzipBytes: 400, brotliBytes: 300 }];
+
+ const big = buildStatsRows({
+ ...meta,
+ measurements: [measurement({ brotliBytes: 315, gzipBytes: 400 })], // brotli +5%, gzip 0%
+ previous,
+ threshold: 1,
+ });
+ expect(big.maxServedDeltaPercent).toBeCloseTo(5);
+ expect(big.significant).toBe(true);
+ expect(big.rows[0]["Brotli delta %"]).toBeCloseTo(5);
+
+ const small = buildStatsRows({
+ ...meta,
+ measurements: [measurement({ brotliBytes: 301, gzipBytes: 999 })], // brotli +0.33%, gzip ignored
+ previous,
+ threshold: 1,
+ });
+ expect(small.significant).toBe(false);
+ expect(small.reason).toContain("threshold 1%");
+ });
+
+ it("falls back to the gzip delta when the cached base predates brotli logging", () => {
+ const result = buildStatsRows({
+ ...meta,
+ measurements: [measurement({ gzipBytes: 440, brotliBytes: 300 })], // gzip +10%
+ previous: [{ bundle: "embedding-sdk-chunked", kind: "total", rawBytes: 1000, gzipBytes: 400 }],
+ threshold: 1,
+ });
+
+ expect(result.maxServedDeltaPercent).toBeCloseTo(10);
+ expect(result.significant).toBe(true);
+ expect(result.rows[0]["Brotli bytes delta"]).toBe(""); // no base brotli to diff against
+ expect(result.rows[0]["Gzip delta %"]).toBeCloseTo(10);
+ });
+});
diff --git a/.github/scripts/csv-to-mb.js b/.github/scripts/csv-to-mb.js
index 875f88d4f51..5f5de281371 100644
--- a/.github/scripts/csv-to-mb.js
+++ b/.github/scripts/csv-to-mb.js
@@ -19,23 +19,68 @@ function jsonToCsvFormData(jsonData) {
return formData;
}
-async function uploadCsvToMb({ baseUrl, tableId, jsonData, mode = 'append' }) {
- const formData = jsonToCsvFormData(jsonData);
+// Transient failures worth retrying: throttling and server-side errors. A 4xx
+// (bad request, auth) won't succeed on retry, so those fail fast.
+const RETRYABLE_STATUS = new Set([429, 500, 502, 503, 504]);
+
+class UploadError extends Error {
+ constructor(message, { retryable }) {
+ super(message);
+ this.retryable = retryable;
+ }
+}
+
+async function attemptUpload(url, jsonData) {
+ let response;
+ try {
+ response = await fetch(url, {
+ method: 'POST',
+ headers: {
+ "x-api-key": process.env.API_KEY
+ },
+ // Rebuilt per attempt: a consumed request body can't be reused.
+ body: jsonToCsvFormData(jsonData)
+ });
+ } catch (networkError) {
+ throw new UploadError(networkError.message, { retryable: true });
+ }
+
+ if (response.ok) {
+ return;
+ }
+
+ const body = await response.text().catch(() => '');
+ throw new UploadError(
+ `Upload failed: ${response.status} ${response.statusText} ${body}`.trim(),
+ { retryable: RETRYABLE_STATUS.has(response.status) }
+ );
+}
+
+const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
+
+/**
+ * Uploads jsonData as CSV, retrying transient API errors with exponential
+ * backoff. `retries` is the number of extra attempts after the first (so the
+ * default 2 means up to 3 tries). Throws the last error once attempts are
+ * exhausted or the error is non-retryable.
+ */
+async function uploadCsvToMb({ baseUrl, tableId, jsonData, mode = 'append', retries = 2, retryDelayMs = 1000 }) {
const operation = mode === 'replace' ? 'replace-csv' : 'append-csv';
+ const url = `${baseUrl}/api/table/${tableId}/${operation}`;
- const response = await fetch(`${baseUrl}/api/table/${tableId}/${operation}`, {
- method: 'POST',
- headers: {
- "x-api-key": process.env.API_KEY
- },
- body: formData
- });
-
- if (!response.ok) {
- const body = await response.text().catch(() => '');
- throw new Error(`Upload failed: ${response.status} ${response.statusText} ${body}`.trim());
+ for (let attempt = 0; ; attempt++) {
+ try {
+ await attemptUpload(url, jsonData);
+ return { success: true };
+ } catch (error) {
+ if (!error.retryable || attempt >= retries) {
+ throw error;
+ }
+ const delay = retryDelayMs * 2 ** attempt;
+ console.warn(`Upload attempt ${attempt + 1} failed (${error.message}); retrying in ${delay}ms`);
+ await sleep(delay);
+ }
}
- return { success: true };
}
module.exports = { uploadCsvToMb };
diff --git a/.github/scripts/csv-to-mb.unit.spec.js b/.github/scripts/csv-to-mb.unit.spec.js
new file mode 100644
index 00000000000..28ebf81913a
--- /dev/null
+++ b/.github/scripts/csv-to-mb.unit.spec.js
@@ -0,0 +1,50 @@
+const { uploadCsvToMb } = require("./csv-to-mb");
+
+const ok = () => ({ ok: true });
+const fail = status => ({ ok: false, status, statusText: "err", text: async () => "" });
+
+const upload = (over = {}) =>
+ uploadCsvToMb({
+ baseUrl: "https://stats.example.com",
+ tableId: 1,
+ jsonData: [{ a: 1 }],
+ retryDelayMs: 1,
+ ...over,
+ });
+
+describe("uploadCsvToMb retries", () => {
+ afterEach(() => {
+ delete global.fetch;
+ });
+
+ it("retries transient server errors then succeeds", async () => {
+ global.fetch = jest.fn().mockResolvedValueOnce(fail(503)).mockResolvedValueOnce(fail(500)).mockResolvedValueOnce(ok());
+
+ await expect(upload({ retries: 2 })).resolves.toEqual({ success: true });
+ expect(global.fetch).toHaveBeenCalledTimes(3);
+ });
+
+ it("retries network errors", async () => {
+ global.fetch = jest
+ .fn()
+ .mockRejectedValueOnce(new Error("ECONNRESET"))
+ .mockResolvedValueOnce(ok());
+
+ await expect(upload({ retries: 2 })).resolves.toEqual({ success: true });
+ expect(global.fetch).toHaveBeenCalledTimes(2);
+ });
+
+ it("gives up after exhausting retries and throws the last error", async () => {
+ global.fetch = jest.fn().mockResolvedValue(fail(500));
+
+ await expect(upload({ retries: 2 })).rejects.toThrow("Upload failed: 500");
+ expect(global.fetch).toHaveBeenCalledTimes(3); // 1 initial + 2 retries
+ });
+
+ it("does not retry a non-transient 4xx", async () => {
+ global.fetch = jest.fn().mockResolvedValue(fail(400));
+
+ await expect(upload({ retries: 2 })).rejects.toThrow("Upload failed: 400");
+ expect(global.fetch).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/.github/scripts/measure-bundle-sizes.js b/.github/scripts/measure-bundle-sizes.js
index 52464facc15..c45122d085f 100644
--- a/.github/scripts/measure-bundle-sizes.js
+++ b/.github/scripts/measure-bundle-sizes.js
@@ -1,68 +1,58 @@
/* eslint-disable import/no-commonjs */
+// Measures the front-end bundles emitted into an extracted uberjar tree.
+//
+// Every bundle reports one or both of:
+// - "initial": JS the browser downloads on first paint (the entry chunk plus
+// its initial/sync split chunks). Never excludes initial chunks.
+// - "total": all *reachable* JS (initial plus async chunks that are actually
+// loadable at runtime). Never includes unreachable/never-fetched chunks.
+// Reachability needs the per-entry asset list from the rspack stats file, so the
+// app and chunked SDK bundles require their stats file to be present.
+//
+// The pure pieces — the asset-name selection (initial vs reachable, runtime
+// filtering, the async-loadable flag) — are split out as select*Assets so they
+// can be unit-tested against a stats object. The measure* functions and main()
+// keep all the filesystem and env I/O, threading an explicit `config`.
const fs = require("fs");
const path = require("path");
const zlib = require("zlib");
-// MEASURE_ROOT lets callers point at an extracted tree (e.g. the PR bundle-size
-// check measures a current and a base-ref tree); defaults to cwd for the stats
-// workflow. MEASURE_BUNDLES restricts which bundles are measured (comma-separated).
-const baseDir = process.env.MEASURE_ROOT ? path.resolve(process.env.MEASURE_ROOT) : process.cwd();
-const resolve = (...relativeParts) => path.resolve(baseDir, ...relativeParts);
-const bundleFilter = process.env.MEASURE_BUNDLES
- ? new Set(process.env.MEASURE_BUNDLES.split(",").map(bundle => bundle.trim()))
- : null;
-const isBundleWanted = bundle => !bundleFilter || bundleFilter.has(bundle);
-
+const APP_DIST = "resources/frontend_client/app/dist";
const SDK_ROOT = "resources/frontend_client/app/embedding-sdk";
-/**
- * Async SDK chunks are reachable now that the runtime publicPath is set, so the
- * bootstrap can fetch import() chunks on demand. "total" therefore includes the
- * lazy chunks while "initial" stays the eager first-load set. There's no reliable
- * way to detect loadability from the build artifacts, so this stays a manual flag:
- * it was flipped to true alongside the publicPath fix.
- */
-const SDK_ASYNC_CHUNKS_LOADABLE = true;
-
// The chunk runtime is inlined into the bootstrap, never sent as a standalone file.
const SDK_RUNTIME_RE = /embedding-sdk-chunk-runtime\./;
+const notRuntime = name => !SDK_RUNTIME_RE.test(name);
-/**
- * Simple targets measured straight from emitted files.
- * - `dir`/`file`/`altFile`: what to measure (altFile is a fallback single file).
- * - `stats`: entrypoint whose initial assets are the "initial" download.
- * - `optional`: skip (don't fail) when the output is absent.
- * The hosted SDK is split into embedding-sdk-legacy (the self-contained
- * monolith) and embedding-sdk-chunked (handled separately below).
- */
-const simpleTargets = [
- {
- bundle: "app",
- dir: "resources/frontend_client/app/dist",
- stats: { file: "artifacts/stats-main.json", entrypoint: "app-main" },
- },
- {
- bundle: "embedding-sdk-legacy",
- dir: `${SDK_ROOT}/legacy`,
- altFile: `${SDK_ROOT}.js`,
- optional: true,
- },
- {
- bundle: "embedding-sdk-package",
- dir: "resources/embedding-sdk/dist",
- optional: true,
- },
- {
- bundle: "embed-js",
- file: "resources/frontend_client/app/embed.js",
- },
-];
+const assetFiles = (root, names) => names.map(name => path.join(root, name));
+const unique = files => [...new Set(files)];
+
+// MEASURE_ROOT lets callers point at an extracted tree (e.g. the PR bundle-size
+// check measures a current and a base-ref tree); defaults to cwd for the stats
+// workflow. MEASURE_BUNDLES restricts which bundles are measured (comma-separated).
+// SDK_ASYNC_CHUNKS_LOADABLE (see selectChunkedSdkAssets) toggles whether async
+// SDK chunks count toward "total".
+function buildConfig(env) {
+ const baseDir = env.MEASURE_ROOT ? path.resolve(env.MEASURE_ROOT) : process.cwd();
+ const bundleFilter = env.MEASURE_BUNDLES
+ ? new Set(env.MEASURE_BUNDLES.split(",").map(bundle => bundle.trim()))
+ : null;
+ return {
+ resolve: (...relativeParts) => path.resolve(baseDir, ...relativeParts),
+ isBundleWanted: bundle => !bundleFilter || bundleFilter.has(bundle),
+ // Async SDK chunks are reachable once output.publicPath is set so import()
+ // chunks are actually fetched at runtime — which landed in master. Defaults
+ // to true to match master; set SDK_ASYNC_CHUNKS_LOADABLE=false to measure an
+ // older commit built before the publicPath fix (async unreachable, so
+ // "total" collapses to "initial").
+ sdkAsyncChunksLoadable: env.SDK_ASYNC_CHUNKS_LOADABLE !== "false",
+ };
+}
function sizeOf(files) {
let rawBytes = 0;
let gzipBytes = 0;
let brotliBytes = 0;
- let anyBrotli = false;
for (const file of files) {
const contents = fs.readFileSync(file);
rawBytes += contents.length;
@@ -70,20 +60,17 @@ function sizeOf(files) {
gzipBytes += zlib.gzipSync(contents).length;
// brotli: as-served — count the precompressed .br the build ships; files
// without one (static-viz, sub-threshold chunks) are served compressed on
- // the fly, so estimate with brotli. Null when no .br shipped at all.
+ // the fly, so estimate with brotli. Always reported (like gzip).
const brotliPath = `${file}.br`;
- if (fs.existsSync(brotliPath)) {
- anyBrotli = true;
- brotliBytes += fs.statSync(brotliPath).size;
- } else {
- brotliBytes += zlib.brotliCompressSync(contents).length;
- }
+ brotliBytes += fs.existsSync(brotliPath)
+ ? fs.statSync(brotliPath).size
+ : zlib.brotliCompressSync(contents).length;
}
return {
fileCount: files.length,
rawBytes,
gzipBytes,
- brotliBytes: anyBrotli ? brotliBytes : null,
+ brotliBytes,
};
}
@@ -102,104 +89,170 @@ function collectJsFiles(absolutePath) {
});
}
-function readStats(statsFile) {
- const statsPath = resolve(statsFile);
+function readStats(config, statsFile) {
+ const statsPath = config.resolve(statsFile);
return fs.existsSync(statsPath) ? JSON.parse(fs.readFileSync(statsPath, "utf8")) : null;
}
-function entrypointJsAssets(stats, name) {
+// An entrypoint's .js assets for a given field: "assets" = its initial
+// (synchronous) load; "reachableAssets" = initial plus everything it can lazily
+// import (the chunk graph walked at build time in bundle-stats.js).
+function entrypointJsAssets(stats, name, field = "assets") {
const entry = stats && stats.entrypoints && stats.entrypoints[name];
- if (!entry) {
+ if (!entry || !entry[field]) {
return null;
}
- return entry.assets.map(asset => asset.name || asset).filter(name => name.endsWith(".js"));
+ return entry[field].map(asset => asset.name || asset).filter(assetName => assetName.endsWith(".js"));
}
-function initialJsFiles({ file: statsFile, entrypoint, root }, joinRoot) {
- const stats = readStats(statsFile);
- if (!stats) {
- console.warn(`Stats file missing (${statsFile}); reporting initial == total.`);
- return null;
+/**
+ * Main app = the app-main entrypoint only (app/dist also holds other pages like
+ * app-public, app-embed*, which are not the app). `reachableAssets` is null on
+ * older stats built before the chunk-graph enrichment — the caller then falls
+ * back to sizing the whole app/dist.
+ */
+function selectAppAssets(stats) {
+ const initialAssets = entrypointJsAssets(stats, "app-main", "assets");
+ if (!initialAssets) {
+ throw new Error('app: stats-main.json / "app-main" entrypoint missing');
}
- const assets = entrypointJsAssets(stats, entrypoint);
- if (!assets) {
- console.warn(`Entrypoint "${entrypoint}" missing in ${statsFile}; reporting initial == total.`);
+ return { initialAssets, reachableAssets: entrypointJsAssets(stats, "app-main", "reachableAssets") };
+}
+
+/**
+ * Legacy SDK: the "embedding-sdk" entry, a single eager file in legacy/. It still
+ * emits on-demand chunks (jspdf, echarts, leaflet, ...) into the shared chunks/
+ * dir via dynamic import(). initial = the eager legacy file; total = initial plus
+ * its reachable async chunks, but only when those are loadable at runtime
+ * (sdkAsyncChunksLoadable), otherwise total collapses to initial. Returns null
+ * when the stats lack the entry (older layout) so the caller can fall back.
+ */
+function selectLegacySdkAssets(stats, { sdkAsyncChunksLoadable }) {
+ const initialNames = entrypointJsAssets(stats, "embedding-sdk", "assets");
+ if (!initialNames) {
return null;
}
- const base = root ? resolve(root) : joinRoot;
- return assets.map(name => path.join(base, name));
+ const reachableNames = entrypointJsAssets(stats, "embedding-sdk", "reachableAssets") || initialNames;
+ const initial = unique(initialNames).filter(notRuntime);
+ return {
+ initialNames: initial,
+ reachableNames: sdkAsyncChunksLoadable ? unique(reachableNames).filter(notRuntime) : initial,
+ };
}
-function measureSimpleTarget({ bundle, dir, file, altFile, stats, optional }) {
- const candidates = [dir, file, altFile].filter(Boolean);
- const chosen = candidates.find(c => fs.existsSync(resolve(c)));
- if (!chosen) {
- if (optional) {
- console.warn(`Skipping "${bundle}": output not found (${candidates.join(", ")})`);
- return [];
- }
- throw new Error(`Bundle output not found for "${bundle}": ${candidates.join(", ")}`);
+/**
+ * Chunked SDK delivery. initial = bootstrap (minus its inlined runtime) + the
+ * chunked entry's initial chunks. total = the chunked entry's reachable set
+ * (initial + async children, from the chunk graph). includesAsyncChunks is true
+ * only when the stats carry the reachable chunk graph AND those chunks are
+ * loadable at runtime (sdkAsyncChunksLoadable). A base ref built before the
+ * reachableAssets enrichment has no graph, so its "total" must collapse to
+ * "initial" — the flag lets the comparison avoid measuring a reachable total
+ * against a collapsed one (an apples-to-oranges ~30% phantom).
+ */
+function selectChunkedSdkAssets(stats, { sdkAsyncChunksLoadable }) {
+ const chunkedInitial = entrypointJsAssets(stats, "embedding-sdk-chunked", "assets");
+ if (!chunkedInitial) {
+ throw new Error('embedding-sdk-chunked: stats-embedding-sdk.json / "embedding-sdk-chunked" entrypoint missing');
}
+ const bootstrapInitial = entrypointJsAssets(stats, "embedding-sdk-bootstrap", "assets") || [];
+ const chunkedReachableAssets = entrypointJsAssets(stats, "embedding-sdk-chunked", "reachableAssets");
+ const chunkedReachable = chunkedReachableAssets || chunkedInitial;
+ const bootstrapReachable = entrypointJsAssets(stats, "embedding-sdk-bootstrap", "reachableAssets") || bootstrapInitial;
+ return {
+ initialNames: unique([...bootstrapInitial, ...chunkedInitial]).filter(notRuntime),
+ reachableNames: unique([...bootstrapReachable, ...chunkedReachable]).filter(notRuntime),
+ includesAsyncChunks: chunkedReachableAssets != null && sdkAsyncChunksLoadable,
+ };
+}
- const root = resolve(chosen);
- const total = sizeOf(collectJsFiles(root));
- const initialFiles = stats ? initialJsFiles(stats, root) : null;
- const initial = initialFiles ? sizeOf(initialFiles) : total;
+function measureApp(config) {
+ const dist = config.resolve(APP_DIST);
+ if (!fs.existsSync(dist)) {
+ throw new Error(`Bundle output not found for "app": ${dist}`);
+ }
+ const { initialAssets, reachableAssets } = selectAppAssets(readStats(config, "artifacts/stats-main.json"));
+ if (!reachableAssets) {
+ console.warn("app: stats has no reachableAssets; app total falls back to the whole app/dist");
+ }
+ const totalFiles = reachableAssets ? assetFiles(dist, reachableAssets) : collectJsFiles(dist);
+ return [
+ { bundle: "app", kind: "initial", ...sizeOf(assetFiles(dist, initialAssets)) },
+ { bundle: "app", kind: "total", ...sizeOf(totalFiles) },
+ ];
+}
+function measureLegacySdk(config) {
+ const absRoot = config.resolve(SDK_ROOT);
+ const selected = selectLegacySdkAssets(readStats(config, "artifacts/stats-embedding-sdk.json"), config);
+ if (!selected) {
+ // No stats / older layout: fall back to whatever sits in the legacy dir.
+ const candidates = [`${SDK_ROOT}/legacy`, `${SDK_ROOT}.js`];
+ const chosen = candidates.find(candidate => fs.existsSync(config.resolve(candidate)));
+ if (!chosen) {
+ console.warn(`Skipping "embedding-sdk-legacy": output not found (${candidates.join(", ")})`);
+ return [];
+ }
+ return [{ bundle: "embedding-sdk-legacy", kind: "total", ...sizeOf(collectJsFiles(config.resolve(chosen))) }];
+ }
return [
- { bundle, kind: "initial", ...initial },
- { bundle, kind: "total", ...total },
+ { bundle: "embedding-sdk-legacy", kind: "initial", ...sizeOf(assetFiles(absRoot, selected.initialNames)) },
+ { bundle: "embedding-sdk-legacy", kind: "total", ...sizeOf(assetFiles(absRoot, selected.reachableNames)) },
];
}
-/**
- * Chunked SDK delivery. "Reachable" = what the bootstrap actually sends: the
- * bootstrap (with its inlined runtime) + the chunked entry's initial chunks,
- * minus the standalone runtime file. Async chunks are excluded (initial == total)
- * unless SDK_ASYNC_CHUNKS_LOADABLE is set, at which point "total" includes them.
- */
-function measureChunkedSdk(statsFile) {
- const chunksDir = resolve(SDK_ROOT, "chunks");
+function measureChunkedSdk(config) {
+ const chunksDir = config.resolve(SDK_ROOT, "chunks");
if (!fs.existsSync(chunksDir)) {
// pre-chunked layout (monolith only) — nothing to measure here
return [];
}
-
- const absRoot = resolve(SDK_ROOT);
- const everything = collectJsFiles(chunksDir).filter(f => !SDK_RUNTIME_RE.test(f));
-
- const stats = readStats(statsFile);
- const chunked = entrypointJsAssets(stats, "embedding-sdk-chunked");
- let reachableFiles;
- if (chunked) {
- const bootstrap = entrypointJsAssets(stats, "embedding-sdk-bootstrap") || [];
- const names = [...new Set([...bootstrap, ...chunked])].filter(n => !SDK_RUNTIME_RE.test(n));
- reachableFiles = names.map(n => path.join(absRoot, n));
- } else {
- console.warn(`embedding-sdk-chunked: entrypoint missing in ${statsFile}; counting whole chunks/ dir`);
- reachableFiles = everything;
- }
-
- const initial = sizeOf(reachableFiles);
- const total = SDK_ASYNC_CHUNKS_LOADABLE ? sizeOf(everything) : sizeOf(reachableFiles);
-
+ const absRoot = config.resolve(SDK_ROOT);
+ const { initialNames, reachableNames, includesAsyncChunks } = selectChunkedSdkAssets(
+ readStats(config, "artifacts/stats-embedding-sdk.json"),
+ config,
+ );
+ const initial = sizeOf(assetFiles(absRoot, initialNames));
+ const total = includesAsyncChunks ? sizeOf(assetFiles(absRoot, reachableNames)) : initial;
return [
{ bundle: "embedding-sdk-chunked", kind: "initial", ...initial },
- { bundle: "embedding-sdk-chunked", kind: "total", ...total },
+ { bundle: "embedding-sdk-chunked", kind: "total", reachable: includesAsyncChunks, ...total },
];
}
-function measureBundleSizes() {
- return [
- ...simpleTargets.filter(target => isBundleWanted(target.bundle)).flatMap(measureSimpleTarget),
- ...(isBundleWanted("embedding-sdk-chunked")
- ? measureChunkedSdk("artifacts/stats-embedding-sdk.json")
- : []),
- ];
+// Single-file/dir bundles loaded in one shot — total only.
+function measureWholeDir(config, bundle, candidates) {
+ const chosen = candidates.find(candidate => fs.existsSync(config.resolve(candidate)));
+ if (!chosen) {
+ console.warn(`Skipping "${bundle}": output not found (${candidates.join(", ")})`);
+ return [];
+ }
+ return [{ bundle, kind: "total", ...sizeOf(collectJsFiles(config.resolve(chosen))) }];
+}
+
+const bundles = {
+ app: measureApp,
+ "embedding-sdk-legacy": measureLegacySdk,
+ "embedding-sdk-chunked": measureChunkedSdk,
+ "embedding-sdk-package": config => measureWholeDir(config, "embedding-sdk-package", ["resources/embedding-sdk/dist"]),
+ "embed-js": config => measureWholeDir(config, "embed-js", ["resources/frontend_client/app/embed.js"]),
+};
+
+function measureBundleSizes(config) {
+ return Object.entries(bundles)
+ .filter(([bundle]) => config.isBundleWanted(bundle))
+ .flatMap(([, measure]) => measure(config));
}
-module.exports = { measureBundleSizes };
+module.exports = {
+ measureBundleSizes,
+ buildConfig,
+ entrypointJsAssets,
+ selectAppAssets,
+ selectLegacySdkAssets,
+ selectChunkedSdkAssets,
+};
if (require.main === module) {
- console.log(JSON.stringify(measureBundleSizes(), null, 2));
+ console.log(JSON.stringify(measureBundleSizes(buildConfig(process.env)), null, 2));
}
diff --git a/.github/scripts/measure-bundle-sizes.unit.spec.js b/.github/scripts/measure-bundle-sizes.unit.spec.js
new file mode 100644
index 00000000000..686c377cf82
--- /dev/null
+++ b/.github/scripts/measure-bundle-sizes.unit.spec.js
@@ -0,0 +1,87 @@
+const {
+ entrypointJsAssets,
+ selectAppAssets,
+ selectLegacySdkAssets,
+ selectChunkedSdkAssets,
+} = require("./measure-bundle-sizes");
+
+const RUNTIME = "embedding-sdk-chunk-runtime.deadbeef.js";
+
+const stats = entrypoints => ({ entrypoints });
+
+describe("entrypointJsAssets", () => {
+ it("returns the .js asset names for a field, accepting strings or {name}", () => {
+ const s = stats({ app: { assets: [{ name: "a.js" }, "b.js", { name: "c.css" }] } });
+ expect(entrypointJsAssets(s, "app", "assets")).toEqual(["a.js", "b.js"]);
+ });
+
+ it("returns null when the entrypoint or field is absent", () => {
+ expect(entrypointJsAssets(stats({}), "missing")).toBeNull();
+ expect(entrypointJsAssets(null, "app")).toBeNull();
+ });
+});
+
+describe("selectAppAssets", () => {
+ it("returns the initial set and a null reachable set on pre-enrichment stats", () => {
+ const s = stats({ "app-main": { assets: ["main.js"] } });
+ expect(selectAppAssets(s)).toEqual({ initialAssets: ["main.js"], reachableAssets: null });
+ });
+
+ it("throws when the app-main entrypoint is missing", () => {
+ expect(() => selectAppAssets(stats({}))).toThrow("app-main");
+ });
+});
+
+describe("selectChunkedSdkAssets", () => {
+ const base = {
+ "embedding-sdk-chunked": { assets: ["a.js", RUNTIME], reachableAssets: ["a.js", "b.js", RUNTIME] },
+ "embedding-sdk-bootstrap": { assets: ["boot.js"], reachableAssets: ["boot.js"] },
+ };
+
+ it("merges bootstrap + chunked, drops the inlined runtime, and counts async chunks", () => {
+ const result = selectChunkedSdkAssets(stats(base), { sdkAsyncChunksLoadable: true });
+ expect(result.initialNames).toEqual(["boot.js", "a.js"]);
+ expect(result.reachableNames).toEqual(["boot.js", "a.js", "b.js"]);
+ expect(result.includesAsyncChunks).toBe(true);
+ });
+
+ it("collapses total to initial when async chunks are not loadable", () => {
+ const result = selectChunkedSdkAssets(stats(base), { sdkAsyncChunksLoadable: false });
+ expect(result.includesAsyncChunks).toBe(false);
+ });
+
+ it("collapses total to initial when the stats carry no reachable graph", () => {
+ const noGraph = { "embedding-sdk-chunked": { assets: ["a.js"] } };
+ const result = selectChunkedSdkAssets(stats(noGraph), { sdkAsyncChunksLoadable: true });
+ expect(result.reachableNames).toEqual(["a.js"]);
+ expect(result.includesAsyncChunks).toBe(false);
+ });
+
+ it("throws when the chunked entrypoint is missing", () => {
+ expect(() => selectChunkedSdkAssets(stats({}), { sdkAsyncChunksLoadable: true })).toThrow(
+ "embedding-sdk-chunked",
+ );
+ });
+});
+
+describe("selectLegacySdkAssets", () => {
+ it("returns null when the legacy entry is absent so the caller can fall back", () => {
+ expect(selectLegacySdkAssets(stats({}), { sdkAsyncChunksLoadable: true })).toBeNull();
+ });
+
+ it("expands total to the reachable async set when loadable", () => {
+ const s = stats({ "embedding-sdk": { assets: ["legacy.js"], reachableAssets: ["legacy.js", "chunk.js"] } });
+ expect(selectLegacySdkAssets(s, { sdkAsyncChunksLoadable: true })).toEqual({
+ initialNames: ["legacy.js"],
+ reachableNames: ["legacy.js", "chunk.js"],
+ });
+ });
+
+ it("collapses total to initial when async chunks are not loadable", () => {
+ const s = stats({ "embedding-sdk": { assets: ["legacy.js"], reachableAssets: ["legacy.js", "chunk.js"] } });
+ expect(selectLegacySdkAssets(s, { sdkAsyncChunksLoadable: false })).toEqual({
+ initialNames: ["legacy.js"],
+ reachableNames: ["legacy.js"],
+ });
+ });
+});
diff --git a/.github/workflows/bundle-size-stats.yml b/.github/workflows/bundle-size-stats.yml
index 203533779b4..dfc95dd7180 100644
--- a/.github/workflows/bundle-size-stats.yml
+++ b/.github/workflows/bundle-size-stats.yml
@@ -8,6 +8,8 @@ on:
workflow_run:
workflows: ["Build Uberjar"]
types: [completed]
+ # Master only: releases form non-linear lines whose deltas are meaningless,
+ # so their sizes are backfilled separately rather than logged here.
branches: [master]
concurrency:
@@ -21,6 +23,10 @@ jobs:
timeout-minutes: 15
env:
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
+ # Only record a data point when a bundle's as-served size (brotli where the
+ # build ships .br, else gzip) moved by at least this percent vs the last
+ # plotted point, keeping the chart to real changes.
+ MIN_DELTA_PERCENT: "1"
steps:
- name: Check out the code
uses: actions/checkout@v6
@@ -39,6 +45,7 @@ jobs:
"frontend_client/app/dist/*" \
"frontend_client/app/embed.js" \
"frontend_client/app/embedding-sdk/*" \
+ "frontend_client/app/embedding-sdk.js" \
-d resources
unzip -o -j "temp/uberjar/metabase.jar" version.properties -d artifacts
- name: Download bundle stats and SDK package artifacts
@@ -78,9 +85,27 @@ jobs:
mkdir -p artifacts resources/embedding-sdk
if [ -n "$STATS_ZIP" ]; then unzip -o "$STATS_ZIP" -d artifacts; fi
if [ -n "$PKG_ZIP" ]; then unzip -o "$PKG_ZIP" -d resources/embedding-sdk; fi
+ - name: Restore last plotted bundle sizes
+ uses: actions/cache/restore@v4
+ with:
+ path: bundle-size-cache
+ key: bundle-size-last-${{ github.run_id }}
+ restore-keys: |
+ bundle-size-last-
- name: Measure bundle sizes
run: node ./.github/scripts/measure-bundle-sizes.js | tee artifacts/bundle-sizes.json
- - name: Upload sizes to Stats
+ - name: Compute deltas and decide whether to record this point
+ id: prepare
+ env:
+ CURRENT: artifacts/bundle-sizes.json
+ LAST: bundle-size-cache/last.json
+ CACHE_OUT: bundle-size-cache/last.json
+ ROWS_OUT: artifacts/upload-rows.json
+ VERSION_PROPS: artifacts/version.properties
+ run: node ./.github/scripts/bundle-size-stats-prepare.js
+ - name: Upload sizes (with deltas) to Stats
+ id: upload
+ if: steps.prepare.outputs.significant == 'true'
uses: actions/github-script@v9
env:
API_KEY: ${{ secrets.STATS_API_KEY }}
@@ -89,43 +114,33 @@ jobs:
const fs = require("fs");
const { uploadCsvToMb } = require("${{ github.workspace }}/.github/scripts/csv-to-mb.js");
- const measurements = JSON.parse(
- fs.readFileSync("${{ github.workspace }}/artifacts/bundle-sizes.json", "utf8"),
+ const data = JSON.parse(
+ fs.readFileSync("${{ github.workspace }}/artifacts/upload-rows.json", "utf8"),
);
- const date = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
- const commit = process.env.HEAD_SHA.slice(0, 12);
-
- // version.properties (extracted from the uberjar) carries the build's tag.
- // Non-release (master) builds report tag=vUNKNOWN, so leave Version empty there.
- const versionProps = "${{ github.workspace }}/artifacts/version.properties";
- const rawVersion = fs.existsSync(versionProps)
- ? (fs.readFileSync(versionProps, "utf8").match(/^tag=(.*)$/m)?.[1]?.trim() ?? "")
- : "";
- const version = rawVersion === "vUNKNOWN" ? "" : rawVersion;
-
- const data = measurements.map(measurement => ({
- "Date": date,
- "Version": version,
- "Commit": commit,
- "Bundle": measurement.bundle,
- "Kind": measurement.kind, // "initial" or "total"
- "Raw bytes": measurement.rawBytes,
- "Gzip bytes": measurement.gzipBytes,
- "Brotli bytes": measurement.brotliBytes ?? "", // as-served; empty when no .br shipped
- "File count": measurement.fileCount,
- }));
-
console.table(data);
- uploadCsvToMb({
- baseUrl: "https://stats.metabase.com",
- tableId: 79417,
- jsonData: data,
- mode: "append",
- }).then(() => {
+ try {
+ await uploadCsvToMb({
+ baseUrl: "https://stats.metabase.com",
+ tableId: 79702,
+ jsonData: data,
+ mode: "append",
+ });
console.log("Bundle sizes uploaded successfully");
- }).catch(error => {
- console.error("Error uploading bundle sizes:", error);
- process.exit(1);
- });
+ core.setOutput("uploaded", "true");
+ } catch (error) {
+ // Stats logging is best-effort: if stats.metabase.com is still
+ // unreachable after the client's retries, keep the run green. The
+ // last-plotted cache is only saved on a successful upload (below),
+ // so this point is re-attempted on the next master build and
+ // nothing is silently dropped.
+ core.warning(`Bundle-size upload failed after retries; leaving run green: ${error.message}`);
+ core.setOutput("uploaded", "false");
+ }
+ - name: Remember this as the last plotted point
+ if: steps.upload.outputs.uploaded == 'true'
+ uses: actions/cache/save@v4
+ with:
+ path: bundle-size-cache
+ key: bundle-size-last-${{ github.run_id }}
diff --git a/.github/workflows/embedding-sdk.yml b/.github/workflows/embedding-sdk.yml
index a7f5b77884e..ab84f0ca223 100644
--- a/.github/workflows/embedding-sdk.yml
+++ b/.github/workflows/embedding-sdk.yml
@@ -150,7 +150,7 @@ jobs:
with:
remove-previous-comment: true
add-new-comment: true
- message: "⚠️ Embedding SDK bundle size increased by ${{ steps.compare.outputs.size_change_percent }}% compared to the ${{ github.base_ref }} branch (threshold: ${{ env.BUNDLE_SIZE_THRESHOLD_PERCENT }}%). Please contact Embedding team."
+ message: "⚠️ Embedding SDK chunked bundle size (gzipped) increased by ${{ steps.compare.outputs.size_change_percent }}% compared to the ${{ github.base_ref }} branch (threshold: ${{ env.BUNDLE_SIZE_THRESHOLD_PERCENT }}%). Please contact Embedding team."
include-log: false
- name: Send notification to Slack via Zapier
@@ -165,7 +165,7 @@ jobs:
PR_URL: ${{ github.event.pull_request.html_url }}
run: |
curl -s -X POST "$ZAPIER_WEBHOOK_URL" \
- -d "⚠️ Embedding SDK bundle size increased by ${SIZE_CHANGE_PERCENT}% (threshold: ${BUNDLE_SIZE_THRESHOLD_PERCENT}%)
+ -d "⚠️ Embedding SDK chunked bundle size (gzipped) increased by ${SIZE_CHANGE_PERCENT}% (threshold: ${BUNDLE_SIZE_THRESHOLD_PERCENT}%)
PR #${PR_NUMBER} ${PR_TITLE} (${PR_URL})"
@@ -175,7 +175,7 @@ jobs:
with:
remove-previous-comment: true
add-new-comment: true
- message: "🎉 Embedding SDK bundle size decreased by ${{ steps.compare.outputs.size_change_percent }}% compared to the ${{ github.base_ref }} branch. Nice work!"
+ message: "🎉 Embedding SDK chunked bundle size (gzipped) decreased by ${{ steps.compare.outputs.size_change_percent }}% compared to the ${{ github.base_ref }} branch. Nice work!"
include-log: false
embedding-sdk-tests-result:
diff --git a/.github/workflows/uberjar.yml b/.github/workflows/uberjar.yml
index 831542640a1..0893aa256a6 100644
--- a/.github/workflows/uberjar.yml
+++ b/.github/workflows/uberjar.yml
@@ -128,8 +128,9 @@ jobs:
MB_EDITION: ${{ matrix.edition }}
INTERACTIVE: false
SKIP_LICENSES: ${{ github.event_name == 'pull_request' }} # faster builds for dev
- # Emit bundle stats on trunk/release builds so they can be tracked over time
- EMIT_BUNDLE_STATS: ${{ (github.ref_name == 'master' || startsWith(github.ref_name, 'release-')) && 'true' || 'false' }}
+ # Emit bundle stats on trunk/release builds (tracked over time) and on PRs
+ # (the bundle-size check reads them for both the PR head and its base ref).
+ EMIT_BUNDLE_STATS: ${{ (github.ref_name == 'master' || startsWith(github.ref_name, 'release-') || github.event_name == 'pull_request') && 'true' || 'false' }}
steps:
- name: Check out the code
uses: actions/checkout@v6
diff --git a/frontend/build/shared/rspack/bundle-stats.js b/frontend/build/shared/rspack/bundle-stats.js
index 44e97854dc8..1fa7807b2e7 100644
--- a/frontend/build/shared/rspack/bundle-stats.js
+++ b/frontend/build/shared/rspack/bundle-stats.js
@@ -7,11 +7,40 @@ const EMIT_BUNDLE_STATS = process.env.EMIT_BUNDLE_STATS === "true";
// below the repo root, so this relative path lands in <repo>/target for both.
const STATS_OUTPUT_PREFIX = "../../../../target/bundle-stats/";
+// Walk the chunk graph from an entrypoint's initial chunks over their async
+// children, returning every .js asset reachable from that entrypoint (initial +
+// lazily imported). Lets measure-bundle-sizes.js scope an entrypoint's "total"
+// to its own reachable code instead of the whole output dir.
+function reachableJsAssets(initialChunkIds, chunkById) {
+ const seen = new Set();
+ const queue = [...(initialChunkIds || [])];
+ const files = new Set();
+ while (queue.length > 0) {
+ const id = queue.shift();
+ if (seen.has(id)) {
+ continue;
+ }
+ seen.add(id);
+ const chunk = chunkById.get(id);
+ if (!chunk) {
+ continue;
+ }
+ for (const file of chunk.files) {
+ files.add(file);
+ }
+ for (const child of chunk.children) {
+ queue.push(child);
+ }
+ }
+ return [...files];
+}
+
/**
* When EMIT_BUNDLE_STATS is set, writes a minimal stats file (asset names/sizes
* plus per-entrypoint asset lists) outside the bundle output dir so it never
* ships inside the uberjar. .github/scripts/measure-bundle-sizes.js reads it to
- * split "initial" (entry + sync chunks) from "total" bundle size.
+ * split each entrypoint's "initial" (entry + sync chunks) from its reachable
+ * "total" (initial + async children).
*
* Returns an array so it can be spread into a `plugins` list and disappear when
* stats are not requested.
@@ -22,10 +51,26 @@ function bundleStatsPlugins(statsFileName) {
}
return [
new StatsWriterPlugin({
- stats: { all: false, assets: true, entrypoints: true },
+ stats: {
+ all: false,
+ assets: true,
+ entrypoints: true,
+ chunks: true,
+ ids: true,
+ chunkRelations: true,
+ },
filename: STATS_OUTPUT_PREFIX + statsFileName,
- transform: (stats) =>
- JSON.stringify({
+ transform: (stats) => {
+ const chunkById = new Map(
+ (stats.chunks || []).map((chunk) => [
+ chunk.id,
+ {
+ files: (chunk.files || []).filter((file) => file.endsWith(".js")),
+ children: chunk.children || [],
+ },
+ ]),
+ );
+ return JSON.stringify({
assets: (stats.assets || []).map((asset) => ({
name: asset.name,
size: asset.size,
@@ -37,10 +82,14 @@ function bundleStatsPlugins(statsFileName) {
assets: (entry.assets || []).map((asset) => ({
name: asset.name || asset,
})),
+ reachableAssets: reachableJsAssets(entry.chunks, chunkById).map(
+ (file) => ({ name: file }),
+ ),
},
]),
),
- }),
+ });
+ },
}),
];
}