From e490df79caa75e7937ebe1eaa4e8105b4796ff28 Mon Sep 17 00:00:00 2001 From: Harvey Date: Sun, 22 Nov 2026 18:46:03 +0330 Subject: [PATCH 1/2] ci: average shard timings with the previous run's When a green push to main publishes e2e-timings, each file's time is averaged half and half with the timings that run packed by, so one noisy run moves the plan only halfway. New specs keep their own time, deleted ones drop out, and without usable previous timings the run's own are written. --- .github/workflows/ci.yml | 8 ++++- CHANGELOG.md | 18 ++++++++++ scripts/e2e-shards.ts | 56 ++++++++++++++++++++++++++----- tests/TESTING.md | 4 ++- tests/unit/ci/ci-workflow.test.ts | 4 +++ tests/unit/ci/e2e-shards.test.ts | 36 ++++++++++++++++++++ 6 files changed, 116 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 99b74b05..1770ab40 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -506,13 +506,19 @@ jobs: fi # Green pushes to main only: the shard timings the next runs pack by. + # Averaged with the timings this run packed by (the changes job's + # output, from the previous green push), so one noisy run moves the + # plan only halfway. - name: Write shard timings if: success() && github.event_name == 'push' && github.ref == 'refs/heads/main' env: PLAYWRIGHT_JSON_OUTPUT_NAME: ${{ runner.temp }}/report.json + PREVIOUS: ${{ needs.changes.outputs.timings }} run: | bun playwright merge-reports --reporter=json all-blob-reports - bun scripts/e2e-shards.ts timings "$RUNNER_TEMP/report.json" e2e-timings.json + printf '%s' "$PREVIOUS" > "$RUNNER_TEMP/previous-timings.json" + bun scripts/e2e-shards.ts timings "$RUNNER_TEMP/report.json" \ + e2e-timings.json "$RUNNER_TEMP/previous-timings.json" - name: Publish shard timings if: success() && github.event_name == 'push' && github.ref == 'refs/heads/main' diff --git a/CHANGELOG.md b/CHANGELOG.md index 3efc8cf6..f6a57384 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,24 @@ since the previous tag. ## [Unreleased] +### CI: shard timings averaged across runs + +One run's per-file times are noisy (`mobile-bar.spec.ts` took 116 s in one +run and 263 s in the next), and a plan packed from a single run inherits +that noise, so a shard could carry 16% more than planned. + +#### Changed + +- **CI:** when a green push to `main` publishes `e2e-timings`, each file's + time is averaged with the timings that run packed by (an exponential + moving average, half weight to the newest run). A new spec keeps its own + time and a deleted one is dropped; without usable previous timings the + run's own are written, as before. +- **Tests:** `e2e-shards.test.ts` covers the average; `ci-workflow.test.ts` + pins that the publish step reads the timings the run packed by. + +**Deploy:** none. + ### CI: four shards, two perf jobs and parallel unit tests The suite has grown to 627 browser tests (about 3,500 s), so three packed diff --git a/scripts/e2e-shards.ts b/scripts/e2e-shards.ts index 1cacc577..b8356577 100644 --- a/scripts/e2e-shards.ts +++ b/scripts/e2e-shards.ts @@ -7,8 +7,10 @@ // Prints the Playwright arguments for this shard: `--test-list=` // after writing the shard's files there, or `--shard=/` // (Playwright's own split) when the timings are missing or invalid. -// bun scripts/e2e-shards.ts timings -// Writes per-file seconds from a merged Playwright JSON report. +// bun scripts/e2e-shards.ts timings [previous.json] +// Writes per-file seconds from a merged Playwright JSON report, averaged +// with the previous timings (when given and valid) so one slow or noisy +// run moves the plan only halfway. // // Every shard computes the same plan from the same inputs, so together // they run each spec file exactly once. @@ -107,6 +109,30 @@ export const timingsFromReport = (report: { suites?: ReportSuite[] }) => { return timings; }; +/** Weight of the newest run in {@link smoothTimings}. */ +export const SMOOTHING = 0.5; + +/** + * An exponential moving average per file: `weight` of the newest run plus + * the rest of the previous average. A file the newest run didn't time (a + * deleted spec) is dropped; a file with no previous time keeps its new one. + */ +export const smoothTimings = ( + current: Timings, + previous: Timings, + weight = SMOOTHING, +): Timings => + Object.fromEntries( + Object.entries(current).map(([file, seconds]) => { + const before = previous[file]; + const blended = + before === undefined + ? seconds + : weight * seconds + (1 - weight) * before; + return [file, Math.round(blended * 10) / 10]; + }), + ); + /** Spec files Playwright would run, relative to its rootDir. */ const listSpecFiles = (projects: string[]) => { const result = Bun.spawnSync( @@ -161,16 +187,30 @@ if (import.meta.main) { const [command, ...args] = process.argv.slice(2); if (command === "plan" && args.length === 3) { plan(args[0], args[1], args[2]); - } else if (command === "timings" && args.length === 2) { + } else if ( + command === "timings" && + (args.length === 2 || args.length === 3) + ) { const report = JSON.parse(readFileSync(args[0], "utf8")); - writeFileSync( - args[1], - `${JSON.stringify(timingsFromReport(report), null, 2)}\n`, - ); + let timings = timingsFromReport(report); + if (args[2]) { + try { + timings = smoothTimings( + timings, + parseTimings(readFileSync(args[2], "utf8")), + ); + console.error("e2e-shards: averaged with the previous timings"); + } catch (error) { + console.error( + `e2e-shards: no usable previous timings (${(error as Error).message}); writing this run's`, + ); + } + } + writeFileSync(args[1], `${JSON.stringify(timings, null, 2)}\n`); } else { console.error( "usage: e2e-shards.ts plan / \n" + - " e2e-shards.ts timings ", + " e2e-shards.ts timings [previous.json]", ); process.exit(2); } diff --git a/tests/TESTING.md b/tests/TESTING.md index efdb233a..08e914d7 100644 --- a/tests/TESTING.md +++ b/tests/TESTING.md @@ -629,7 +629,9 @@ Each client gets a unique identity token (`load-client-{i}-{docId}`). spec files into four shards by duration (longest first, into the least-loaded shard; a new spec counts as the median), from per-file seconds that `Playwright (Chromium)` publishes as the `e2e-timings` - artifact on green pushes to `main` only. The `Changed files` job reads them + artifact on green pushes to `main` only, each file averaged half and half + with the timings that run packed by (one run's noise, such as a spec + taking twice as long on a slow runner, moves the plan only halfway). The `Changed files` job reads them once, so every shard packs the same plan; without usable timings (none yet, expired after 30 days, or invalid) every shard uses Playwright's own `--shard=N/4`. Each browser job logs its runner's CPU before the suites. diff --git a/tests/unit/ci/ci-workflow.test.ts b/tests/unit/ci/ci-workflow.test.ts index b70f769f..67144e69 100644 --- a/tests/unit/ci/ci-workflow.test.ts +++ b/tests/unit/ci/ci-workflow.test.ts @@ -137,6 +137,10 @@ describe("CI workflow", () => { ), ); expect(writers).toHaveLength(2); + // Averaged with the timings this run read, never written from a PR. + const write = writers.find((step) => step.name === "Write shard timings"); + expect(write?.env?.PREVIOUS).toBe(`\${{ needs.changes.outputs.timings }}`); + expect(write?.run).toContain("previous-timings.json"); for (const step of writers) expect(step.if).toBe(MAIN_ONLY); // No other job uploads the timings artifact. const uploads = Object.values(jobs).flatMap((job) => diff --git a/tests/unit/ci/e2e-shards.test.ts b/tests/unit/ci/e2e-shards.test.ts index 1a8e1109..014f1c15 100644 --- a/tests/unit/ci/e2e-shards.test.ts +++ b/tests/unit/ci/e2e-shards.test.ts @@ -3,6 +3,7 @@ import { describe, expect, it } from "bun:test"; import { packShards, parseTimings, + smoothTimings, timingsFromReport, } from "../../../scripts/e2e-shards"; @@ -138,3 +139,38 @@ describe("timingsFromReport", () => { }); }); }); + +describe("smoothTimings", () => { + it.each([ + { + name: "averages a file timed in both runs", + current: { a: 263 }, + previous: { a: 116 }, + expected: { a: 189.5 }, + }, + { + name: "keeps a new file's own time", + current: { a: 10, fresh: 42 }, + previous: { a: 20 }, + expected: { a: 15, fresh: 42 }, + }, + { + name: "drops a file the new run did not time", + current: { a: 10 }, + previous: { a: 10, deleted: 300 }, + expected: { a: 10 }, + }, + { + name: "converges on a steady time", + current: { a: 100 }, + previous: { a: 100 }, + expected: { a: 100 }, + }, + ])("$name", ({ current, previous, expected }) => { + expect(smoothTimings(current, previous)).toEqual(expected); + }); + + it("weights the newest run as asked", () => { + expect(smoothTimings({ a: 100 }, { a: 0 }, 0.25)).toEqual({ a: 25 }); + }); +}); From e20794df59c922d4c2bb793ec7e68e447ea4d1e8 Mon Sep 17 00:00:00 2001 From: Harvey Date: Sun, 22 Nov 2026 19:07:48 +0330 Subject: [PATCH 2/2] test(ci): type the timing-smoothing cases as Timings --- tests/unit/ci/e2e-shards.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/unit/ci/e2e-shards.test.ts b/tests/unit/ci/e2e-shards.test.ts index 014f1c15..0ced0a1e 100644 --- a/tests/unit/ci/e2e-shards.test.ts +++ b/tests/unit/ci/e2e-shards.test.ts @@ -4,6 +4,7 @@ import { packShards, parseTimings, smoothTimings, + type Timings, timingsFromReport, } from "../../../scripts/e2e-shards"; @@ -141,7 +142,12 @@ describe("timingsFromReport", () => { }); describe("smoothTimings", () => { - it.each([ + const cases: { + name: string; + current: Timings; + previous: Timings; + expected: Timings; + }[] = [ { name: "averages a file timed in both runs", current: { a: 263 }, @@ -166,7 +172,9 @@ describe("smoothTimings", () => { previous: { a: 100 }, expected: { a: 100 }, }, - ])("$name", ({ current, previous, expected }) => { + ]; + + it.each(cases)("$name", ({ current, previous, expected }) => { expect(smoothTimings(current, previous)).toEqual(expected); });