diff --git a/scripts/__tests__/e2e-shard.test.mjs b/scripts/__tests__/e2e-shard.test.mjs index fc4fa2c58cc..244c308f34b 100644 --- a/scripts/__tests__/e2e-shard.test.mjs +++ b/scripts/__tests__/e2e-shard.test.mjs @@ -16,7 +16,9 @@ import { isSerialE2eSpec, listE2eSpecs, partitionE2eShards, + SERIAL_SLOWDOWN_2W, resolveEffectiveParallelSpeedup, + resolveSerialSlowdown, selectE2eShard, } from "../e2e-shard.mjs"; @@ -186,6 +188,46 @@ test("resolveEffectiveParallelSpeedup matches Playwright worker scaling for 4-vC assert.equal(resolveEffectiveParallelSpeedup(16), EFFECTIVE_PARALLEL_SPEEDUP_4W); }); +test("resolveSerialSlowdown only calibrates 2-worker (4-vCPU hosted) runners", () => { + assert.equal(resolveSerialSlowdown(2), 1); + assert.equal(resolveSerialSlowdown(4), SERIAL_SLOWDOWN_2W); + assert.equal(resolveSerialSlowdown(5), SERIAL_SLOWDOWN_2W); + assert.equal(resolveSerialSlowdown(8), 1); +}); + +test("4-shard hosted partitioning splits serial specs across two shards with the hosted calibration", () => { + const durations = loadShardDurations(durationsManifest); + const specs = listE2eSpecs(); + const options = { parallelWorkers: EFFECTIVE_PARALLEL_SPEEDUP, serialSlowdown: SERIAL_SLOWDOWN_2W }; + const shards = partitionE2eShards(specs, TARGET_HOSTED_SHARD_COUNT, durations, options); + assert.equal(shards.length, TARGET_HOSTED_SHARD_COUNT); + assert.deepEqual([...shards.flatMap((shard) => shard.files)].sort(), [...specs].sort()); + const serialShards = shards.filter((shard) => shard.mode === "serial"); + assert.ok( + serialShards.length >= 2, + `hosted runs must not put every serial spec on one shard: ${serialShards.map((s) => s.files.length).join(",")}`, + ); +}); + +test("the CLI applies the hosted serial calibration by default on a 4-vCPU runner", () => { + const env = { ...process.env }; + delete env.PAPERCLIP_E2E_PARALLEL_SPEEDUP; + delete env.PAPERCLIP_E2E_SERIAL_SLOWDOWN; + const fourCpus = "data:text/javascript,import os from 'node:os'; os.availableParallelism = () => 4;"; + const shards = Array.from({ length: TARGET_HOSTED_SHARD_COUNT }, (_, index) => { + const result = spawnSync( + process.execPath, + ["--import", fourCpus, script, "--shard-index", String(index), "--shard-count", String(TARGET_HOSTED_SHARD_COUNT)], + { cwd: repoRoot, encoding: "utf8", env }, + ); + assert.equal(result.status, 0, result.stderr); + return result.stdout.trim().split(/\s+/).filter(Boolean); + }); + const serialShards = shards.filter((files) => files.some(isSerialE2eSpec)); + assert.equal(serialShards.length, 2); + for (const files of serialShards) assert.ok(files.every(isSerialE2eSpec)); +}); + test("16-shard AWS partitioning stays complete, segregated, and balanced under 85s estimated wall-clock", () => { const durations = loadShardDurations(durationsManifest); const specs = listE2eSpecs(); diff --git a/scripts/e2e-shard.mjs b/scripts/e2e-shard.mjs index 755ddbb4b33..ccbfda8de5b 100644 --- a/scripts/e2e-shard.mjs +++ b/scripts/e2e-shard.mjs @@ -51,6 +51,13 @@ export function resolveEffectiveParallelSpeedup(cpus = os.availableParallelism() return 1; } +export const SERIAL_SLOWDOWN_2W = 1.45; + +export function resolveSerialSlowdown(cpus = os.availableParallelism()) { + const workers = Math.max(1, Math.min(4, Math.floor(cpus / 2))); + return workers === 2 ? SERIAL_SLOWDOWN_2W : 1; +} + export function isSerialE2eSpec(file) { return SERIAL_SPECS.includes(path.posix.basename(file.split(path.sep).join("/"))); } @@ -72,7 +79,7 @@ export function partitionE2eShards( files, shardCount, durations = {}, - { parallelWorkers = EFFECTIVE_PARALLEL_SPEEDUP } = {}, + { parallelWorkers = EFFECTIVE_PARALLEL_SPEEDUP, serialSlowdown = 1 } = {}, ) { const serialFiles = files.filter(isSerialE2eSpec); const parallelFiles = files.filter((file) => !isSerialE2eSpec(file)); @@ -97,7 +104,7 @@ export function partitionE2eShards( const serialShards = partitionGeneralServerSuites(serialFiles, serialShardCount, durations).map((shard) => ({ ...shard, mode: "serial", - estimatedWallMs: shard.totalWeight, + estimatedWallMs: shard.totalWeight * serialSlowdown, })); const parallelShards = partitionGeneralServerSuites(parallelFiles, parallelShardCount, durations).map((shard) => ({ ...shard, @@ -124,17 +131,18 @@ export function selectE2eShard(files, shardIndex, shardCount, durations = {}, op } function parseArgs(argv) { - const args = { shardIndex: 0, shardCount: 1, parallelSpeedup: null }; + const args = { shardIndex: 0, shardCount: 1, parallelSpeedup: null, serialSlowdown: null }; for (let index = 0; index < argv.length; index += 1) { if (argv[index] === "--shard-index") args.shardIndex = Number(argv[index + 1]); if (argv[index] === "--shard-count") args.shardCount = Number(argv[index + 1]); if (argv[index] === "--parallel-speedup") args.parallelSpeedup = Number(argv[index + 1]); + if (argv[index] === "--serial-slowdown") args.serialSlowdown = Number(argv[index + 1]); } return args; } function main(argv) { - const { shardIndex, shardCount, parallelSpeedup } = parseArgs(argv); + const { shardIndex, shardCount, parallelSpeedup, serialSlowdown } = parseArgs(argv); if (!Number.isInteger(shardCount) || shardCount < 1) { throw new Error(`--shard-count must be a positive integer, got ${shardCount}`); } @@ -148,11 +156,21 @@ function main(argv) { : Number.isFinite(envSpeedup) && envSpeedup > 0 ? envSpeedup : resolveEffectiveParallelSpeedup(); + const envSlowdown = Number.parseFloat(process.env.PAPERCLIP_E2E_SERIAL_SLOWDOWN ?? ""); + const effectiveSlowdown = + Number.isFinite(serialSlowdown) && serialSlowdown > 0 + ? serialSlowdown + : Number.isFinite(envSlowdown) && envSlowdown > 0 + ? envSlowdown + : resolveSerialSlowdown(); const specs = listE2eSpecs(); const durations = loadShardDurations(DURATIONS_MANIFEST); process.stdout.write( - `${selectE2eShard(specs, shardIndex, shardCount, durations, { parallelWorkers: effectiveSpeedup }).join(" ")}\n`, + `${selectE2eShard(specs, shardIndex, shardCount, durations, { + parallelWorkers: effectiveSpeedup, + serialSlowdown: effectiveSlowdown, + }).join(" ")}\n`, ); }