From d2303c5bd59feac5b6a3edae9342b81711530dc8 Mon Sep 17 00:00:00 2001 From: Paul Hawxby Date: Mon, 10 Dec 2018 14:38:26 +0000 Subject: [PATCH 1/5] Allow % based configuration of max workers --- CHANGELOG.md | 1 + docs/CLI.md | 4 +++- .../src/__tests__/getMaxWorkers.test.js | 17 +++++++++++++++++ packages/jest-config/src/getMaxWorkers.js | 15 ++++++++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69eea7572b77..438dd5ee2136 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-cli]` Allow % based configuration of `--max-workers` - `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363)) - `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)) diff --git a/docs/CLI.md b/docs/CLI.md index f19cf7579a1a..14db109610d4 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -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=` +### `--maxWorkers=|` 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 which could be unknown or changable you can use percentage based configuration, `--maxWorkers=50%` + ### `--noStackTrace` Disables stack trace in test results output. diff --git a/packages/jest-config/src/__tests__/getMaxWorkers.test.js b/packages/jest-config/src/__tests__/getMaxWorkers.test.js index 5b0d9fb26b95..0545ef8e6cc4 100644 --- a/packages/jest-config/src/__tests__/getMaxWorkers.test.js +++ b/packages/jest-config/src/__tests__/getMaxWorkers.test.js @@ -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); + }); + }); }); diff --git a/packages/jest-config/src/getMaxWorkers.js b/packages/jest-config/src/getMaxWorkers.js index 307ef02c9653..80deadc427d0 100644 --- a/packages/jest-config/src/getMaxWorkers.js +++ b/packages/jest-config/src/getMaxWorkers.js @@ -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); From 224298f5c17af532a2371a89b3f79937d51dd75a Mon Sep 17 00:00:00 2001 From: Paul Hawxby Date: Mon, 10 Dec 2018 14:42:29 +0000 Subject: [PATCH 2/5] Add pull ref --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 438dd5ee2136..7c11ff1685a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- `[jest-cli]` Allow % based configuration of `--max-workers` +- `[jest-cli]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494)) - `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363)) - `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)) From 12ca0cccf93a2756d1e820f07a9c512427d2a75f Mon Sep 17 00:00:00 2001 From: Paul Hawxby Date: Mon, 10 Dec 2018 15:07:25 +0000 Subject: [PATCH 3/5] Whoops, copy and paste --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c11ff1685a4..02984a7e5860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- `[jest-cli]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494)) +- `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494)) - `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363)) - `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)) From 2bf156436b9cc24a73468543e1b62d233d9534ba Mon Sep 17 00:00:00 2001 From: Rick Hanlon II Date: Tue, 11 Dec 2018 13:37:49 +0000 Subject: [PATCH 4/5] Update docs/CLI.md Co-Authored-By: phawxby --- docs/CLI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CLI.md b/docs/CLI.md index 14db109610d4..7ca495392a35 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -197,7 +197,7 @@ Logs the heap usage after every test. Useful to debug memory leaks. Use together 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 which could be unknown or changable you can use percentage based configuration, `--maxWorkers=50%` +For environments with variable CPUs available, you can use percentage based configuration: `--maxWorkers=50%` ### `--noStackTrace` From 70defd6b126fef798da972dfe80151ac93cdb665 Mon Sep 17 00:00:00 2001 From: Paul Hawxby Date: Tue, 11 Dec 2018 13:38:15 +0000 Subject: [PATCH 5/5] Move changlog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02984a7e5860..ffa3830e0bfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ ### Features -- `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494)) - `[jest-cli]` [**BREAKING**] Only set error process error codes when they are non-zero ([#7363](https://github.com/facebook/jest/pull/7363)) - `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)) @@ -36,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