Add --worker-threads flag to set the runtime thread count - #893
Add --worker-threads flag to set the runtime thread count#893ChrisJr404 wants to merge 2 commits into
Conversation
oha already honours the TOKIO_WORKER_THREADS environment variable when building the Tokio runtime, but there was no way to set it from the command line and it was not discoverable from --help. Add a --worker-threads <N> option exposing the same knob. The flag takes precedence over the environment variable; when neither is set the previous default (the number of physical CPU cores) is unchanged. Parse the CLI before building the runtime so the flag can feed the builder, and use NonZeroUsize so 0 and negative values are rejected by the parser instead of panicking inside Tokio. Closes hatoo#574.
|
Thank you! Line 2222 in a132389 Could you update to use |
The fast-mode workers build their own runtimes and spawned one OS thread per physical core regardless of --worker-threads. Thread the parsed option through fast::work / fast::work_until (HTTP/1+2 and HTTP/3 paths) so the flag controls their thread count too, falling back to the physical core count when unset.
|
Done - |
| Takes precedence over the TOKIO_WORKER_THREADS environment variable. When neither is set, the number of physical CPU cores is used.", | ||
| long = "worker-threads" | ||
| )] | ||
| pub worker_threads: Option<std::num::NonZeroUsize>, |
There was a problem hiding this comment.
clap can read env.
env = "TOKIO_WORKER_THREADS"
Maybe num_cpus::get_physical can be used as clap's default value.
So codes around worker_threads can be simpler.
| n_tasks: usize, | ||
| n_connections: usize, | ||
| n_http_parallel: usize, | ||
| worker_threads: Option<std::num::NonZeroUsize>, |
There was a problem hiding this comment.
Please remove Option and use num_workers_threads in main.rs for simplicity.
It changes existing behavior to read TOKIO_WORKER_THREADS in fast workers. But it's OK.
Add a
--worker-threadsflag to control the number of OS threads the async runtime uses, so the count can be set per invocation without having to export an environment variable.ohaalready honoursTOKIO_WORKER_THREADSwhen building the Tokio runtime, but there was no way to set it from the command line and it wasn't discoverable from--help. This adds a--worker-threads <N>option that surfaces the same knob. The flag takes precedence over the environment variable; when neither is set the previous default (the number of physical CPU cores) is unchanged, so existing behaviour is untouched.Because the runtime is built before the CLI is parsed,
mainnow parsesOptsfirst and then builds the runtime using the resolved value. The argument is aNonZeroUsize, so0and negative values are rejected by the parser with a clear error rather than panicking inside Tokio.Closes #574.
Testing
cargo test --test tests test_worker_threads_cli_flag— new test covering the default (unset), a valid value, and rejection of0/-1.cargo build,cargo clippy,cargo fmt --check.oha --worker-threads 2 -n 10 --no-tui http://127.0.0.1:PORT/runs, andoha --helplists the new option.