Skip to content

Add --worker-threads flag to set the runtime thread count - #893

Open
ChrisJr404 wants to merge 2 commits into
hatoo:masterfrom
ChrisJr404:worker-threads-flag
Open

Add --worker-threads flag to set the runtime thread count#893
ChrisJr404 wants to merge 2 commits into
hatoo:masterfrom
ChrisJr404:worker-threads-flag

Conversation

@ChrisJr404

Copy link
Copy Markdown

Add a --worker-threads flag 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.

oha already honours TOKIO_WORKER_THREADS when 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, main now parses Opts first and then builds the runtime using the resolved value. The argument is a NonZeroUsize, so 0 and 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 of 0/-1.
  • cargo build, cargo clippy, cargo fmt --check.
  • Verified manually: oha --worker-threads 2 -n 10 --no-tui http://127.0.0.1:PORT/ runs, and oha --help lists the new option.

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.
@hatoo

hatoo commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Thank you!
Since It's good timing, I think --worker-threads should also apply to "fast" mode workers.
One of them is located at

oha/src/client.rs

Line 2222 in a132389

let num_threads = num_cpus::get_physical();

Could you update to use --worker-threads in fast workers?

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.
@ChrisJr404

Copy link
Copy Markdown
Author

Done - --worker-threads now also controls the fast-mode workers. I threaded the parsed option through fast::work / fast::work_until (both the HTTP/1+2 path in client.rs and the HTTP/3 path in client_h3.rs) so the number of worker threads they spawn honors the flag, falling back to the physical core count when it's unset. Same knob, no behavior change when the flag isn't passed.

@hatoo hatoo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some reviews

Comment thread src/lib.rs
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>,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/client.rs
n_tasks: usize,
n_connections: usize,
n_http_parallel: usize,
worker_threads: Option<std::num::NonZeroUsize>,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add an option to configure the number of worker threads used by tokio

2 participants