dev-stress 0.9.2

High-load stress testing for Rust. Concurrency, volume, saturation. Part of the dev-* verification suite.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! # dev-stress
//!
//! High-load stress testing for Rust. Concurrency, volume, saturation
//! under pressure. Part of the `dev-*` verification suite.
//!
//! `dev-stress` is the answer to "does this code survive real load?"
//! Not "is it fast" (that's `dev-bench`). Not "does it deadlock"
//! (that's `dev-async`). Not "does it recover from failure" (that's
//! `dev-chaos`).
//!
//! ## Quick example
//!
//! ```no_run
//! use dev_stress::{Workload, StressRun};
//!
//! #[derive(Clone)]
//! struct MyWorkload;
//! impl Workload for MyWorkload {
//!     fn run_once(&self) {
//!         std::hint::black_box(40 + 2);
//!     }
//! }
//!
//! let run = StressRun::new("hot_path")
//!     .iterations(100_000)
//!     .threads(8);
//!
//! let result = run.execute(&MyWorkload);
//! let _check = result.into_check_result(None);
//! ```
//!
//! ## What's measured
//!
//! - **`ops_per_sec`** — total iterations divided by total wall time.
//! - **`thread_time_cv`** — coefficient of variation across per-thread
//!   elapsed times. High CV indicates load imbalance or contention.
//! - **`latency_p50/p95/p99`** — per-operation latency percentiles
//!   (when [`LatencyTracker`] is enabled).
//!
//! ## Features
//!
//! - `system-stats` (opt-in): measure peak RSS and CPU time via
//!   `sysinfo`. See the `system` module
//!   (visible in rustdoc when the feature is enabled).

#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]

use std::sync::Arc;
use std::time::{Duration, Instant};

use dev_report::{CheckResult, Evidence, Producer, Report, Severity};

pub mod latency;
pub mod soak;

#[cfg(feature = "system-stats")]
#[cfg_attr(docsrs, doc(cfg(feature = "system-stats")))]
pub mod system;

pub use latency::{LatencyStats, LatencyTracker};
pub use soak::{SoakCheckpoint, SoakResult, SoakRun};

/// A workload that can be executed many times under stress.
///
/// Implementations MUST be safe to call concurrently from multiple
/// threads (`Send + Sync`) and MUST be cheap to clone, since each
/// thread receives an `Arc<Self>`.
///
/// # Example
///
/// ```
/// use dev_stress::Workload;
///
/// #[derive(Clone)]
/// struct Noop;
/// impl Workload for Noop {
///     fn run_once(&self) {
///         std::hint::black_box(1 + 1);
///     }
/// }
/// ```
pub trait Workload: Send + Sync {
    /// Execute one unit of work. MUST be safe to call concurrently
    /// from multiple threads.
    fn run_once(&self);
}

/// Configuration for a stress run.
///
/// # Example
///
/// ```
/// use dev_stress::StressRun;
///
/// let run = StressRun::new("hot_path").iterations(1_000).threads(4);
/// assert_eq!(run.iterations_planned(), 1_000);
/// assert_eq!(run.threads_planned(), 4);
/// ```
pub struct StressRun {
    name: String,
    iterations: usize,
    threads: usize,
    track_latency: Option<usize>, // None = off; Some(n) = sample 1/n iterations
    /// Per-thread target ops/sec rate. None = unbounded (run as fast as possible).
    target_ops_per_sec_per_thread: Option<f64>,
}

impl StressRun {
    /// Begin building a stress run with a stable name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            iterations: 1_000,
            threads: 1,
            track_latency: None,
            target_ops_per_sec_per_thread: None,
        }
    }

    /// Total iterations across all threads.
    pub fn iterations(mut self, n: usize) -> Self {
        self.iterations = n;
        self
    }

    /// Number of OS threads to run concurrently. Minimum is `1`.
    pub fn threads(mut self, n: usize) -> Self {
        self.threads = n.max(1);
        self
    }

    /// Track per-operation latency, sampling 1 of every `rate` iterations.
    ///
    /// `rate = 1` records every iteration; `rate = 100` records 1% of
    /// iterations. Lower rates lower memory and overhead at the cost
    /// of percentile precision.
    ///
    /// # Example
    ///
    /// ```
    /// use dev_stress::StressRun;
    ///
    /// let run = StressRun::new("hot").iterations(10_000).threads(2)
    ///     .track_latency(10); // 10% sample rate
    /// assert_eq!(run.iterations_planned(), 10_000);
    /// ```
    pub fn track_latency(mut self, rate: usize) -> Self {
        self.track_latency = Some(rate.max(1));
        self
    }

    /// Cap the workload at approximately `total_rate` ops per second
    /// (across all threads).
    ///
    /// Implemented as a per-thread sleep based on observed elapsed time
    /// vs. ideal scheduling. Sleep precision varies by OS; for very
    /// high target rates (e.g. > 10k ops/sec/thread) the actual rate
    /// may be lower than the target due to sleep granularity.
    ///
    /// # Example
    ///
    /// ```
    /// use dev_stress::StressRun;
    ///
    /// // Cap to ~1000 ops/sec total across 4 threads = 250 per thread.
    /// let run = StressRun::new("rate-limited")
    ///     .iterations(500)
    ///     .threads(4)
    ///     .target_ops_per_sec(1000.0);
    /// assert_eq!(run.threads_planned(), 4);
    /// ```
    pub fn target_ops_per_sec(mut self, total_rate: f64) -> Self {
        if total_rate <= 0.0 {
            self.target_ops_per_sec_per_thread = None;
        } else {
            let per_thread = total_rate / (self.threads.max(1) as f64);
            self.target_ops_per_sec_per_thread = Some(per_thread);
        }
        self
    }

    /// The configured iteration count.
    pub fn iterations_planned(&self) -> usize {
        self.iterations
    }

    /// The configured thread count.
    pub fn threads_planned(&self) -> usize {
        self.threads
    }

    /// The configured per-thread target rate, if any.
    pub fn target_ops_per_sec_per_thread(&self) -> Option<f64> {
        self.target_ops_per_sec_per_thread
    }

    /// Execute the run. Returns a result with timing statistics.
    pub fn execute<W>(&self, workload: &W) -> StressResult
    where
        W: Workload + Clone + 'static,
    {
        let per_thread = self.iterations / self.threads;
        let leftover = self.iterations % self.threads;
        let started = Instant::now();
        let mut handles = Vec::with_capacity(self.threads);
        let workload = Arc::new(workload.clone());

        for t in 0..self.threads {
            let count = per_thread + if t < leftover { 1 } else { 0 };
            let w = workload.clone();
            let track = self.track_latency;
            let target_rate = self.target_ops_per_sec_per_thread;
            handles.push(std::thread::spawn(move || {
                let start = Instant::now();
                let mut tracker = track.map(LatencyTracker::new);
                // Inverse of the per-thread rate, in seconds per op.
                let interval_s = target_rate.map(|r| 1.0 / r);
                for i in 0..count {
                    if let (Some(interval), idx) = (interval_s, i) {
                        let target = start + Duration::from_secs_f64(interval * idx as f64);
                        let now = Instant::now();
                        if target > now {
                            std::thread::sleep(target - now);
                        }
                    }
                    if let Some(t) = tracker.as_mut() {
                        t.record(i, || w.run_once());
                    } else {
                        w.run_once();
                    }
                }
                (start.elapsed(), tracker)
            }));
        }

        let mut thread_times = Vec::with_capacity(self.threads);
        let mut latency_samples: Vec<Duration> = Vec::new();
        for h in handles {
            let (elapsed, tracker) = h.join().unwrap();
            thread_times.push(elapsed);
            if let Some(t) = tracker {
                latency_samples.extend(t.into_samples());
            }
        }
        let total_elapsed = started.elapsed();

        StressResult {
            name: self.name.clone(),
            iterations: self.iterations,
            threads: self.threads,
            total_elapsed,
            thread_times,
            latency: if latency_samples.is_empty() {
                None
            } else {
                Some(LatencyStats::from_samples(latency_samples))
            },
        }
    }
}

/// Result of a stress run.
///
/// # Example
///
/// ```no_run
/// use dev_stress::{StressRun, Workload};
///
/// #[derive(Clone)]
/// struct Noop;
/// impl Workload for Noop {
///     fn run_once(&self) { std::hint::black_box(1 + 1); }
/// }
///
/// let r = StressRun::new("noop").iterations(100).threads(2).execute(&Noop);
/// assert!(r.ops_per_sec() > 0.0);
/// ```
#[derive(Debug, Clone)]
pub struct StressResult {
    /// Stable name of the run.
    pub name: String,
    /// Iterations actually executed.
    pub iterations: usize,
    /// Threads used.
    pub threads: usize,
    /// Wall-clock time from run start to all threads finishing.
    pub total_elapsed: Duration,
    /// Per-thread elapsed times. Variance here indicates contention.
    pub thread_times: Vec<Duration>,
    /// Per-operation latency percentiles, when [`StressRun::track_latency`]
    /// was enabled. `None` otherwise.
    pub latency: Option<LatencyStats>,
}

impl StressResult {
    /// Effective throughput in operations per second.
    pub fn ops_per_sec(&self) -> f64 {
        if self.total_elapsed.is_zero() {
            return 0.0;
        }
        self.iterations as f64 / self.total_elapsed.as_secs_f64()
    }

    /// Coefficient of variation across thread times. Higher numbers
    /// indicate worse contention or load imbalance.
    pub fn thread_time_cv(&self) -> f64 {
        if self.thread_times.is_empty() {
            return 0.0;
        }
        let n = self.thread_times.len() as f64;
        let mean: f64 = self
            .thread_times
            .iter()
            .map(|d| d.as_secs_f64())
            .sum::<f64>()
            / n;
        if mean == 0.0 {
            return 0.0;
        }
        let var = self
            .thread_times
            .iter()
            .map(|d| (d.as_secs_f64() - mean).powi(2))
            .sum::<f64>()
            / n;
        var.sqrt() / mean
    }

    /// Convert this result into a `CheckResult` using the legacy
    /// behavior (90%-baseline ops/sec floor, no latency thresholds).
    ///
    /// `baseline_ops_per_sec` is the previously-recorded throughput.
    /// `None` -> `Pass` with detail. Below 90% baseline -> `Fail+Warning`.
    /// Otherwise `Pass`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use dev_stress::{StressRun, Workload};
    ///
    /// #[derive(Clone)]
    /// struct Noop;
    /// impl Workload for Noop { fn run_once(&self) {} }
    ///
    /// let r = StressRun::new("noop").iterations(100).threads(1).execute(&Noop);
    /// let _check = r.into_check_result(None);
    /// ```
    pub fn into_check_result(self, baseline_ops_per_sec: Option<f64>) -> CheckResult {
        self.compare_with_options(&CompareOptions {
            baseline_ops_per_sec,
            ..CompareOptions::default()
        })
    }

    /// Compare this result against a baseline using full options.
    ///
    /// Always returns a `CheckResult` tagged `stress`, with numeric
    /// `Evidence` for `iterations`, `threads`, `ops_per_sec`,
    /// `thread_time_cv`, `total_elapsed_ms`, plus latency percentiles
    /// (when tracked) and any baseline values provided.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use dev_stress::{CompareOptions, StressRun, Workload};
    /// use std::time::Duration;
    ///
    /// #[derive(Clone)]
    /// struct Noop;
    /// impl Workload for Noop { fn run_once(&self) {} }
    ///
    /// let r = StressRun::new("noop").iterations(100).threads(1).execute(&Noop);
    /// let opts = CompareOptions {
    ///     baseline_ops_per_sec: Some(1_000_000.0),
    ///     ops_drop_pct_threshold: 10.0,
    ///     baseline_p99: None,
    ///     p99_regression_pct_threshold: 20.0,
    /// };
    /// let _ = r.compare_with_options(&opts);
    /// ```
    pub fn compare_with_options(&self, opts: &CompareOptions) -> CheckResult {
        let name = format!("stress::{}", self.name);
        let mut evidence = self.numeric_evidence();
        let mut tags = vec!["stress".to_string()];

        let mut regressions: Vec<String> = Vec::new();

        // ops/sec drop check.
        if let Some(baseline_ops) = opts.baseline_ops_per_sec {
            evidence.push(Evidence::numeric("baseline_ops_per_sec", baseline_ops));
            let floor = baseline_ops * (1.0 - opts.ops_drop_pct_threshold / 100.0);
            if self.ops_per_sec() < floor {
                regressions.push(format!(
                    "ops_per_sec {:.0} < floor {:.0} ({}% drop allowed)",
                    self.ops_per_sec(),
                    floor,
                    opts.ops_drop_pct_threshold
                ));
            }
        }

        // p99 regression check.
        if let (Some(baseline_p99), Some(latency)) = (opts.baseline_p99, &self.latency) {
            evidence.push(Evidence::numeric(
                "baseline_p99_ns",
                baseline_p99.as_nanos() as f64,
            ));
            let allowed =
                baseline_p99.as_nanos() as f64 * (1.0 + opts.p99_regression_pct_threshold / 100.0);
            if (latency.p99.as_nanos() as f64) > allowed {
                regressions.push(format!(
                    "p99_ns {} > allowed {:.0} ({}% regression allowed)",
                    latency.p99.as_nanos(),
                    allowed,
                    opts.p99_regression_pct_threshold
                ));
            }
        }

        let detail = self.detail_string();

        if regressions.is_empty() {
            // No baseline OR within thresholds.
            let mut c = CheckResult::pass(name).with_detail(detail);
            c.tags = tags;
            c.evidence = evidence;
            return c;
        }

        tags.push("regression".to_string());
        let combined_detail = format!("{} :: {}", detail, regressions.join("; "));
        let mut c = CheckResult::fail(name, Severity::Warning).with_detail(combined_detail);
        c.tags = tags;
        c.evidence = evidence;
        c
    }

    /// Build a one-check `Report` containing the comparison result.
    ///
    /// Sets `subject = self.name`, `producer = "dev-stress"`.
    pub fn into_report(self, subject_version: impl Into<String>, opts: &CompareOptions) -> Report {
        let name = self.name.clone();
        let check = self.compare_with_options(opts);
        let mut r = Report::new(name, subject_version).with_producer("dev-stress");
        r.push(check);
        r.finish();
        r
    }

    fn numeric_evidence(&self) -> Vec<Evidence> {
        let mut e = vec![
            Evidence::numeric("iterations", self.iterations as f64),
            Evidence::numeric("threads", self.threads as f64),
            Evidence::numeric("ops_per_sec", self.ops_per_sec()),
            Evidence::numeric("thread_time_cv", self.thread_time_cv()),
            Evidence::numeric(
                "total_elapsed_ms",
                self.total_elapsed.as_secs_f64() * 1000.0,
            ),
        ];
        if let Some(lat) = &self.latency {
            e.push(Evidence::numeric(
                "latency_p50_ns",
                lat.p50.as_nanos() as f64,
            ));
            e.push(Evidence::numeric(
                "latency_p95_ns",
                lat.p95.as_nanos() as f64,
            ));
            e.push(Evidence::numeric(
                "latency_p99_ns",
                lat.p99.as_nanos() as f64,
            ));
            e.push(Evidence::numeric(
                "latency_samples",
                lat.samples_count as f64,
            ));
        }
        e
    }

    fn detail_string(&self) -> String {
        let lat = match &self.latency {
            Some(l) => format!(
                ", p50={}ns, p95={}ns, p99={}ns",
                l.p50.as_nanos(),
                l.p95.as_nanos(),
                l.p99.as_nanos()
            ),
            None => String::new(),
        };
        format!(
            "iterations={}, threads={}, total={:.3}s, ops/sec={:.0}, thread_cv={:.3}{}",
            self.iterations,
            self.threads,
            self.total_elapsed.as_secs_f64(),
            self.ops_per_sec(),
            self.thread_time_cv(),
            lat
        )
    }
}

/// Options controlling how a [`StressResult`] is compared against a baseline.
///
/// Defaults: no baseline; ops/sec drop threshold 10%; p99 regression threshold 20%.
///
/// # Example
///
/// ```
/// use dev_stress::CompareOptions;
///
/// let opts = CompareOptions {
///     baseline_ops_per_sec: Some(900_000.0),
///     ops_drop_pct_threshold: 5.0,
///     baseline_p99: None,
///     p99_regression_pct_threshold: 25.0,
/// };
/// assert_eq!(opts.ops_drop_pct_threshold, 5.0);
/// ```
#[derive(Debug, Clone)]
pub struct CompareOptions {
    /// Baseline throughput (ops/sec). When `Some`, the run fails if
    /// `ops_per_sec < baseline * (1 - ops_drop_pct_threshold/100)`.
    pub baseline_ops_per_sec: Option<f64>,
    /// Maximum allowed drop, as a percent.
    pub ops_drop_pct_threshold: f64,
    /// Baseline p99 latency. When `Some` AND latency was tracked, the
    /// run fails if `p99 > baseline_p99 * (1 + p99_regression_pct_threshold/100)`.
    pub baseline_p99: Option<Duration>,
    /// Maximum allowed p99 regression, as a percent.
    pub p99_regression_pct_threshold: f64,
}

impl Default for CompareOptions {
    fn default() -> Self {
        Self {
            baseline_ops_per_sec: None,
            ops_drop_pct_threshold: 10.0,
            baseline_p99: None,
            p99_regression_pct_threshold: 20.0,
        }
    }
}

/// Producer wrapper that runs a stress run and emits a single-check `Report`.
///
/// # Example
///
/// ```no_run
/// use dev_stress::{CompareOptions, StressProducer, StressRun, Workload};
/// use dev_report::Producer;
///
/// #[derive(Clone)]
/// struct Noop;
/// impl Workload for Noop { fn run_once(&self) {} }
///
/// let producer = StressProducer::new(
///     || StressRun::new("hot").iterations(1_000).threads(2).execute(&Noop),
///     "0.1.0",
///     CompareOptions::default(),
/// );
/// let report = producer.produce();
/// assert_eq!(report.checks.len(), 1);
/// ```
pub struct StressProducer<F>
where
    F: Fn() -> StressResult,
{
    run: F,
    subject_version: String,
    opts: CompareOptions,
}

impl<F> StressProducer<F>
where
    F: Fn() -> StressResult,
{
    /// Build a new producer.
    pub fn new(run: F, subject_version: impl Into<String>, opts: CompareOptions) -> Self {
        Self {
            run,
            subject_version: subject_version.into(),
            opts,
        }
    }
}

impl<F> Producer for StressProducer<F>
where
    F: Fn() -> StressResult,
{
    fn produce(&self) -> Report {
        let result = (self.run)();
        result.into_report(self.subject_version.clone(), &self.opts)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use dev_report::Verdict;

    #[derive(Clone)]
    struct Noop;
    impl Workload for Noop {
        fn run_once(&self) {
            std::hint::black_box(1 + 1);
        }
    }

    #[test]
    fn run_completes() {
        let run = StressRun::new("noop").iterations(1_000).threads(2);
        let r = run.execute(&Noop);
        assert_eq!(r.iterations, 1_000);
        assert_eq!(r.threads, 2);
        assert!(r.ops_per_sec() > 0.0);
    }

    #[test]
    fn no_baseline_passes() {
        let run = StressRun::new("noop").iterations(100).threads(1);
        let r = run.execute(&Noop);
        let c = r.into_check_result(None);
        assert_eq!(c.verdict, Verdict::Pass);
        assert!(c.has_tag("stress"));
    }

    #[test]
    fn check_result_has_numeric_evidence() {
        let run = StressRun::new("noop").iterations(100).threads(2);
        let r = run.execute(&Noop);
        let c = r.into_check_result(None);
        let labels: Vec<&str> = c.evidence.iter().map(|e| e.label.as_str()).collect();
        assert!(labels.contains(&"iterations"));
        assert!(labels.contains(&"threads"));
        assert!(labels.contains(&"ops_per_sec"));
        assert!(labels.contains(&"thread_time_cv"));
        assert!(labels.contains(&"total_elapsed_ms"));
    }

    #[test]
    fn ops_drop_below_threshold_fails() {
        let run = StressRun::new("x").iterations(50).threads(1);
        let r = run.execute(&Noop);
        let baseline = r.ops_per_sec() * 100.0; // way higher than current
        let opts = CompareOptions {
            baseline_ops_per_sec: Some(baseline),
            ops_drop_pct_threshold: 10.0,
            ..CompareOptions::default()
        };
        let c = r.compare_with_options(&opts);
        assert_eq!(c.verdict, Verdict::Fail);
        assert!(c.has_tag("regression"));
    }

    #[test]
    fn ops_within_threshold_passes() {
        let run = StressRun::new("x").iterations(50).threads(1);
        let r = run.execute(&Noop);
        let baseline = r.ops_per_sec(); // exactly current
        let opts = CompareOptions {
            baseline_ops_per_sec: Some(baseline),
            ops_drop_pct_threshold: 10.0,
            ..CompareOptions::default()
        };
        let c = r.compare_with_options(&opts);
        assert_eq!(c.verdict, Verdict::Pass);
    }

    #[test]
    fn latency_tracking_records_percentiles() {
        let run = StressRun::new("hot")
            .iterations(1_000)
            .threads(2)
            .track_latency(1);
        let r = run.execute(&Noop);
        let lat = r.latency.expect("latency tracked");
        assert!(lat.samples_count > 0);
        assert!(lat.p50.as_nanos() <= lat.p95.as_nanos());
        assert!(lat.p95.as_nanos() <= lat.p99.as_nanos());
    }

    #[test]
    fn p99_regression_detected() {
        let run = StressRun::new("hot")
            .iterations(200)
            .threads(2)
            .track_latency(1);
        let r = run.execute(&Noop);
        // Baseline p99 set so far below current that any non-zero p99 fails.
        let opts = CompareOptions {
            baseline_p99: Some(Duration::from_nanos(1)),
            p99_regression_pct_threshold: 0.0,
            ..CompareOptions::default()
        };
        let c = r.compare_with_options(&opts);
        // If p99 was 0 (very fast noop) treat as pass; otherwise fail.
        if r.latency.as_ref().unwrap().p99.as_nanos() > 1 {
            assert_eq!(c.verdict, Verdict::Fail);
            assert!(c.has_tag("regression"));
        }
    }

    #[test]
    fn into_report_emits_one_check() {
        let run = StressRun::new("noop").iterations(100).threads(2);
        let r = run.execute(&Noop);
        let report = r.into_report("0.1.0", &CompareOptions::default());
        assert_eq!(report.checks.len(), 1);
        assert_eq!(report.producer.as_deref(), Some("dev-stress"));
        assert_eq!(report.subject, "noop");
    }

    #[test]
    fn stress_producer_implements_producer_trait() {
        let producer = StressProducer::new(
            || {
                StressRun::new("hot")
                    .iterations(50)
                    .threads(1)
                    .execute(&Noop)
            },
            "0.1.0",
            CompareOptions::default(),
        );
        let report = producer.produce();
        assert_eq!(report.checks.len(), 1);
    }

    #[test]
    fn iterations_distribute_evenly_with_leftover() {
        // 7 iters across 3 threads -> 3, 2, 2 (front-loaded).
        let run = StressRun::new("x").iterations(7).threads(3);
        let r = run.execute(&Noop);
        assert_eq!(r.iterations, 7);
        assert_eq!(r.thread_times.len(), 3);
    }

    #[test]
    fn target_ops_per_sec_divides_across_threads() {
        let run = StressRun::new("x")
            .iterations(100)
            .threads(4)
            .target_ops_per_sec(1000.0);
        // 1000 ops/sec / 4 threads = 250 ops/sec/thread.
        assert_eq!(run.target_ops_per_sec_per_thread(), Some(250.0));
    }

    #[test]
    fn target_ops_per_sec_zero_or_negative_disables() {
        let run = StressRun::new("x")
            .iterations(10)
            .threads(2)
            .target_ops_per_sec(0.0);
        assert_eq!(run.target_ops_per_sec_per_thread(), None);
        let run = StressRun::new("x")
            .iterations(10)
            .threads(2)
            .target_ops_per_sec(-5.0);
        assert_eq!(run.target_ops_per_sec_per_thread(), None);
    }

    #[test]
    fn rate_limited_run_takes_at_least_target_duration() {
        // 50 ops at 100 ops/sec total -> at least 0.5 seconds (with one thread).
        // Use a reasonable lower bound to avoid flakes; we just want to
        // confirm the limiter actually slows things down.
        let started = Instant::now();
        let run = StressRun::new("x")
            .iterations(50)
            .threads(1)
            .target_ops_per_sec(100.0);
        let _ = run.execute(&Noop);
        let elapsed = started.elapsed();
        // Lower bound: 50/100 = 500ms; allow 50% slack for sleep granularity
        // on slow CI runners. Concretely we just want > 100ms (prove it slept).
        assert!(elapsed >= Duration::from_millis(100));
    }
}