8000 [rush] Move parallelism parsing to helper · mstomar698/rushstack@76f5697 · GitHub
[go: up one dir, main page]

Skip to content

Commit 76f5697

Browse files
committed
[rush] Move parallelism parsing to helper
1 parent a142f46 commit 76f5697

11 files changed

+218
-205
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "Move parsing of \"--parallelism\" out of execution logic.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
10+
}

libraries/rush-lib/src/cli/actions/BaseInstallAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Stopwatch } from '../../utilities/Stopwatch';
2121
import { VersionMismatchFinder } from '../../logic/versionMismatch/VersionMismatchFinder';
2222
import { Variants } from '../../api/Variants';
2323
import { RushConstants } from '../../logic/RushConstants';
24-
import { SelectionParameterSet } from '../SelectionParameterSet';
24+
import { SelectionParameterSet } from '../parsing/SelectionParameterSet';
2525

2626
const installManagerFactoryModule: typeof import('../../logic/InstallManagerFactory') = Import.lazy(
2727
'../../logic/InstallManagerFactory',

libraries/rush-lib/src/cli/actions/InstallAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ConsoleTerminalProvider, Terminal } from '@rushstack/node-core-library'
77
import { BaseInstallAction } from './BaseInstallAction';
88
import { IInstallManagerOptions } from '../../logic/base/BaseInstallManager';
99
import { RushCommandLineParser } from '../RushCommandLineParser';
10-
import { SelectionParameterSet } from '../SelectionParameterSet';
10+
import { SelectionParameterSet } from '../parsing/SelectionParameterSet';
1111

1212
export class InstallAction extends BaseInstallAction {
1313
private _checkOnlyParameter!: CommandLineFlagParameter;

libraries/rush-lib/src/cli/actions/ListAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { BaseRushAction } from './BaseRushAction';
88
import { RushCommandLineParser } from '../RushCommandLineParser';
99
import { RushConfigurationProject } from '../../api/RushConfigurationProject';
1010
import { VersionPolicyDefinitionName } from '../../api/VersionPolicy';
11-
import { SelectionParameterSet } from '../SelectionParameterSet';
11+
import { SelectionParameterSet } from '../parsing/SelectionParameterSet';
1212

1313
/**
1414
* Shape of "rush list --json" output.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import * as os from 'os';
5+
6+
/**
7+
* Parses a command line specification for desired parallelism.
8+
* Factored out to enable unit tests
9+
*/
10+
export function parseParallelism(
11+
rawParallelism: string | undefined,
12+
numberOfCores: number = os.cpus().length
13+
): number {
14+
if (rawParallelism) {
15+
if (rawParallelism === 'max') {
16+
return numberOfCores;
17+
} else {
18+
const parallelismAsNumber: number = Number(rawParallelism);
19+
20+
if (typeof rawParallelism === 'string' && rawParallelism.trim().endsWith('%')) {
21+
const parsedPercentage: number = Number(rawParallelism.trim().replace(/\%$/, ''));
22+
23+
if (parsedPercentage <= 0 || parsedPercentage > 100) {
24+
throw new Error(
25+
`Invalid percentage value of '${rawParallelism}', value cannot be less than '0%' or more than '100%'`
26+
);
27+
}
28+
29+
const workers: number = Math.floor((parsedPercentage / 100) * numberOfCores);
30+
return Math.max(workers, 1);
31+
} else if (!isNaN(parallelismAsNumber)) {
32+
return Math.max(parallelismAsNumber, 1);
33+
} else {
34+
throw new Error(
35+
`Invalid parallelism value of '${rawParallelism}', expected a number, a percentage, or 'max'`
36+
);
37+
}
38+
}
39+
} else {
40+
// If an explicit parallelism number wasn't provided, then choose a sensible
41+
// default.
42+
if (os.platform() === 'win32') {
43+
// On desktop Windows, some people have complained that their system becomes
44+
// sluggish if Rush is using all the CPU cores. Leave one thread for
45+
// other operations. For CI environments, you can use the "max" argument to use all available cores.
46+
return Math.max(numberOfCores - 1, 1);
47+
} else {
48+
// Unix-like operating systems have more balanced scheduling, so default
49+
// to the number of CPU cores
50+
return numberOfCores;
51+
}
52+
}
53+
}

libraries/rush-lib/src/cli/SelectionParameterSet.ts renamed to libraries/rush-lib/src/cli/parsing/SelectionParameterSet.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import {
99
} from '@rushstack/node-core-library';
1010
import { CommandLineParameterProvider, CommandLineStringListParameter } from '@rushstack/ts-command-line';
1111

12-
import { RushConfiguration } from '../api/RushConfiguration';
13-
import { RushConfigurationProject } from '../api/RushConfigurationProject';
14-
import { Selection } from '../logic/Selection';
15-
import type { ISelectorParser as ISelectorParser } from '../logic/selectors/ISelectorParser';
12+
import { RushConfiguration } from '../../api/RushConfiguration';
13+
import { RushConfigurationProject } from '../../api/RushConfigurationProject';
14+
import { Selection } from '../../logic/Selection';
15+
import type { ISelectorParser as ISelectorParser } from '../../logic/selectors/ISelectorParser';
1616
import {
1717
GitChangedProjectSelectorParser,
1818
IGitSelectorParserOptions
19-
} from '../logic/selectors/GitChangedProjectSelectorParser';
20-
import { NamedProjectSelectorParser } from '../logic/selectors/NamedProjectSelectorParser';
21-
import { TagProjectSelectorParser } from '../logic/selectors/TagProjectSelectorParser';
22-
import { VersionPolicyProjectSelectorParser } from '../logic/selectors/VersionPolicyProjectSelectorParser';
19+
} from '../../logic/selectors/GitChangedProjectSelectorParser';
20+
import { NamedProjectSelectorParser } from '../../logic/selectors/NamedProjectSelectorParser';
21+
import { TagProjectSelectorParser } from '../../logic/selectors/TagProjectSelectorParser';
22+
import { VersionPolicyProjectSelectorParser } from '../../logic/selectors/VersionPolicyProjectSelectorParser';
2323

2424
/**
2525
* This class is provides the set of command line parameters used to select projects
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { parseParallelism } from '../ParseParallelism';
5+
6+
describe(parseParallelism.name, () => {
7+
it('throwsErrorOnInvalidParallelism', () => {
8+
expect(() => parseParallelism('tequila')).toThrowErrorMatchingSnapshot();
9+
});
10+
11+
it('createsWithPercentageBasedParallelism', () => {
12+
const value: number = parseParallelism('50%', 20);
13+
expect(value).toEqual(10);
14+
});
15+
16+
it('throwsErrorOnInvalidParallelismPercentage', () => {
17+
expect(() => parseParallelism('200%')).toThrowErrorMatchingSnapshot();
18+
});
19+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`parseParallelism throwsErrorOnInvalidParallelism 1`] = `"Invalid parallelism value of 'tequila', expected a number, a percentage, or 'max'"`;
4+
5+
exports[`parseParallelism throwsErrorOnInvalidParallelismPercentage 1`] = `"Invalid percentage value of '200%', value cannot be less than '0%' or more than '100%'"`;

0 commit comments

Comments
 (0)
0