Skip to main content

workload/
workload.rs

1//! Define a `Workload`, run it under multi-threaded stress, print results.
2//!
3//! ```text
4//! cargo run --example workload --release
5//! ```
6//!
7//! Demonstrates the `Workload` trait + `StressRun::execute` flow. The
8//! workload is intentionally trivial; replace `run_once` with the code
9//! under test to measure real throughput, contention, and per-thread CV.
10
11use dev_stress::{StressRun, Workload};
12
13#[derive(Clone)]
14struct TrivialAdd;
15
16impl Workload for TrivialAdd {
17    fn run_once(&self) {
18        let mut acc: u64 = 0;
19        for i in 0..100 {
20            acc = std::hint::black_box(acc.wrapping_add(i));
21        }
22        std::hint::black_box(acc);
23    }
24}
25
26fn main() {
27    let run = StressRun::new("trivial_add").iterations(100_000).threads(4);
28
29    println!(
30        "configured: {} iterations across {} threads",
31        run.iterations_planned(),
32        run.threads_planned()
33    );
34
35    let result = run.execute(&TrivialAdd);
36
37    println!("name:           {}", result.name);
38    println!("iterations:     {}", result.iterations);
39    println!("threads:        {}", result.threads);
40    println!("total_elapsed:  {:?}", result.total_elapsed);
41    println!("ops_per_sec:    {:.0}", result.ops_per_sec());
42    println!("thread_time_cv: {:.4}", result.thread_time_cv());
43}