Added a runtime parameter to the CPU stress test to automatically limit how long it runs
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Gregory Ballantine 2022-08-17 15:28:11 -04:00
parent 360ef2f959
commit 132a0ee501
2 changed files with 25 additions and 19 deletions

View File

@ -39,7 +39,9 @@ enum CpuCommands {
// CPU stress test subcommand
#[clap(name = "stress", about = "Stress test the CPU with math!")]
StressTest {
#[clap(short = 't', long, default_value_t = 0, help = "Number of threads to use; defaults to CPU's max thread count.")]
#[clap(short = 'r', long, default_value_t = 5, help = "Length of time (in minutes) to run the stress test. Defaults to 5")]
runtime: u16,
#[clap(short = 't', long, default_value_t = 0, help = "Number of threads to use; defaults to CPU's max thread count. Defaults to 0 (automatic)")]
threads: usize,
},
}
@ -122,7 +124,7 @@ fn main() {
// map subcommands back to the main command
match &cli.command {
Commands::Cpu(args) => match &args.cpu_commands {
CpuCommands::StressTest { threads } => stress::cpu::cpu_stress_math(*threads),
CpuCommands::StressTest { runtime, threads } => stress::cpu::cpu_stress_math(*runtime, *threads),
},
Commands::Disk(args) => match &args.disk_commands {

View File

@ -1,7 +1,8 @@
use std::thread;
use std::{thread, time};
use std::process::exit;
use sysinfo::{System,SystemExt};
pub fn cpu_stress_math(threads: usize) {
pub fn cpu_stress_math(runtime: u16, threads: usize) {
// fetch system information
let mut sys = System::new_all();
sys.refresh_all();
@ -15,14 +16,17 @@ pub fn cpu_stress_math(threads: usize) {
println!("Using specified thread count of {}", num_threads);
}
for i in 1..num_threads {
println!("Spawning thread number {}", i);
for i in 0..num_threads {
println!("Spawning thread number {}", i + 1);
thread::spawn (|| {
worker();
});
}
println!("Using main as last thread");
worker();
println!("Sleeping main thread for the allotted runtime of {} minute(s).", runtime);
let duration = time::Duration::from_secs((runtime * 60).into());
thread::sleep(duration);
exit(0);
}
fn worker() {