Skip to content

Commit b3315f2

Browse files
ThomsonTanhdost
andauthored
Make the stress threads number configurable via command line argument (#1871)
Co-authored-by: Harold Dost <h.dost@criteo.com>
1 parent c1f351c commit b3315f2

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

stress/src/throughput.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use num_format::{Locale, ToFormattedString};
2+
use std::env;
23
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
34
use std::sync::Arc;
45
use std::thread;
@@ -28,7 +29,34 @@ where
2829
STOP.store(true, Ordering::SeqCst);
2930
})
3031
.expect("Error setting Ctrl-C handler");
31-
let num_threads = num_cpus::get();
32+
33+
let mut num_threads = num_cpus::get();
34+
let mut args_iter = env::args();
35+
36+
if let Some(arg_str) = args_iter.nth(1) {
37+
let arg = arg_str.parse::<usize>();
38+
39+
if !arg.is_ok() {
40+
eprintln!("Invalid command line argument '{}' as number of threads. Make sure the value is a positive integer.", arg_str);
41+
std::process::exit(1);
42+
}
43+
44+
let arg_num = arg.unwrap();
45+
46+
if arg_num > 0 {
47+
if arg_num > num_cpus::get() {
48+
println!(
49+
"Specified {} threads which is larger than the number of logical cores ({})!",
50+
arg_num, num_threads
51+
);
52+
}
53+
num_threads = arg_num as usize;
54+
} else {
55+
eprintln!("Invalid command line argument {} as number of threads. Make sure the value is above 0 and less than or equal to number of available logical cores ({}).", arg_num, num_threads);
56+
std::process::exit(1);
57+
}
58+
}
59+
3260
println!("Number of threads: {}\n", num_threads);
3361
let mut handles = Vec::with_capacity(num_threads);
3462
let func_arc = Arc::new(func);

0 commit comments

Comments
 (0)