perf(ci): speed up full WebUI regression gates (#6684)
* perf(ci): speed up full WebUI regression gates * perf(ci): balance browser shards with measured hosted timings
This commit is contained in:
1 parent
e91906bc3a
commit
7782095a85
21 files changed
+821
-346
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import path from "node:path";
|
||||
|
||||
// Use native discovery and execution reports so test-list filtering cannot turn
|
||||
// a nonempty planned shard into a successful empty or incomplete execution.
|
||||
export function browserTests(report, root) {
|
||||
const tests = [];
|
||||
function visit(suite) {
|
||||
for (const spec of suite.specs ?? []) {
|
||||
const file = path.relative(root, path.resolve(report.config.rootDir, spec.file)).replaceAll("\\", "/");
|
||||
for (const test of spec.tests) {
|
||||
tests.push({ file, id: `${spec.id}:${test.projectId}` });
|
||||
}
|
||||
}
|
||||
suite.suites?.forEach(visit);
|
||||
}
|
||||
report.suites.forEach(visit);
|
||||
return tests;
|
||||
}
|
||||
|
||||
export function verifyBrowserShard(report, expected, root) {
|
||||
const actual = browserTests(report, root).map((test) => test.id).sort();
|
||||
const wanted = expected.map((test) => test.id).sort();
|
||||
if (actual.join("\n") !== wanted.join("\n")) {
|
||||
throw new Error("Browser shard execution differs from full Playwright discovery");
|
||||
}
|
||||
console.log(`Verified all ${wanted.length} planned browser tests executed exactly once`);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import path from "node:path";
|
||||
import { browserTests, verifyBrowserShard } from "./browser-report.mjs";
|
||||
|
||||
const root = path.resolve("webui-fixture");
|
||||
const spec = (id, file) => ({ id, file, tests: [{ projectId: "chromium" }] });
|
||||
function report(specs) {
|
||||
return { config: { rootDir: path.join(root, "browser") }, suites: [{ suites: [{ specs }] }] };
|
||||
}
|
||||
|
||||
const specs = [spec("first", "nested/first.spec.ts"), spec("second", "second.spec.ts")];
|
||||
const expected = [
|
||||
{ file: "browser/nested/first.spec.ts", id: "first:chromium" },
|
||||
{ file: "browser/second.spec.ts", id: "second:chromium" },
|
||||
];
|
||||
|
||||
test("native nested reports retain file and project identities in discovery and execution", () => {
|
||||
expect(browserTests(report(specs), root)).toEqual(expected);
|
||||
expect(() => verifyBrowserShard(report([...specs].reverse()), expected, root)).not.toThrow();
|
||||
});
|
||||
|
||||
test("an empty or partially filtered browser shard cannot pass", () => {
|
||||
expect(() => verifyBrowserShard(report([]), expected, root)).toThrow("differs");
|
||||
expect(() => verifyBrowserShard(report(specs.slice(0, 1)), expected, root)).toThrow("differs");
|
||||
});
|
||||
|
||||
test("running a test twice cannot replace another planned test", () => {
|
||||
expect(() => verifyBrowserShard(report([specs[0], specs[0]]), expected, root)).toThrow("differs");
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { parseArgs } from "node:util";
|
||||
import { loadDurations, partitionFiles } from "./test-shards.mjs";
|
||||
import { browserTests, verifyBrowserShard } from "./browser-report.mjs";
|
||||
|
||||
const cli = path.resolve("node_modules/@playwright/test/cli.js");
|
||||
|
||||
function collectFiles() {
|
||||
const env = { ...process.env };
|
||||
delete env.PLAYWRIGHT_JSON_OUTPUT_NAME;
|
||||
delete env.PLAYWRIGHT_JSON_OUTPUT_DIR;
|
||||
const collected = spawnSync("node", [cli, "test", "--list", "--reporter=json"], {
|
||||
env,
|
||||
encoding: "utf8",
|
||||
maxBuffer: 20 * 1024 * 1024,
|
||||
});
|
||||
if (collected.error) throw collected.error;
|
||||
if (collected.status !== 0) {
|
||||
process.stderr.write(collected.stderr);
|
||||
process.stderr.write(collected.stdout);
|
||||
throw new Error("Playwright test discovery failed");
|
||||
}
|
||||
const report = JSON.parse(collected.stdout);
|
||||
const tests = browserTests(report, process.cwd());
|
||||
return { rootDir: report.config.rootDir, files: [...new Set(tests.map((test) => test.file))], tests };
|
||||
}
|
||||
|
||||
const { values } = parseArgs({
|
||||
options: {
|
||||
shard: { type: "string" },
|
||||
list: { type: "boolean", default: false },
|
||||
trace: { type: "string", default: "retain-on-failure" },
|
||||
},
|
||||
});
|
||||
const match = /^([1-9]\d*)\/([1-9]\d*)$/.exec(values.shard ?? "");
|
||||
if (!match) throw new Error("Use --shard=index/count, for example --shard=1/4");
|
||||
const [, indexText, countText] = match;
|
||||
const index = Number(indexText);
|
||||
const count = Number(countText);
|
||||
if (index > count) throw new Error("Shard index exceeds shard count");
|
||||
|
||||
const { rootDir, files, tests } = collectFiles();
|
||||
const shards = partitionFiles(files, count, loadDurations());
|
||||
const selected = shards[index - 1];
|
||||
if (!selected.length) throw new Error("Browser shard is empty");
|
||||
if (values.list) {
|
||||
console.log(JSON.stringify({ shard: index, count, files: selected }, null, 2));
|
||||
} else {
|
||||
const directory = mkdtempSync(path.join(tmpdir(), "nanobot-browser-shard-"));
|
||||
try {
|
||||
const list = path.join(directory, "tests.txt");
|
||||
writeFileSync(list, selected.map((file) => path.relative(rootDir, path.resolve(file)).replaceAll("\\", "/")).join("\n"));
|
||||
console.log(`Browser shard ${index}/${count}: ${selected.length}/${files.length} files`);
|
||||
const reportFile = path.resolve(`test-results/browser/results-${index}-of-${count}.json`);
|
||||
rmSync(reportFile, { force: true });
|
||||
const result = spawnSync("node", [
|
||||
cli, "test", `--test-list=${list}`, `--trace=${values.trace}`, "--reporter=list,json",
|
||||
], {
|
||||
stdio: "inherit",
|
||||
env: {
|
||||
...process.env,
|
||||
PLAYWRIGHT_JSON_OUTPUT_NAME: reportFile,
|
||||
},
|
||||
});
|
||||
if (result.error) throw result.error;
|
||||
process.exitCode = result.status ?? 1;
|
||||
if (result.status === 0) {
|
||||
const selectedFiles = new Set(selected);
|
||||
verifyBrowserShard(JSON.parse(readFileSync(reportFile, "utf8")), tests.filter((test) => selectedFiles.has(test.file)), process.cwd());
|
||||
}
|
||||
} finally {
|
||||
rmSync(directory, { recursive: true });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
const timingsFile = new URL("../test-timings.json", import.meta.url);
|
||||
|
||||
/**
|
||||
* Assign every discovered file exactly once using longest-processing-time first.
|
||||
* Equal durations use path order; equal loads use file count, then shard index.
|
||||
* @param {Array<string>} files Normalized paths relative to webui/.
|
||||
* @param {number} count
|
||||
* @param {Record<string, number>} durations Durations in milliseconds.
|
||||
* @returns {Array<Array<string>>}
|
||||
*/
|
||||
export function partitionFiles(files, count, durations) {
|
||||
if (!Number.isInteger(count) || count < 1) {
|
||||
throw new Error("Shard count must be a positive integer");
|
||||
}
|
||||
if (new Set(files).size !== files.length) {
|
||||
throw new Error("Discovered test files contain duplicates");
|
||||
}
|
||||
const weight = (file) => {
|
||||
// New files are never selected out by a stale timing manifest.
|
||||
const duration = Object.hasOwn(durations, file) ? durations[file] : 1;
|
||||
if (!Number.isFinite(duration) || duration < 0) {
|
||||
throw new Error(`Invalid test duration for ${file}`);
|
||||
}
|
||||
return duration;
|
||||
};
|
||||
const ordered = files.map((file) => ({ file, duration: weight(file) })).sort((a, b) =>
|
||||
b.duration - a.duration || (a.file < b.file ? -1 : a.file > b.file ? 1 : 0),
|
||||
);
|
||||
const shards = Array.from({ length: count }, () => []);
|
||||
const loads = Array(count).fill(0);
|
||||
for (const { file, duration } of ordered) {
|
||||
let selected = 0;
|
||||
for (let index = 1; index < count; index += 1) {
|
||||
if (loads[index] < loads[selected]
|
||||
|| (loads[index] === loads[selected] && shards[index].length < shards[selected].length)) {
|
||||
selected = index;
|
||||
}
|
||||
}
|
||||
shards[selected].push(file);
|
||||
loads[selected] += duration;
|
||||
}
|
||||
return shards;
|
||||
}
|
||||
|
||||
/** @returns {Record<string, number>} */
|
||||
export function loadDurations() {
|
||||
return JSON.parse(readFileSync(timingsFile, "utf8")).browser;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { partitionFiles } from "./test-shards.mjs";
|
||||
|
||||
const files = ["browser/a.spec.ts", "browser/b.spec.ts", "browser/c.spec.ts", "browser/nested/d.spec.ts"];
|
||||
const durations = { [files[0]]: 9, [files[1]]: 8, [files[2]]: 7, [files[3]]: 6 };
|
||||
|
||||
test("LPT balances measured durations without changing the inputs", () => {
|
||||
const input = Object.freeze([...files]);
|
||||
expect(partitionFiles(input, 2, Object.freeze(durations))).toEqual([
|
||||
[files[0], files[3]], [files[1], files[2]],
|
||||
]);
|
||||
expect(input).toEqual(files);
|
||||
});
|
||||
|
||||
test("equal loads and durations are deterministic across discovery order", () => {
|
||||
const input = ["e", "b", "d", "a", "c"];
|
||||
expect(partitionFiles(input, 2, {})).toEqual([["a", "c", "e"], ["b", "d"]]);
|
||||
expect(partitionFiles([...input].reverse(), 2, {})).toEqual(partitionFiles(input, 2, {}));
|
||||
expect(partitionFiles(input, 2, Object.fromEntries(input.map((file) => [file, 0]))))
|
||||
.toEqual([["a", "c", "e"], ["b", "d"]]);
|
||||
});
|
||||
|
||||
test("stale timings cannot omit newly discovered tests", () => {
|
||||
const discovered = [...files, "browser/new.spec.ts", "browser/nested/new.spec.ts"];
|
||||
const shards = partitionFiles(discovered, 3, { ...durations, "browser/deleted.spec.ts": 100_000 });
|
||||
expect(shards.flat().sort()).toEqual([...discovered].sort());
|
||||
expect(new Set(shards.flat()).size).toBe(discovered.length);
|
||||
expect(partitionFiles([], 2, {})).toEqual([[], []]);
|
||||
});
|
||||
|
||||
test("invalid counts and duplicate discovery fail explicitly", () => {
|
||||
expect(() => partitionFiles(files, 0, durations)).toThrow("positive integer");
|
||||
expect(() => partitionFiles([files[0], files[0]], 2, {})).toThrow("duplicates");
|
||||
});
|
||||
Reference in new issue
Block a user