mabi-modbus 1.4.0

Mabinogion - Modbus TCP/RTU simulator
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
//! Performance validation framework for large-scale testing.
//!
//! Provides infrastructure to validate:
//! - 10,000+ simultaneous connections
//! - 100,000+ transactions per second
//! - Sub-10ms P99 latency under load

use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::RwLock;
use tokio::net::TcpStream;
use tokio::sync::{broadcast, Semaphore};
use tokio::time::timeout;

use super::report::TestMetrics;

/// Performance validation configuration.
#[derive(Debug, Clone)]
pub struct PerformanceConfig {
    /// Target number of simultaneous connections.
    pub target_connections: usize,
    /// Target transactions per second.
    pub target_tps: u64,
    /// Test duration.
    pub duration: Duration,
    /// Connection ramp-up time.
    pub ramp_up: Duration,
    /// Server address to test.
    pub server_addr: SocketAddr,
    /// Maximum acceptable P99 latency.
    pub max_p99_latency: Duration,
    /// Maximum acceptable error rate (0.0 - 1.0).
    pub max_error_rate: f64,
    /// Number of warmup iterations.
    pub warmup_iterations: usize,
    /// Report interval for progress updates.
    pub report_interval: Duration,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            target_connections: 1000,
            target_tps: 10_000,
            duration: Duration::from_secs(60),
            ramp_up: Duration::from_secs(10),
            server_addr: "127.0.0.1:502".parse().unwrap(),
            max_p99_latency: Duration::from_millis(10),
            max_error_rate: 0.01,
            warmup_iterations: 100,
            report_interval: Duration::from_secs(5),
        }
    }
}

impl PerformanceConfig {
    /// Create configuration for 10K connection test.
    pub fn ten_thousand_connections() -> Self {
        Self {
            target_connections: 10_000,
            target_tps: 50_000,
            duration: Duration::from_secs(120),
            ramp_up: Duration::from_secs(30),
            max_p99_latency: Duration::from_millis(50),
            ..Default::default()
        }
    }

    /// Create configuration for 100K TPS test.
    pub fn hundred_thousand_tps() -> Self {
        Self {
            target_connections: 5_000,
            target_tps: 100_000,
            duration: Duration::from_secs(60),
            ramp_up: Duration::from_secs(15),
            max_p99_latency: Duration::from_millis(10),
            ..Default::default()
        }
    }

    /// Create configuration for stress test.
    pub fn stress_test() -> Self {
        Self {
            target_connections: 50_000,
            target_tps: 500_000,
            duration: Duration::from_secs(300),
            ramp_up: Duration::from_secs(60),
            max_p99_latency: Duration::from_millis(100),
            max_error_rate: 0.05,
            ..Default::default()
        }
    }

    /// Set target connections.
    pub fn with_connections(mut self, n: usize) -> Self {
        self.target_connections = n;
        self
    }

    /// Set target TPS.
    pub fn with_tps(mut self, tps: u64) -> Self {
        self.target_tps = tps;
        self
    }

    /// Set test duration.
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    /// Set server address.
    pub fn with_server(mut self, addr: SocketAddr) -> Self {
        self.server_addr = addr;
        self
    }
}

/// Performance targets to validate.
#[derive(Debug, Clone)]
pub struct PerformanceTarget {
    pub name: String,
    pub metric: TargetMetric,
    pub threshold: f64,
    pub comparison: Comparison,
}

#[derive(Debug, Clone, Copy)]
pub enum TargetMetric {
    Connections,
    Tps,
    P50Latency,
    P95Latency,
    P99Latency,
    ErrorRate,
    MemoryMb,
}

#[derive(Debug, Clone, Copy)]
pub enum Comparison {
    GreaterThan,
    LessThan,
    GreaterOrEqual,
    LessOrEqual,
}

impl PerformanceTarget {
    pub fn min_connections(n: usize) -> Self {
        Self {
            name: format!("Min {} connections", n),
            metric: TargetMetric::Connections,
            threshold: n as f64,
            comparison: Comparison::GreaterOrEqual,
        }
    }

    pub fn min_tps(tps: u64) -> Self {
        Self {
            name: format!("Min {} TPS", tps),
            metric: TargetMetric::Tps,
            threshold: tps as f64,
            comparison: Comparison::GreaterOrEqual,
        }
    }

    pub fn max_p99_latency(ms: u64) -> Self {
        Self {
            name: format!("Max P99 latency {}ms", ms),
            metric: TargetMetric::P99Latency,
            threshold: ms as f64,
            comparison: Comparison::LessOrEqual,
        }
    }

    pub fn max_error_rate(rate: f64) -> Self {
        Self {
            name: format!("Max error rate {:.1}%", rate * 100.0),
            metric: TargetMetric::ErrorRate,
            threshold: rate,
            comparison: Comparison::LessOrEqual,
        }
    }

    /// Check if the target is met.
    pub fn check(&self, value: f64) -> bool {
        match self.comparison {
            Comparison::GreaterThan => value > self.threshold,
            Comparison::LessThan => value < self.threshold,
            Comparison::GreaterOrEqual => value >= self.threshold,
            Comparison::LessOrEqual => value <= self.threshold,
        }
    }
}

/// Result of a single target validation.
#[derive(Debug, Clone)]
pub struct TargetResult {
    pub target: PerformanceTarget,
    pub actual_value: f64,
    pub passed: bool,
}

/// Validation result containing all target results.
#[derive(Debug, Clone)]
pub struct ValidationResult {
    pub passed: bool,
    pub targets: Vec<TargetResult>,
    pub metrics: TestMetrics,
    pub duration: Duration,
    pub error_message: Option<String>,
}

impl ValidationResult {
    /// Get a summary of the validation.
    pub fn summary(&self) -> String {
        let passed_count = self.targets.iter().filter(|t| t.passed).count();
        let total = self.targets.len();
        format!(
            "{} ({}/{} targets passed)",
            if self.passed { "PASSED" } else { "FAILED" },
            passed_count,
            total
        )
    }
}

/// Performance validator for running large-scale tests.
pub struct PerformanceValidator {
    /// Performance configuration (public for test access).
    pub config: PerformanceConfig,
    targets: Vec<PerformanceTarget>,
    running: Arc<AtomicBool>,
    metrics: Arc<LiveMetrics>,
}

/// Live metrics collector during test execution.
struct LiveMetrics {
    requests_total: AtomicU64,
    requests_success: AtomicU64,
    requests_failed: AtomicU64,
    connections_active: AtomicU64,
    connections_total: AtomicU64,
    latencies: RwLock<Vec<Duration>>,
}

impl LiveMetrics {
    fn new() -> Self {
        Self {
            requests_total: AtomicU64::new(0),
            requests_success: AtomicU64::new(0),
            requests_failed: AtomicU64::new(0),
            connections_active: AtomicU64::new(0),
            connections_total: AtomicU64::new(0),
            latencies: RwLock::new(Vec::with_capacity(100_000)),
        }
    }

    fn record_request(&self, success: bool, latency: Duration) {
        self.requests_total.fetch_add(1, Ordering::Relaxed);
        if success {
            self.requests_success.fetch_add(1, Ordering::Relaxed);
        } else {
            self.requests_failed.fetch_add(1, Ordering::Relaxed);
        }

        // Sample latencies (keep every Nth to manage memory)
        let total = self.requests_total.load(Ordering::Relaxed);
        if total % 100 == 0 {
            self.latencies.write().push(latency);
        }
    }

    fn add_connection(&self) {
        self.connections_active.fetch_add(1, Ordering::Relaxed);
        self.connections_total.fetch_add(1, Ordering::Relaxed);
    }

    fn remove_connection(&self) {
        self.connections_active.fetch_sub(1, Ordering::Relaxed);
    }

    fn snapshot(&self) -> MetricsSnapshot {
        let mut latencies = self.latencies.read().clone();
        latencies.sort();

        let p50 = latencies.get(latencies.len() / 2).copied();
        let p95 = latencies.get(latencies.len() * 95 / 100).copied();
        let p99 = latencies.get(latencies.len() * 99 / 100).copied();

        let total = self.requests_total.load(Ordering::Relaxed);
        let success = self.requests_success.load(Ordering::Relaxed);
        let failed = self.requests_failed.load(Ordering::Relaxed);

        MetricsSnapshot {
            requests_total: total,
            requests_success: success,
            requests_failed: failed,
            connections_active: self.connections_active.load(Ordering::Relaxed),
            connections_total: self.connections_total.load(Ordering::Relaxed),
            p50_latency: p50,
            p95_latency: p95,
            p99_latency: p99,
            error_rate: if total > 0 {
                failed as f64 / total as f64
            } else {
                0.0
            },
        }
    }
}

#[derive(Debug, Clone)]
struct MetricsSnapshot {
    requests_total: u64,
    requests_success: u64,
    requests_failed: u64,
    connections_active: u64,
    connections_total: u64,
    p50_latency: Option<Duration>,
    p95_latency: Option<Duration>,
    p99_latency: Option<Duration>,
    error_rate: f64,
}

impl PerformanceValidator {
    /// Create a new performance validator.
    pub fn new(config: PerformanceConfig) -> Self {
        Self {
            config,
            targets: Vec::new(),
            running: Arc::new(AtomicBool::new(false)),
            metrics: Arc::new(LiveMetrics::new()),
        }
    }

    /// Create validator for 10K connection test.
    pub fn ten_thousand_connections() -> Self {
        let config = PerformanceConfig::ten_thousand_connections();
        let mut validator = Self::new(config);
        validator.add_target(PerformanceTarget::min_connections(10_000));
        validator.add_target(PerformanceTarget::max_p99_latency(50));
        validator.add_target(PerformanceTarget::max_error_rate(0.01));
        validator
    }

    /// Create validator for 100K TPS test.
    pub fn hundred_thousand_tps() -> Self {
        let config = PerformanceConfig::hundred_thousand_tps();
        let mut validator = Self::new(config);
        validator.add_target(PerformanceTarget::min_tps(100_000));
        validator.add_target(PerformanceTarget::max_p99_latency(10));
        validator.add_target(PerformanceTarget::max_error_rate(0.01));
        validator
    }

    /// Add a performance target.
    pub fn add_target(&mut self, target: PerformanceTarget) {
        self.targets.push(target);
    }

    /// Set server address.
    pub fn server_addr(mut self, addr: SocketAddr) -> Self {
        self.config.server_addr = addr;
        self
    }

    /// Run the performance validation test.
    pub async fn run(&self) -> Result<ValidationResult, String> {
        tracing::info!(
            "Starting performance validation: {} connections, {} TPS target",
            self.config.target_connections,
            self.config.target_tps
        );

        self.running.store(true, Ordering::SeqCst);
        let start = Instant::now();

        // Spawn progress reporter
        let metrics = self.metrics.clone();
        let report_interval = self.config.report_interval;
        let running = self.running.clone();
        let reporter = tokio::spawn(async move {
            while running.load(Ordering::Relaxed) {
                tokio::time::sleep(report_interval).await;
                let snapshot = metrics.snapshot();
                tracing::info!(
                    "Progress: {} requests ({} success, {} failed), {} active connections, P99: {:?}",
                    snapshot.requests_total,
                    snapshot.requests_success,
                    snapshot.requests_failed,
                    snapshot.connections_active,
                    snapshot.p99_latency,
                );
            }
        });

        // Run the test
        let result = self.run_test().await;

        // Stop reporter
        self.running.store(false, Ordering::SeqCst);
        let _ = reporter.await;

        let duration = start.elapsed();

        // Build final metrics
        let snapshot = self.metrics.snapshot();
        let tps = if duration.as_secs() > 0 {
            snapshot.requests_total / duration.as_secs()
        } else {
            0
        };

        // Validate targets
        let mut target_results = Vec::new();
        let mut all_passed = true;

        for target in &self.targets {
            let value = match target.metric {
                TargetMetric::Connections => snapshot.connections_total as f64,
                TargetMetric::Tps => tps as f64,
                TargetMetric::P50Latency => snapshot
                    .p50_latency
                    .map(|d| d.as_millis() as f64)
                    .unwrap_or(0.0),
                TargetMetric::P95Latency => snapshot
                    .p95_latency
                    .map(|d| d.as_millis() as f64)
                    .unwrap_or(0.0),
                TargetMetric::P99Latency => snapshot
                    .p99_latency
                    .map(|d| d.as_millis() as f64)
                    .unwrap_or(0.0),
                TargetMetric::ErrorRate => snapshot.error_rate,
                TargetMetric::MemoryMb => 0.0, // TODO: Integrate with memory profiler
            };

            let passed = target.check(value);
            if !passed {
                all_passed = false;
            }

            target_results.push(TargetResult {
                target: target.clone(),
                actual_value: value,
                passed,
            });
        }

        let test_metrics = TestMetrics {
            total_requests: snapshot.requests_total,
            successful_requests: snapshot.requests_success,
            failed_requests: snapshot.requests_failed,
            total_connections: snapshot.connections_total,
            peak_connections: snapshot.connections_active, // Approximation
            avg_tps: tps,
            peak_tps: tps, // Would need more tracking for accurate peak
            p50_latency_ms: snapshot
                .p50_latency
                .map(|d| d.as_millis() as f64)
                .unwrap_or(0.0),
            p95_latency_ms: snapshot
                .p95_latency
                .map(|d| d.as_millis() as f64)
                .unwrap_or(0.0),
            p99_latency_ms: snapshot
                .p99_latency
                .map(|d| d.as_millis() as f64)
                .unwrap_or(0.0),
            error_rate: snapshot.error_rate,
            memory_peak_mb: 0.0, // TODO: Integrate with memory profiler
        };

        Ok(ValidationResult {
            passed: all_passed && result.is_ok(),
            targets: target_results,
            metrics: test_metrics,
            duration,
            error_message: result.err(),
        })
    }

    async fn run_test(&self) -> Result<(), String> {
        let semaphore = Arc::new(Semaphore::new(self.config.target_connections));
        let (shutdown_tx, _) = broadcast::channel::<()>(1);

        // Calculate request rate per connection
        let connections = self.config.target_connections;
        let target_tps = self.config.target_tps;
        let requests_per_connection = (target_tps as f64 / connections as f64).max(1.0);
        let request_interval = Duration::from_secs_f64(1.0 / requests_per_connection);

        // Spawn connection tasks with ramp-up
        let ramp_up_interval = self.config.ramp_up.as_millis() as usize / connections.max(1);
        let mut handles = Vec::with_capacity(connections);

        for i in 0..connections {
            if !self.running.load(Ordering::Relaxed) {
                break;
            }

            // Ramp-up delay
            if ramp_up_interval > 0 && i > 0 {
                tokio::time::sleep(Duration::from_millis(ramp_up_interval as u64)).await;
            }

            let permit = semaphore
                .clone()
                .acquire_owned()
                .await
                .map_err(|e| e.to_string())?;
            let metrics = self.metrics.clone();
            let server_addr = self.config.server_addr;
            let duration = self.config.duration;
            let running = self.running.clone();
            let mut shutdown_rx = shutdown_tx.subscribe();

            let handle = tokio::spawn(async move {
                let _permit = permit;

                // Connect to server
                let stream =
                    match timeout(Duration::from_secs(10), TcpStream::connect(server_addr)).await {
                        Ok(Ok(s)) => s,
                        Ok(Err(e)) => {
                            tracing::debug!("Connection failed: {}", e);
                            return;
                        }
                        Err(_) => {
                            tracing::debug!("Connection timeout");
                            return;
                        }
                    };

                metrics.add_connection();
                let start = Instant::now();

                // Send requests until duration expires or shutdown
                while running.load(Ordering::Relaxed) && start.elapsed() < duration {
                    tokio::select! {
                        _ = shutdown_rx.recv() => break,
                        _ = tokio::time::sleep(request_interval) => {
                            let req_start = Instant::now();
                            let success = Self::send_modbus_request(&stream).await;
                            let latency = req_start.elapsed();
                            metrics.record_request(success, latency);
                        }
                    }
                }

                metrics.remove_connection();
            });

            handles.push(handle);
        }

        // Wait for test duration
        tokio::time::sleep(self.config.duration).await;

        // Signal shutdown
        let _ = shutdown_tx.send(());
        self.running.store(false, Ordering::SeqCst);

        // Wait for all connections to close
        for handle in handles {
            let _ = handle.await;
        }

        Ok(())
    }

    /// Send a simple Modbus read request.
    async fn send_modbus_request(stream: &TcpStream) -> bool {
        #![allow(unused_imports)]
        use tokio::io::AsyncWriteExt;

        // Simple Modbus TCP read holding registers request
        // MBAP Header (7 bytes) + PDU (5 bytes)
        let request: [u8; 12] = [
            0x00, 0x01, // Transaction ID
            0x00, 0x00, // Protocol ID
            0x00, 0x06, // Length
            0x01, // Unit ID
            0x03, // Function code: Read Holding Registers
            0x00, 0x00, // Starting address
            0x00, 0x01, // Quantity
        ];

        let mut response = [0u8; 256];

        // We need to use try_write/try_read since we have &TcpStream not &mut
        // This is a simplified version - in production, use proper framing
        match stream.try_write(&request) {
            Ok(_) => {
                // Wait a bit for response
                tokio::time::sleep(Duration::from_micros(100)).await;
                match stream.try_read(&mut response) {
                    Ok(n) if n > 0 => true,
                    _ => false,
                }
            }
            Err(_) => false,
        }
    }
}

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

    #[test]
    fn test_performance_config_default() {
        let config = PerformanceConfig::default();
        assert_eq!(config.target_connections, 1000);
        assert_eq!(config.target_tps, 10_000);
    }

    #[test]
    fn test_performance_config_presets() {
        let config = PerformanceConfig::ten_thousand_connections();
        assert_eq!(config.target_connections, 10_000);

        let config = PerformanceConfig::hundred_thousand_tps();
        assert_eq!(config.target_tps, 100_000);
    }

    #[test]
    fn test_performance_target_check() {
        let target = PerformanceTarget::min_connections(10_000);
        assert!(target.check(10_000.0));
        assert!(target.check(15_000.0));
        assert!(!target.check(9_999.0));

        let target = PerformanceTarget::max_p99_latency(10);
        assert!(target.check(5.0));
        assert!(target.check(10.0));
        assert!(!target.check(15.0));
    }

    #[test]
    fn test_live_metrics() {
        let metrics = LiveMetrics::new();

        metrics.add_connection();
        assert_eq!(metrics.connections_active.load(Ordering::Relaxed), 1);

        metrics.record_request(true, Duration::from_millis(5));
        assert_eq!(metrics.requests_total.load(Ordering::Relaxed), 1);
        assert_eq!(metrics.requests_success.load(Ordering::Relaxed), 1);

        metrics.record_request(false, Duration::from_millis(10));
        assert_eq!(metrics.requests_total.load(Ordering::Relaxed), 2);
        assert_eq!(metrics.requests_failed.load(Ordering::Relaxed), 1);

        metrics.remove_connection();
        assert_eq!(metrics.connections_active.load(Ordering::Relaxed), 0);
    }
}