Repository navigation
Commit 6011949
authored
[CLI] Add --workers=<n|auto> flag to configure worker thread count (#3504)
## Motivation for the change, related issues
The Playground CLI hardcodes the number of request-handling worker
threads to 6 (chosen to match HTTP/1.1's per-origin browser connection
limit). That's fine for a single browser, but it caps multi-client
workloads — parallel Playwright contexts, load tests, fanning e2e suites
— at six in-flight requests, which is too few on modern hosts.
The old `--experimental-multi-worker` flag that let users override this
was deprecated and its value is now silently ignored, and the
pre-hardcoding default of `cpus - 1` was dropped along with it. There's
no way to turn the dial back up.
## Implementation details
Adds a new `--workers=<n|auto>` flag:
- A positive integer sets an explicit worker count.
- `auto` uses `max(1, os.cpus().length - 1)` — restores the old
pre-hardcoding default.
- When the flag is absent, the default is `min(6, max(1, cpus - 1))`:
preserves today's 6-worker behavior on machines with ≥7 cores, shrinks
on small hosts so we don't spawn more workers than cores. The
`Math.max(1, ...)` guard covers single-core hosts and restricted
environments where `os.cpus()` returns an empty array.
- Validation happens in yargs `coerce`: rejects 0, negatives,
non-integers, and any non-`auto` string with a clear error message,
routed through the existing `.fail()` handler.
The resolver is a small exported pure function (`resolveWorkerCount`) so
the min/max/cpu logic is unit-testable without spinning up a server.
`--experimental-multi-worker` stays parsed-but-ignored — existing
invocations don't break — but now emits a one-line `logger.warn`
pointing users at `--workers`.
Docs updated in both `packages/playground/cli/README.md` and the
Docusaurus CLI reference page.
### Breaking changes
None. The default worker count is unchanged on typical dev machines (≥7
cores → still 6). On hosts with 1–6 cores the default drops to `cpus -
1`, which is a behavior change on small hosts but strictly safer (we
were previously spawning more workers than cores there).
## Testing Instructions (or ideally a Blueprint)
**Unit tests** (fast, no server spawn):
```bash
npx nx run playground-cli:test-playground-cli --testNamePattern="resolveWorkerCount|worker count"
```
All 14 new tests should pass: default, explicit number, `--workers=1`
(single-worker bootstrap), `--workers=auto`, invalid `0`, invalid `abc`,
deprecation warning for `--experimental-multi-worker`, plus 7 unit tests
stubbing `os.cpus()` to cover the min/max/empty-array edges.
**Manual smoke**:
```bash
# Explicit count — server prints "(12 workers)"
npx nx dev playground-cli server --workers=12
# auto — uses cpus-1
npx nx dev playground-cli server --workers=auto
# default — min(6, cpus-1)
npx nx dev playground-cli server
# Deprecated flag still works, now warns
npx nx dev playground-cli server --experimental-multi-worker=4
# Validation
npx nx dev playground-cli server --workers=0 # exits 1 with clear error
npx nx dev playground-cli server --workers=abc # exits 1 with clear error
```
**Multi-client verification** (the motivating use case):
```bash
# Start with more than 6 workers
npx nx dev playground-cli server --workers=16 &
# Fire >6 concurrent requests; throughput should scale past the old 6-worker ceiling
seq 1 32 | xargs -P32 -I{} curl -s -o /dev/null -w "%{time_total}\n" http://127.0.0.1:9400/
```1 parent a86fac8 commit 6011949
4 files changed
Lines changed: 318 additions & 14 deletions
File tree
- packages
- docs/site/docs/developers/05-local-development
- playground/cli
- src
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
262 | 262 | | |
263 | 263 | | |
264 | 264 | | |
265 | | - | |
| 265 | + | |
| 266 | + | |
266 | 267 | | |
267 | 268 | | |
268 | 269 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
| 100 | + | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
346 | 369 | | |
347 | 370 | | |
348 | | - | |
| 371 | + | |
349 | 372 | | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
| 373 | + | |
| 374 | + | |
354 | 375 | | |
355 | 376 | | |
356 | 377 | | |
| |||
813 | 834 | | |
814 | 835 | | |
815 | 836 | | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
816 | 855 | | |
817 | 856 | | |
818 | 857 | | |
| |||
854 | 893 | | |
855 | 894 | | |
856 | 895 | | |
| 896 | + | |
| 897 | + | |
857 | 898 | | |
858 | 899 | | |
859 | 900 | | |
| |||
1082 | 1123 | | |
1083 | 1124 | | |
1084 | 1125 | | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
1085 | 1133 | | |
1086 | 1134 | | |
1087 | 1135 | | |
1088 | 1136 | | |
1089 | 1137 | | |
1090 | 1138 | | |
1091 | 1139 | | |
1092 | | - | |
1093 | | - | |
1094 | | - | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
1095 | 1143 | | |
1096 | | - | |
1097 | | - | |
1098 | | - | |
| 1144 | + | |
| 1145 | + | |
1099 | 1146 | | |
1100 | | - | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
1101 | 1172 | | |
1102 | 1173 | | |
1103 | 1174 | | |
| |||
0 commit comments