Skip to main content

cpu_stress/
lib.rs

1//! Multi-algorithm CPU saturation workloads.
2
3mod branch_chaos;
4mod hash_loop;
5mod matrix;
6mod parallel;
7
8use anyhow::Result;
9use stress_core::{Intensity, run_timed};
10use std::time::Duration;
11use tracing::info;
12
13pub use parallel::CpuWorkload;
14
15/// Run all CPU stress algorithms for the given duration.
16pub fn run(
17    intensity: Intensity,
18    duration: Duration,
19    threads: usize,
20) -> Result<()> {
21    info!(
22        intensity = intensity.0,
23        ?duration,
24        threads,
25        "starting CPU stress"
26    );
27
28    let pool = rayon::ThreadPoolBuilder::new()
29        .num_threads(threads)
30        .build()?;
31
32    pool.install(|| {
33        run_timed(duration, |stop| {
34            if stop.should_stop() {
35                return;
36            }
37            parallel::dispatch_round(intensity, stop);
38        });
39    });
40
41    Ok(())
42}