From ee64b15d479a8da8fd84e3dd8af47c08435c2e60 Mon Sep 17 00:00:00 2001 From: Mathias Leppich Date: Wed, 22 May 2024 19:22:22 +0200 Subject: [PATCH] improve shard algorithm by sorting test groups by number of tests --- packages/playwright/src/runner/testGroups.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/playwright/src/runner/testGroups.ts b/packages/playwright/src/runner/testGroups.ts index 284fe2066c..9b75cad399 100644 --- a/packages/playwright/src/runner/testGroups.ts +++ b/packages/playwright/src/runner/testGroups.ts @@ -141,8 +141,11 @@ export function filterForShard(shard: { total: number, current: number }, testGr const lengths = new Array(shard.total).fill(0); const shardSet = new Array(shard.total).fill(0).map(() => new Set()); - for (const group of testGroups) { - // We add the group to the shard with the smallest number of tests. + // We sort the test groups by the number of tests in descending order. + const sortedTestGroups = testGroups.slice().sort((a, b) => b.tests.length - a.tests.length); + + // Then we add each group to the shard with the smallest number of tests. + for (const group of sortedTestGroups) { const index = lengths.reduce((minIndex, currentLength, currentIndex) => currentLength < lengths[minIndex] ? currentIndex : minIndex, 0); lengths[index] += group.tests.length; shardSet[index].add(group);