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

Allow % based configuration of max workers #7494

Merged
merged 6 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- `[expect]` `expect(Infinity).toBeCloseTo(Infinity)` Treats `Infinity` as equal in toBeCloseTo matcher ([#7405](https://github.com/facebook/jest/pull/7405))
- `[jest-worker]` Add node worker-thread support to jest-worker ([#7408](https://github.com/facebook/jest/pull/7408))
- `[jest-config]` Allow `bail` setting to be configured with a number allowing tests to abort after `n` of failures ([#7335](https://github.com/facebook/jest/pull/7335))
- `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494))

### Fixes

Expand Down
4 changes: 3 additions & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ Lists all tests as JSON that Jest will run given the arguments, and exits. This

Logs the heap usage after every test. Useful to debug memory leaks. Use together with `--runInBand` and `--expose-gc` in node.

### `--maxWorkers=<num>`
### `--maxWorkers=<num>|<string>`

Alias: `-w`. Specifies the maximum number of workers the worker-pool will spawn for running tests. This defaults to the number of the cores available on your machine. It may be useful to adjust this in resource limited environments like CIs but the default should be adequate for most use-cases.

For environments with variable CPUs available, you can use percentage based configuration: `--maxWorkers=50%`

### `--noStackTrace`

Disables stack trace in test results output.
Expand Down
17 changes: 17 additions & 0 deletions packages/jest-config/src/__tests__/getMaxWorkers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ describe('getMaxWorkers', () => {
expect(getMaxWorkers({})).toBe(3);
expect(getMaxWorkers({watch: true})).toBe(2);
});

describe('% based', () => {
it('50% = 2 workers', () => {
const argv = {maxWorkers: '50%'};
expect(getMaxWorkers(argv)).toBe(2);
});

it('< 0 workers should become 1', () => {
const argv = {maxWorkers: '1%'};
expect(getMaxWorkers(argv)).toBe(1);
});

it("0% shouldn't break", () => {
const argv = {maxWorkers: '0%'};
expect(getMaxWorkers(argv)).toBe(1);
});
});
});
15 changes: 14 additions & 1 deletion packages/jest-config/src/getMaxWorkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ export default function getMaxWorkers(argv: Argv): number {
if (argv.runInBand) {
return 1;
} else if (argv.maxWorkers) {
return parseInt(argv.maxWorkers, 10);
const parsed = parseInt(argv.maxWorkers, 10);

if (
typeof argv.maxWorkers === 'string' &&
argv.maxWorkers.trim().endsWith('%') &&
parsed > 0 &&
parsed <= 100
) {
const cpus = os.cpus().length;
const workers = Math.floor((parsed / 100) * cpus);
return workers >= 1 ? workers : 1;
}

return parsed > 0 ? parsed : 1;
} else {
const cpus = os.cpus() ? os.cpus().length : 1;
return Math.max(argv.watch ? Math.floor(cpus / 2) : cpus - 1, 1);
Expand Down