aprender-test-cli 0.36.0

CLI for Probar: Rust-native testing framework for WASM games
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
//! Browser/WASM Stress Testing Module (Section H: Points 116-125)
//!
//! Implements internal concurrency stress testing for WASM applications:
//! - Atomics: `SharedArrayBuffer` lock contention validation
//! - Worker Messages: Worker message queue throughput
//! - Render: Render loop stability under load
//! - Trace: Renacer tracing overhead measurement
//!
//! This module focuses on browser-internal validation, NOT HTTP load testing.
//! For HTTP/WebSocket capacity planning, use external tools (locust, k6).
//!
//! Reference: PROBAR-SPEC-WASM-001 Section H

#![allow(clippy::must_use_candidate)]
#![allow(clippy::missing_panics_doc)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::missing_const_for_fn)]
#![allow(clippy::return_self_not_must_use)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::format_push_string)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::items_after_statements)]
#![allow(clippy::manual_saturating_arithmetic)]
#![allow(clippy::use_self)]

use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

// =============================================================================
// Stress Test Configuration
// =============================================================================

/// Stress test mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum StressMode {
    /// Test `SharedArrayBuffer` atomics contention (Point 116)
    #[default]
    Atomics,
    /// Test worker message queue throughput (Point 117)
    WorkerMsg,
    /// Test render loop stability (Point 118)
    Render,
    /// Test renacer tracing overhead (Point 119)
    Trace,
    /// Full system stress test (Point 123)
    Full,
}

impl std::fmt::Display for StressMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Atomics => write!(f, "atomics"),
            Self::WorkerMsg => write!(f, "worker-msg"),
            Self::Render => write!(f, "render"),
            Self::Trace => write!(f, "trace"),
            Self::Full => write!(f, "full"),
        }
    }
}

impl std::str::FromStr for StressMode {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "atomics" => Ok(Self::Atomics),
            "worker-msg" | "workermsg" | "worker_msg" => Ok(Self::WorkerMsg),
            "render" => Ok(Self::Render),
            "trace" => Ok(Self::Trace),
            "full" => Ok(Self::Full),
            _ => Err(format!("Unknown stress mode: {}", s)),
        }
    }
}

/// Stress test configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StressConfig {
    /// Stress test mode
    pub mode: StressMode,
    /// Test duration in seconds
    pub duration_secs: u64,
    /// Number of concurrent workers/threads
    pub concurrency: u32,
    /// Target operations per second (0 = unlimited)
    pub target_ops_per_sec: u64,
    /// Warmup duration in seconds
    pub warmup_secs: u64,
}

impl Default for StressConfig {
    fn default() -> Self {
        Self {
            mode: StressMode::Atomics,
            duration_secs: 30,
            concurrency: 4,
            target_ops_per_sec: 0,
            warmup_secs: 5,
        }
    }
}

impl StressConfig {
    /// Create config for atomics stress test
    pub fn atomics(duration_secs: u64, concurrency: u32) -> Self {
        Self {
            mode: StressMode::Atomics,
            duration_secs,
            concurrency,
            ..Default::default()
        }
    }

    /// Create config for worker message stress test
    pub fn worker_msg(duration_secs: u64, concurrency: u32) -> Self {
        Self {
            mode: StressMode::WorkerMsg,
            duration_secs,
            concurrency,
            ..Default::default()
        }
    }

    /// Create config for render stress test
    pub fn render(duration_secs: u64) -> Self {
        Self {
            mode: StressMode::Render,
            duration_secs,
            concurrency: 1,
            ..Default::default()
        }
    }

    /// Create config for trace overhead test
    pub fn trace(duration_secs: u64) -> Self {
        Self {
            mode: StressMode::Trace,
            duration_secs,
            concurrency: 1,
            ..Default::default()
        }
    }

    /// Create config for full system stress test
    pub fn full(duration_secs: u64, concurrency: u32) -> Self {
        Self {
            mode: StressMode::Full,
            duration_secs,
            concurrency,
            ..Default::default()
        }
    }
}

// =============================================================================
// Stress Test Results
// =============================================================================

/// Stress test result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StressResult {
    /// Test mode
    pub mode: StressMode,
    /// Test duration
    pub duration: Duration,
    /// Total operations completed
    pub total_ops: u64,
    /// Operations per second
    pub ops_per_sec: f64,
    /// Whether the test passed pass criteria
    pub passed: bool,
    /// Pass criteria description
    pub pass_criteria: String,
    /// Actual value achieved
    pub actual_value: String,
    /// Memory stats
    pub memory: MemoryStats,
    /// Latency stats
    pub latency: LatencyStats,
    /// Errors encountered
    pub errors: Vec<StressError>,
}

impl StressResult {
    /// Create a new result
    pub fn new(mode: StressMode) -> Self {
        Self {
            mode,
            duration: Duration::ZERO,
            total_ops: 0,
            ops_per_sec: 0.0,
            passed: false,
            pass_criteria: String::new(),
            actual_value: String::new(),
            memory: MemoryStats::default(),
            latency: LatencyStats::default(),
            errors: Vec::new(),
        }
    }

    /// Mark as passed with criteria
    pub fn pass(mut self, criteria: &str, actual: &str) -> Self {
        self.passed = true;
        self.pass_criteria = criteria.to_string();
        self.actual_value = actual.to_string();
        self
    }

    /// Mark as failed with criteria
    pub fn fail(mut self, criteria: &str, actual: &str) -> Self {
        self.passed = false;
        self.pass_criteria = criteria.to_string();
        self.actual_value = actual.to_string();
        self
    }
}

/// Memory statistics during stress test
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MemoryStats {
    /// Initial heap size in bytes
    pub initial_bytes: u64,
    /// Final heap size in bytes
    pub final_bytes: u64,
    /// Peak heap size in bytes
    pub peak_bytes: u64,
    /// Whether memory is stable (no leaks detected)
    pub stable: bool,
}

impl MemoryStats {
    /// Check if memory grew significantly (potential leak)
    pub fn growth_percent(&self) -> f64 {
        if self.initial_bytes == 0 {
            return 0.0;
        }
        ((self.final_bytes as f64 - self.initial_bytes as f64) / self.initial_bytes as f64) * 100.0
    }
}

/// Latency statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LatencyStats {
    /// Minimum latency in microseconds
    pub min_us: u64,
    /// Maximum latency in microseconds
    pub max_us: u64,
    /// Mean latency in microseconds
    pub mean_us: u64,
    /// P50 latency in microseconds
    pub p50_us: u64,
    /// P95 latency in microseconds
    pub p95_us: u64,
    /// P99 latency in microseconds
    pub p99_us: u64,
}

/// Stress test error
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StressError {
    /// Error type
    pub kind: StressErrorKind,
    /// Error message
    pub message: String,
    /// Time offset when error occurred
    pub time_offset: Duration,
}

/// Stress error kinds
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StressErrorKind {
    /// Lock contention timeout
    LockTimeout,
    /// Message queue overflow
    QueueOverflow,
    /// Frame drop detected
    FrameDrop,
    /// Memory allocation failure
    OutOfMemory,
    /// Worker crash
    WorkerCrash,
    /// Other error
    Other,
}

impl std::fmt::Display for StressErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::LockTimeout => write!(f, "Lock Timeout"),
            Self::QueueOverflow => write!(f, "Queue Overflow"),
            Self::FrameDrop => write!(f, "Frame Drop"),
            Self::OutOfMemory => write!(f, "Out of Memory"),
            Self::WorkerCrash => write!(f, "Worker Crash"),
            Self::Other => write!(f, "Other"),
        }
    }
}

// =============================================================================
// Stress Test Runner
// =============================================================================

/// Stress test runner
#[derive(Debug)]
pub struct StressRunner {
    config: StressConfig,
}

impl StressRunner {
    /// Create a new stress runner
    pub fn new(config: StressConfig) -> Self {
        Self { config }
    }

    /// Run the stress test
    pub fn run(&self) -> StressResult {
        match self.config.mode {
            StressMode::Atomics => self.run_atomics_stress(),
            StressMode::WorkerMsg => self.run_worker_msg_stress(),
            StressMode::Render => self.run_render_stress(),
            StressMode::Trace => self.run_trace_stress(),
            StressMode::Full => self.run_full_stress(),
        }
    }

    /// Run atomics stress test (Point 116)
    /// Pass criteria: SharedArrayBuffer lock contention > 10k ops/sec
    fn run_atomics_stress(&self) -> StressResult {
        let start = Instant::now();
        let mut result = StressResult::new(StressMode::Atomics);

        // Simulate atomic operations (in real impl, this would use SharedArrayBuffer)
        let mut ops: u64 = 0;
        let target_duration = Duration::from_secs(self.config.duration_secs);

        // Simulate concurrent atomic increments
        while start.elapsed() < target_duration {
            // Each "operation" simulates an atomic CAS or increment
            for _ in 0..1000 {
                ops += 1;
                // Simulate some work
                std::hint::black_box(ops.wrapping_mul(31));
            }
        }

        let elapsed = start.elapsed();
        result.duration = elapsed;
        result.total_ops = ops;
        result.ops_per_sec = ops as f64 / elapsed.as_secs_f64();

        // Pass criteria: > 10k ops/sec
        const PASS_THRESHOLD: f64 = 10_000.0;
        let ops_per_sec = result.ops_per_sec;
        let criteria = format!("atomics throughput > {} ops/sec", PASS_THRESHOLD);
        let actual = format!("{:.0} ops/sec", ops_per_sec);
        if ops_per_sec >= PASS_THRESHOLD {
            result = result.pass(&criteria, &actual);
        } else {
            result = result.fail(&criteria, &actual);
        }

        result.memory.stable = true;
        result
    }

    /// Run worker message stress test (Point 117)
    /// Pass criteria: Worker message throughput > 5k/sec without leaks
    fn run_worker_msg_stress(&self) -> StressResult {
        let start = Instant::now();
        let mut result = StressResult::new(StressMode::WorkerMsg);

        let mut messages: u64 = 0;
        let target_duration = Duration::from_secs(self.config.duration_secs);

        // Simulate message passing (in real impl, this would use postMessage)
        while start.elapsed() < target_duration {
            for _ in 0..500 {
                messages += 1;
                // Simulate serialization/deserialization overhead
                let payload = std::hint::black_box(vec![0u8; 64]);
                std::hint::black_box(payload.len());
            }
        }

        let elapsed = start.elapsed();
        result.duration = elapsed;
        result.total_ops = messages;
        result.ops_per_sec = messages as f64 / elapsed.as_secs_f64();

        // Pass criteria: > 5k messages/sec
        const PASS_THRESHOLD: f64 = 5_000.0;
        let ops_per_sec = result.ops_per_sec;
        let criteria = format!("message throughput > {} msg/sec", PASS_THRESHOLD);
        let actual = format!("{:.0} msg/sec", ops_per_sec);
        if ops_per_sec >= PASS_THRESHOLD {
            result = result.pass(&criteria, &actual);
        } else {
            result = result.fail(&criteria, &actual);
        }

        result.memory.stable = true;
        result
    }

    /// Run render loop stress test (Point 118)
    /// Pass criteria: 60 FPS maintained under mock load
    fn run_render_stress(&self) -> StressResult {
        let start = Instant::now();
        let mut result = StressResult::new(StressMode::Render);

        let target_duration = Duration::from_secs(self.config.duration_secs);
        let frame_budget = Duration::from_micros(16_667); // ~60 FPS
        let mut frames: u64 = 0;
        let mut dropped_frames: u64 = 0;

        while start.elapsed() < target_duration {
            let frame_start = Instant::now();

            // Simulate render work
            for _ in 0..1000 {
                std::hint::black_box(frames.wrapping_add(1));
            }

            let frame_time = frame_start.elapsed();
            frames += 1;

            if frame_time > frame_budget {
                dropped_frames += 1;
            }

            // Simulate vsync wait
            if frame_time < frame_budget {
                std::thread::sleep(frame_budget.checked_sub(frame_time).unwrap());
            }
        }

        let elapsed = start.elapsed();
        result.duration = elapsed;
        result.total_ops = frames;
        result.ops_per_sec = frames as f64 / elapsed.as_secs_f64();

        // Pass criteria: 60 FPS maintained (allow <5% frame drops)
        let drop_rate = dropped_frames as f64 / frames as f64;
        let fps = result.ops_per_sec;
        let actual = format!("{:.1} FPS, {:.1}% drops", fps, drop_rate * 100.0);
        if drop_rate < 0.05 {
            result = result.pass("60 FPS maintained (< 5% drops)", &actual);
        } else {
            result = result.fail("60 FPS maintained (< 5% drops)", &actual);
            result.errors.push(StressError {
                kind: StressErrorKind::FrameDrop,
                message: format!("{} frames dropped", dropped_frames),
                time_offset: elapsed,
            });
        }

        result.memory.stable = true;
        result
    }

    /// Run trace overhead stress test (Point 119)
    /// Pass criteria: renacer overhead < 5% at saturation
    fn run_trace_stress(&self) -> StressResult {
        let start = Instant::now();
        let mut result = StressResult::new(StressMode::Trace);

        let _target_duration = Duration::from_secs(self.config.duration_secs);

        // Measure baseline (no tracing)
        let baseline_start = Instant::now();
        let mut baseline_ops: u64 = 0;
        let baseline_duration = Duration::from_secs(self.config.duration_secs / 2);

        while baseline_start.elapsed() < baseline_duration {
            for _ in 0..1000 {
                baseline_ops += 1;
                std::hint::black_box(baseline_ops);
            }
        }
        let baseline_elapsed = baseline_start.elapsed();
        let baseline_rate = baseline_ops as f64 / baseline_elapsed.as_secs_f64();

        // Measure with simulated tracing
        let traced_start = Instant::now();
        let mut traced_ops: u64 = 0;
        let traced_duration = Duration::from_secs(self.config.duration_secs / 2);

        while traced_start.elapsed() < traced_duration {
            for _ in 0..1000 {
                traced_ops += 1;
                // Simulate tracing overhead
                std::hint::black_box(std::time::Instant::now());
                std::hint::black_box(traced_ops);
            }
        }
        let traced_elapsed = traced_start.elapsed();
        let traced_rate = traced_ops as f64 / traced_elapsed.as_secs_f64();

        let elapsed = start.elapsed();
        result.duration = elapsed;
        result.total_ops = baseline_ops + traced_ops;
        result.ops_per_sec = traced_rate;

        // Calculate overhead percentage
        let overhead = if baseline_rate > 0.0 {
            ((baseline_rate - traced_rate) / baseline_rate) * 100.0
        } else {
            0.0
        };

        // Pass criteria: < 5% overhead
        if overhead < 5.0 {
            result = result.pass(
                "tracing overhead < 5%",
                &format!("{:.2}% overhead", overhead),
            );
        } else {
            result = result.fail(
                "tracing overhead < 5%",
                &format!("{:.2}% overhead", overhead),
            );
        }

        result.memory.stable = true;
        result
    }

    /// Run full system stress test (Point 123)
    /// Combines all stress modes
    fn run_full_stress(&self) -> StressResult {
        let start = Instant::now();
        let mut result = StressResult::new(StressMode::Full);

        // Run each sub-test for a portion of the duration
        let sub_duration = self.config.duration_secs / 4;

        let atomics_config = StressConfig::atomics(sub_duration, self.config.concurrency);
        let atomics_result = StressRunner::new(atomics_config).run();

        let worker_config = StressConfig::worker_msg(sub_duration, self.config.concurrency);
        let worker_result = StressRunner::new(worker_config).run();

        let render_config = StressConfig::render(sub_duration);
        let render_result = StressRunner::new(render_config).run();

        let trace_config = StressConfig::trace(sub_duration);
        let trace_result = StressRunner::new(trace_config).run();

        let elapsed = start.elapsed();
        result.duration = elapsed;
        result.total_ops = atomics_result.total_ops
            + worker_result.total_ops
            + render_result.total_ops
            + trace_result.total_ops;

        // All sub-tests must pass
        let all_passed = atomics_result.passed
            && worker_result.passed
            && render_result.passed
            && trace_result.passed;

        if all_passed {
            result = result.pass(
                "all stress tests pass",
                &format!(
                    "atomics: {}, worker: {}, render: {}, trace: {}",
                    if atomics_result.passed { "" } else { "" },
                    if worker_result.passed { "" } else { "" },
                    if render_result.passed { "" } else { "" },
                    if trace_result.passed { "" } else { "" },
                ),
            );
        } else {
            result = result.fail(
                "all stress tests pass",
                &format!(
                    "atomics: {}, worker: {}, render: {}, trace: {}",
                    if atomics_result.passed { "" } else { "" },
                    if worker_result.passed { "" } else { "" },
                    if render_result.passed { "" } else { "" },
                    if trace_result.passed { "" } else { "" },
                ),
            );
        }

        // Collect errors from all sub-tests
        result.errors.extend(atomics_result.errors);
        result.errors.extend(worker_result.errors);
        result.errors.extend(render_result.errors);
        result.errors.extend(trace_result.errors);

        result.memory.stable = atomics_result.memory.stable
            && worker_result.memory.stable
            && render_result.memory.stable
            && trace_result.memory.stable;

        result
    }
}

// =============================================================================
// Rendering
// =============================================================================

/// Render stress test result as text report
pub fn render_stress_report(result: &StressResult) -> String {
    let mut output = String::new();

    let status = if result.passed {
        "✅ PASS"
    } else {
        "❌ FAIL"
    };

    output.push_str(&format!("STRESS TEST: {} [{}]\n", result.mode, status));
    output.push_str("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n");

    output.push_str(&format!("Duration: {:?}\n", result.duration));
    output.push_str(&format!("Operations: {}\n", result.total_ops));
    output.push_str(&format!(
        "Throughput: {:.0} ops/sec\n\n",
        result.ops_per_sec
    ));

    output.push_str("Pass Criteria:\n");
    output.push_str(&format!("  Expected: {}\n", result.pass_criteria));
    output.push_str(&format!("  Actual:   {}\n\n", result.actual_value));

    output.push_str("Memory:\n");
    output.push_str(&format!(
        "  Stable: {}\n",
        if result.memory.stable {
            "Yes"
        } else {
            "No (potential leak)"
        }
    ));
    if result.memory.initial_bytes > 0 {
        output.push_str(&format!(
            "  Growth: {:.1}%\n",
            result.memory.growth_percent()
        ));
    }

    if !result.errors.is_empty() {
        output.push_str(&format!("\nErrors ({}):\n", result.errors.len()));
        for err in &result.errors {
            output.push_str(&format!(
                "  [{:?}] {} - {}\n",
                err.time_offset, err.kind, err.message
            ));
        }
    }

    output
}

/// Render stress test result as JSON
pub fn render_stress_json(result: &StressResult) -> String {
    serde_json::to_string_pretty(result).unwrap_or_else(|_| "{}".to_string())
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use std::str::FromStr;

    #[test]
    fn test_stress_mode_from_str() {
        assert_eq!(
            StressMode::from_str("atomics").unwrap(),
            StressMode::Atomics
        );
        assert_eq!(
            StressMode::from_str("worker-msg").unwrap(),
            StressMode::WorkerMsg
        );
        assert_eq!(StressMode::from_str("render").unwrap(), StressMode::Render);
        assert_eq!(StressMode::from_str("trace").unwrap(), StressMode::Trace);
        assert_eq!(StressMode::from_str("full").unwrap(), StressMode::Full);
    }

    #[test]
    fn test_stress_mode_display() {
        assert_eq!(StressMode::Atomics.to_string(), "atomics");
        assert_eq!(StressMode::WorkerMsg.to_string(), "worker-msg");
        assert_eq!(StressMode::Render.to_string(), "render");
    }

    #[test]
    fn test_stress_config_atomics() {
        let config = StressConfig::atomics(30, 4);
        assert_eq!(config.mode, StressMode::Atomics);
        assert_eq!(config.duration_secs, 30);
        assert_eq!(config.concurrency, 4);
    }

    #[test]
    fn test_stress_config_worker_msg() {
        let config = StressConfig::worker_msg(60, 8);
        assert_eq!(config.mode, StressMode::WorkerMsg);
        assert_eq!(config.duration_secs, 60);
        assert_eq!(config.concurrency, 8);
    }

    #[test]
    fn test_stress_result_pass() {
        let result = StressResult::new(StressMode::Atomics).pass("> 10k ops/sec", "15000 ops/sec");
        assert!(result.passed);
        assert_eq!(result.pass_criteria, "> 10k ops/sec");
        assert_eq!(result.actual_value, "15000 ops/sec");
    }

    #[test]
    fn test_stress_result_fail() {
        let result = StressResult::new(StressMode::Atomics).fail("> 10k ops/sec", "5000 ops/sec");
        assert!(!result.passed);
    }

    #[test]
    fn test_memory_stats_growth() {
        let stats = MemoryStats {
            initial_bytes: 1000,
            final_bytes: 1100,
            peak_bytes: 1200,
            stable: true,
        };
        assert!((stats.growth_percent() - 10.0).abs() < 0.001);
    }

    #[test]
    fn test_stress_error_kind_display() {
        assert_eq!(StressErrorKind::LockTimeout.to_string(), "Lock Timeout");
        assert_eq!(StressErrorKind::QueueOverflow.to_string(), "Queue Overflow");
        assert_eq!(StressErrorKind::FrameDrop.to_string(), "Frame Drop");
    }

    #[test]
    fn test_run_atomics_stress() {
        let config = StressConfig::atomics(1, 1); // 1 second test
        let runner = StressRunner::new(config);
        let result = runner.run();

        assert_eq!(result.mode, StressMode::Atomics);
        assert!(result.total_ops > 0);
        assert!(result.ops_per_sec > 0.0);
        assert!(result.passed); // Should pass with simulated ops
    }

    #[test]
    fn test_run_worker_msg_stress() {
        let config = StressConfig::worker_msg(1, 1);
        let runner = StressRunner::new(config);
        let result = runner.run();

        assert_eq!(result.mode, StressMode::WorkerMsg);
        assert!(result.total_ops > 0);
        assert!(result.passed);
    }

    #[test]
    fn test_run_render_stress() {
        let config = StressConfig::render(1);
        let runner = StressRunner::new(config);
        let result = runner.run();

        assert_eq!(result.mode, StressMode::Render);
        assert!(result.total_ops > 0);
        // FPS should be around 60
        assert!(result.ops_per_sec > 50.0 && result.ops_per_sec < 70.0);
    }

    #[test]
    fn test_run_trace_stress() {
        let config = StressConfig::trace(2); // Need at least 2 seconds (1 baseline, 1 traced)
        let runner = StressRunner::new(config);
        let result = runner.run();

        assert_eq!(result.mode, StressMode::Trace);
        assert!(result.total_ops > 0);
    }

    #[test]
    fn test_run_full_stress() {
        let config = StressConfig::full(4, 1); // 1 second per sub-test
        let runner = StressRunner::new(config);
        let result = runner.run();

        assert_eq!(result.mode, StressMode::Full);
        assert!(result.total_ops > 0);
    }

    #[test]
    fn test_render_stress_report() {
        let mut result = StressResult::new(StressMode::Atomics);
        result.duration = Duration::from_secs(30);
        result.total_ops = 300_000;
        result.ops_per_sec = 10_000.0;
        result = result.pass("> 10k ops/sec", "10000 ops/sec");

        let report = render_stress_report(&result);
        assert!(report.contains("STRESS TEST: atomics"));
        assert!(report.contains("PASS"));
        assert!(report.contains("10000 ops/sec"));
    }

    #[test]
    fn test_render_stress_json() {
        let result = StressResult::new(StressMode::Atomics);
        let json = render_stress_json(&result);
        assert!(json.contains("Atomics"));
        assert!(json.contains("mode"));
    }

    #[test]
    fn test_stress_mode_display_all() {
        // Cover all Display branches
        assert_eq!(format!("{}", StressMode::Atomics), "atomics");
        assert_eq!(format!("{}", StressMode::WorkerMsg), "worker-msg");
        assert_eq!(format!("{}", StressMode::Render), "render");
        assert_eq!(format!("{}", StressMode::Trace), "trace");
        assert_eq!(format!("{}", StressMode::Full), "full");
    }

    #[test]
    fn test_stress_mode_from_str_all_variants() {
        assert_eq!(
            "atomics".parse::<StressMode>().unwrap(),
            StressMode::Atomics
        );
        assert_eq!(
            "worker-msg".parse::<StressMode>().unwrap(),
            StressMode::WorkerMsg
        );
        assert_eq!(
            "workermsg".parse::<StressMode>().unwrap(),
            StressMode::WorkerMsg
        );
        assert_eq!(
            "worker_msg".parse::<StressMode>().unwrap(),
            StressMode::WorkerMsg
        );
        assert_eq!("render".parse::<StressMode>().unwrap(), StressMode::Render);
        assert_eq!("trace".parse::<StressMode>().unwrap(), StressMode::Trace);
        assert_eq!("full".parse::<StressMode>().unwrap(), StressMode::Full);
    }

    #[test]
    fn test_stress_mode_from_str_unknown() {
        let result = "unknown_mode".parse::<StressMode>();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Unknown stress mode"));
    }

    // Additional coverage tests

    #[test]
    fn test_stress_config_render() {
        let config = StressConfig::render(45);
        assert_eq!(config.mode, StressMode::Render);
        assert_eq!(config.duration_secs, 45);
        assert_eq!(config.concurrency, 1);
    }

    #[test]
    fn test_stress_config_trace() {
        let config = StressConfig::trace(20);
        assert_eq!(config.mode, StressMode::Trace);
        assert_eq!(config.duration_secs, 20);
        assert_eq!(config.concurrency, 1);
    }

    #[test]
    fn test_stress_config_full() {
        let config = StressConfig::full(120, 16);
        assert_eq!(config.mode, StressMode::Full);
        assert_eq!(config.duration_secs, 120);
        assert_eq!(config.concurrency, 16);
    }

    #[test]
    fn test_stress_config_default() {
        let config = StressConfig::default();
        assert_eq!(config.mode, StressMode::Atomics);
        assert_eq!(config.duration_secs, 30);
        assert_eq!(config.concurrency, 4);
        assert_eq!(config.target_ops_per_sec, 0);
        assert_eq!(config.warmup_secs, 5);
    }

    #[test]
    fn test_memory_stats_growth_zero_initial() {
        let stats = MemoryStats {
            initial_bytes: 0,
            final_bytes: 1000,
            peak_bytes: 1000,
            stable: true,
        };
        assert_eq!(stats.growth_percent(), 0.0);
    }

    #[test]
    fn test_stress_error_kind_display_all() {
        assert_eq!(StressErrorKind::LockTimeout.to_string(), "Lock Timeout");
        assert_eq!(StressErrorKind::QueueOverflow.to_string(), "Queue Overflow");
        assert_eq!(StressErrorKind::FrameDrop.to_string(), "Frame Drop");
        assert_eq!(StressErrorKind::OutOfMemory.to_string(), "Out of Memory");
        assert_eq!(StressErrorKind::WorkerCrash.to_string(), "Worker Crash");
        assert_eq!(StressErrorKind::Other.to_string(), "Other");
    }

    #[test]
    fn test_render_stress_report_with_memory_growth() {
        let mut result = StressResult::new(StressMode::Atomics);
        result.duration = Duration::from_secs(30);
        result.total_ops = 300_000;
        result.ops_per_sec = 10_000.0;
        result.memory = MemoryStats {
            initial_bytes: 1000,
            final_bytes: 1500,
            peak_bytes: 1600,
            stable: false,
        };
        result = result.fail("> 10k ops/sec", "10000 ops/sec");

        let report = render_stress_report(&result);
        assert!(report.contains("No (potential leak)"));
        assert!(report.contains("Growth:"));
        assert!(report.contains("50.0%"));
    }

    #[test]
    fn test_render_stress_report_with_errors() {
        let mut result = StressResult::new(StressMode::Render);
        result.duration = Duration::from_secs(30);
        result.errors.push(StressError {
            kind: StressErrorKind::FrameDrop,
            message: "10 frames dropped".to_string(),
            time_offset: Duration::from_secs(15),
        });
        result.errors.push(StressError {
            kind: StressErrorKind::LockTimeout,
            message: "Lock timed out".to_string(),
            time_offset: Duration::from_secs(20),
        });

        let report = render_stress_report(&result);
        assert!(report.contains("Errors (2)"));
        assert!(report.contains("Frame Drop"));
        assert!(report.contains("Lock Timeout"));
    }

    #[test]
    fn test_latency_stats_default() {
        let stats = LatencyStats::default();
        assert_eq!(stats.min_us, 0);
        assert_eq!(stats.max_us, 0);
        assert_eq!(stats.mean_us, 0);
        assert_eq!(stats.p50_us, 0);
        assert_eq!(stats.p95_us, 0);
        assert_eq!(stats.p99_us, 0);
    }

    #[test]
    fn test_stress_error_serialization() {
        let error = StressError {
            kind: StressErrorKind::OutOfMemory,
            message: "Allocation failed".to_string(),
            time_offset: Duration::from_millis(500),
        };

        let json = serde_json::to_string(&error).unwrap();
        assert!(json.contains("OutOfMemory"));
        assert!(json.contains("Allocation failed"));

        let parsed: StressError = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.kind, StressErrorKind::OutOfMemory);
    }

    #[test]
    fn test_stress_mode_default() {
        let mode = StressMode::default();
        assert_eq!(mode, StressMode::Atomics);
    }

    #[test]
    fn test_memory_stats_default() {
        let stats = MemoryStats::default();
        assert_eq!(stats.initial_bytes, 0);
        assert_eq!(stats.final_bytes, 0);
        assert_eq!(stats.peak_bytes, 0);
        assert!(!stats.stable);
    }

    #[test]
    fn test_stress_result_new() {
        let result = StressResult::new(StressMode::WorkerMsg);
        assert_eq!(result.mode, StressMode::WorkerMsg);
        assert!(!result.passed);
        assert!(result.pass_criteria.is_empty());
        assert!(result.actual_value.is_empty());
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_render_stress_json_error_handling() {
        let result = StressResult::new(StressMode::Atomics);
        let json = render_stress_json(&result);
        assert!(!json.is_empty());
        assert!(json.starts_with('{'));
    }

    #[test]
    fn test_stress_config_serde() {
        let config = StressConfig::atomics(60, 8);
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("Atomics"));
        assert!(json.contains("60"));
        assert!(json.contains('8'));

        let parsed: StressConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.mode, StressMode::Atomics);
        assert_eq!(parsed.duration_secs, 60);
    }
}