Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 48 additions & 8 deletions scripts/e2e-shards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
// Prints the Playwright arguments for this shard: `--test-list=<list.txt>`
// after writing the shard's files there, or `--shard=<current>/<total>`
// (Playwright's own split) when the timings are missing or invalid.
// bun scripts/e2e-shards.ts timings <report.json> <timings.json>
// Writes per-file seconds from a merged Playwright JSON report.
// bun scripts/e2e-shards.ts timings <report.json> <timings.json> [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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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>/<total> <timings.json> <list.txt>\n" +
" e2e-shards.ts timings <report.json> <timings.json>",
" e2e-shards.ts timings <report.json> <timings.json> [previous.json]",
);
process.exit(2);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/ci/ci-workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/ci/e2e-shards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { describe, expect, it } from "bun:test";
import {
packShards,
parseTimings,
smoothTimings,
type Timings,
timingsFromReport,
} from "../../../scripts/e2e-shards";

Expand Down Expand Up @@ -138,3 +140,45 @@ describe("timingsFromReport", () => {
});
});
});

describe("smoothTimings", () => {
const cases: {
name: string;
current: Timings;
previous: Timings;
expected: Timings;
}[] = [
{
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 },
},
];

it.each(cases)("$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 });
});
});
Loading