sharding algorithm to better spread similar tests among shards
This commit is contained in:
parent
0e0b426e47
commit
54bbba8cf4
|
|
@ -138,27 +138,15 @@ export function filterForShard(shard: { total: number, current: number }, testGr
|
||||||
// Shards are still balanced by the number of tests, not files,
|
// Shards are still balanced by the number of tests, not files,
|
||||||
// even in the case of non-paralleled files.
|
// even in the case of non-paralleled files.
|
||||||
|
|
||||||
let shardableTotal = 0;
|
const lengths = new Array(shard.total).fill(0);
|
||||||
for (const group of testGroups)
|
const shardSet = new Array(shard.total).fill(0).map(() => new Set<TestGroup>());
|
||||||
shardableTotal += group.tests.length;
|
|
||||||
|
|
||||||
// Each shard gets some tests.
|
|
||||||
const shardSize = Math.floor(shardableTotal / shard.total);
|
|
||||||
// First few shards get one more test each.
|
|
||||||
const extraOne = shardableTotal - shardSize * shard.total;
|
|
||||||
|
|
||||||
const currentShard = shard.current - 1; // Make it zero-based for calculations.
|
|
||||||
const from = shardSize * currentShard + Math.min(extraOne, currentShard);
|
|
||||||
const to = from + shardSize + (currentShard < extraOne ? 1 : 0);
|
|
||||||
|
|
||||||
let current = 0;
|
|
||||||
const result = new Set<TestGroup>();
|
|
||||||
for (const group of testGroups) {
|
for (const group of testGroups) {
|
||||||
// Any test group goes to the shard that contains the first test of this group.
|
// We add the group to the shard with the smallest number of tests.
|
||||||
// So, this shard gets any group that starts at [from; to)
|
const index = lengths.reduce((minIndex, currentLength, currentIndex) => currentLength < lengths[minIndex] ? currentIndex : minIndex, 0);
|
||||||
if (current >= from && current < to)
|
lengths[index] += group.tests.length;
|
||||||
result.add(group);
|
shardSet[index].add(group);
|
||||||
current += group.tests.length;
|
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
|
return shardSet[shard.current - 1];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue