Skip to content

Commit 8a3d62e

Browse files
committed
Make the stress threads number configurable via command line argument
1 parent 36ad46a commit 8a3d62e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

stress/src/throughput.rs

+16-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,21 @@ 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 args: Vec<String> = env::args().collect();
34+
35+
let mut num_threads = num_cpus::get();
36+
if args.len() == 2 {
37+
let arg = args[1].parse::<i32>().unwrap();
38+
39+
if arg > 0 && arg <= num_threads as i32 {
40+
num_threads = arg as usize;
41+
} else {
42+
eprintln!("Invalid command line argument {} as number of threads. Make sure the value is above 0 and less than or equal to {}.", arg, num_threads);
43+
std::process::exit(1);
44+
}
45+
}
46+
3247
println!("Number of threads: {}\n", num_threads);
3348
let mut handles = Vec::with_capacity(num_threads);
3449
let func_arc = Arc::new(func);

0 commit comments

Comments
 (0)