Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: distribute rest files over shards #13476

Merged
merged 4 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -368,6 +368,23 @@ test('returns expected 100/8 shards', async () => {
);

expect(shards.map(shard => shard.length)).toEqual([
13, 13, 13, 13, 13, 13, 13, 9,
13, 13, 13, 13, 12, 12, 12, 12,
SimenB marked this conversation as resolved.
Show resolved Hide resolved
]);
});

test('returns expected 55/12 shards', async () => {
const allTests = toTests(new Array(55).fill(true).map((_, i) => `/${i}.js`));

const shards = await Promise.all(
new Array(12).fill(true).map((_, i) =>
sequencer.shard(allTests, {
shardCount: 12,
shardIndex: i + 1,
}),
),
);

expect(shards.map(shard => shard.length)).toEqual([
5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4,
]);
});
27 changes: 24 additions & 3 deletions packages/jest-test-sequencer/src/index.ts
Expand Up @@ -72,6 +72,19 @@ export default class TestSequencer {
return cache;
}

_shardPosition(options: ShardOptions & {suiteLength: number}): number {
marionebl marked this conversation as resolved.
Show resolved Hide resolved
const shardRest = options.suiteLength % options.shardCount;
const ratio = options.suiteLength / options.shardCount;

return new Array(options.shardIndex)
.fill(true)
.reduce((acc, _, shardIndex) => {
const dangles = shardIndex < shardRest;
const shardSize = dangles ? Math.ceil(ratio) : Math.floor(ratio);
return acc + shardSize;
}, 0);
}

/**
* Select tests for shard requested via --shard=shardIndex/shardCount
* Sharding is applied before sorting
Expand All @@ -97,9 +110,17 @@ export default class TestSequencer {
tests: Array<Test>,
options: ShardOptions,
): Array<Test> | Promise<Array<Test>> {
const shardSize = Math.ceil(tests.length / options.shardCount);
const shardStart = shardSize * (options.shardIndex - 1);
const shardEnd = shardSize * options.shardIndex;
const shardStart = this._shardPosition({
shardCount: options.shardCount,
shardIndex: options.shardIndex - 1,
suiteLength: tests.length,
});

const shardEnd = this._shardPosition({
shardCount: options.shardCount,
shardIndex: options.shardIndex,
suiteLength: tests.length,
});

return tests
.map(test => {
Expand Down