oxirs-cluster 0.3.1

Raft-backed distributed dataset for high availability and horizontal scaling
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
//! The comprehensive cluster metrics manager.
//!
//! This module implements [`ClusterMetricsManager`], which coordinates SciRS2-Core
//! metric primitives (counters, gauges, histograms, timers, profiler) together with
//! [`EnhancedLatencyStats`] per operation. It provides timing, gauge/counter helpers,
//! Prometheus export, report generation, baseline establishment, statistical
//! regression detection, and a synthetic benchmarking suite.

use crate::cluster_metrics_stats::{ClusterOperation, EnhancedLatencyStats};
use crate::cluster_metrics_types::{
    BenchmarkComparison, BenchmarkResultRecord, OperationBaseline, OperationMetrics,
    OperationTimer, PerformanceRegression, RegressionSeverity,
};
use scirs2_core::metrics::{Counter, Gauge, Histogram, MetricsRegistry, Timer};
use scirs2_core::profiling::Profiler;
use scirs2_stats::distributions::StudentT;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Instant, SystemTime};
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

/// Comprehensive cluster metrics manager
pub struct ClusterMetricsManager {
    /// Node ID
    node_id: u64,
    /// SciRS2-Core metrics registry (reserved for future use)
    #[allow(dead_code)]
    registry: Arc<MetricsRegistry>,
    /// SciRS2-Core profiler
    profiler: Arc<RwLock<Profiler>>,
    /// Enhanced latency statistics per operation
    latency_stats: Arc<RwLock<HashMap<String, EnhancedLatencyStats>>>,
    /// SciRS2-Core histograms
    histograms: Arc<RwLock<HashMap<String, Histogram>>>,
    /// SciRS2-Core counters
    counters: Arc<RwLock<HashMap<String, Counter>>>,
    /// SciRS2-Core gauges
    gauges: Arc<RwLock<HashMap<String, Gauge>>>,
    /// SciRS2-Core timers (reserved for future use)
    #[allow(dead_code)]
    timers: Arc<RwLock<HashMap<String, Timer>>>,
    /// Enabled flag
    enabled: Arc<RwLock<bool>>,
    /// Rolling window size for statistics
    window_size: usize,
    /// Benchmark results storage
    benchmark_results: Arc<RwLock<Vec<BenchmarkResultRecord>>>,
    /// Regression baselines
    baselines: Arc<RwLock<HashMap<String, OperationBaseline>>>,
}

impl ClusterMetricsManager {
    /// Create a new cluster metrics manager
    pub fn new(node_id: u64, window_size: usize) -> Self {
        let registry = Arc::new(MetricsRegistry::new());
        let profiler = Arc::new(RwLock::new(Profiler::new()));

        Self {
            node_id,
            registry,
            profiler,
            latency_stats: Arc::new(RwLock::new(HashMap::new())),
            histograms: Arc::new(RwLock::new(HashMap::new())),
            counters: Arc::new(RwLock::new(HashMap::new())),
            gauges: Arc::new(RwLock::new(HashMap::new())),
            timers: Arc::new(RwLock::new(HashMap::new())),
            enabled: Arc::new(RwLock::new(true)),
            window_size,
            benchmark_results: Arc::new(RwLock::new(Vec::new())),
            baselines: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Enable metrics collection
    pub async fn enable(&self) {
        let mut enabled = self.enabled.write().await;
        *enabled = true;
        info!("Cluster metrics enabled for node {}", self.node_id);
    }

    /// Disable metrics collection
    pub async fn disable(&self) {
        let mut enabled = self.enabled.write().await;
        *enabled = false;
        info!("Cluster metrics disabled for node {}", self.node_id);
    }

    /// Check if metrics are enabled
    pub async fn is_enabled(&self) -> bool {
        *self.enabled.read().await
    }

    /// Start timing an operation
    pub async fn start_operation(&self, operation: ClusterOperation) -> OperationTimer {
        if !*self.enabled.read().await {
            return OperationTimer::disabled();
        }

        let label = operation.as_str().to_string();

        // Initialize metrics if not exists
        {
            let mut stats = self.latency_stats.write().await;
            if !stats.contains_key(&label) {
                stats.insert(label.clone(), EnhancedLatencyStats::new(self.window_size));
            }

            let mut histograms = self.histograms.write().await;
            if !histograms.contains_key(&label) {
                histograms.insert(
                    label.clone(),
                    Histogram::new(format!("cluster_{}_latency_us", label)),
                );
            }

            let mut counters = self.counters.write().await;
            if !counters.contains_key(&label) {
                counters.insert(
                    label.clone(),
                    Counter::new(format!("cluster_{}_count", label)),
                );
            }
        }

        // Start profiling
        {
            let mut profiler = self.profiler.write().await;
            profiler.start();
        }

        debug!(
            "Started timing {} for node {}",
            operation.as_str(),
            self.node_id
        );

        OperationTimer {
            operation,
            label,
            start_time: Instant::now(),
            latency_stats: Arc::clone(&self.latency_stats),
            profiler: Arc::clone(&self.profiler),
            histograms: Arc::clone(&self.histograms),
            counters: Arc::clone(&self.counters),
            enabled: true,
        }
    }

    /// Set a gauge value
    pub async fn set_gauge(&self, name: &str, value: f64) {
        if !*self.enabled.read().await {
            return;
        }

        let mut gauges = self.gauges.write().await;
        let gauge = gauges
            .entry(name.to_string())
            .or_insert_with(|| Gauge::new(format!("cluster_{}", name)));
        gauge.set(value);
    }

    /// Increment a gauge
    pub async fn inc_gauge(&self, name: &str) {
        if !*self.enabled.read().await {
            return;
        }

        let mut gauges = self.gauges.write().await;
        let gauge = gauges
            .entry(name.to_string())
            .or_insert_with(|| Gauge::new(format!("cluster_{}", name)));
        gauge.inc();
    }

    /// Decrement a gauge
    pub async fn dec_gauge(&self, name: &str) {
        if !*self.enabled.read().await {
            return;
        }

        let mut gauges = self.gauges.write().await;
        let gauge = gauges
            .entry(name.to_string())
            .or_insert_with(|| Gauge::new(format!("cluster_{}", name)));
        gauge.dec();
    }

    /// Get gauge value
    pub async fn get_gauge(&self, name: &str) -> f64 {
        let gauges = self.gauges.read().await;
        gauges.get(name).map(|g| g.get()).unwrap_or(0.0)
    }

    /// Increment a counter
    pub async fn inc_counter(&self, name: &str) {
        if !*self.enabled.read().await {
            return;
        }

        let mut counters = self.counters.write().await;
        let counter = counters
            .entry(name.to_string())
            .or_insert_with(|| Counter::new(format!("cluster_{}", name)));
        counter.inc();
    }

    /// Increment counter by value
    pub async fn inc_counter_by(&self, name: &str, value: u64) {
        if !*self.enabled.read().await {
            return;
        }

        let mut counters = self.counters.write().await;
        let counter = counters
            .entry(name.to_string())
            .or_insert_with(|| Counter::new(format!("cluster_{}", name)));
        counter.add(value);
    }

    /// Get counter value
    pub async fn get_counter(&self, name: &str) -> u64 {
        let counters = self.counters.read().await;
        counters.get(name).map(|c| c.get()).unwrap_or(0)
    }

    /// Get operation metrics
    pub async fn get_operation_metrics(
        &self,
        operation: ClusterOperation,
    ) -> Option<OperationMetrics> {
        let label = operation.as_str().to_string();
        let stats = self.latency_stats.read().await;
        let stat = stats.get(&label)?;

        let counters = self.counters.read().await;
        let count = counters.get(&label).map(|c| c.get()).unwrap_or(0);

        Some(OperationMetrics {
            operation: operation.as_str().to_string(),
            node_id: self.node_id,
            count,
            mean_micros: stat.mean(),
            std_dev_micros: stat.std_dev(),
            variance_micros: stat.variance(),
            cv: stat.coefficient_of_variation(),
            p50_micros: stat.percentile(50.0),
            p75_micros: stat.percentile(75.0),
            p90_micros: stat.percentile(90.0),
            p95_micros: stat.percentile(95.0),
            p99_micros: stat.percentile(99.0),
            p999_micros: stat.percentile(99.9),
            min_micros: if stat.min == f64::MAX { 0.0 } else { stat.min },
            max_micros: stat.max,
            iqr_micros: stat.iqr(),
            skewness: stat.skewness(),
            kurtosis: stat.kurtosis(),
            trend: stat.trend(),
            ema_micros: stat.ema(),
            rate_ops_per_sec: stat.rate(),
            timestamp: chrono::Utc::now().to_rfc3339(),
        })
    }

    /// Get all operation metrics
    pub async fn get_all_metrics(&self) -> Vec<OperationMetrics> {
        let mut all_metrics = Vec::new();

        for operation in ClusterOperation::all() {
            if let Some(metrics) = self.get_operation_metrics(operation).await {
                all_metrics.push(metrics);
            }
        }

        all_metrics
    }

    /// Reset all metrics
    pub async fn reset(&self) {
        let mut stats = self.latency_stats.write().await;
        stats.clear();

        let mut counters = self.counters.write().await;
        counters.clear();

        let mut gauges = self.gauges.write().await;
        gauges.clear();

        let mut histograms = self.histograms.write().await;
        histograms.clear();

        info!("Reset all cluster metrics for node {}", self.node_id);
    }

    /// Export metrics in Prometheus format
    pub async fn export_prometheus(&self) -> String {
        let mut output = String::new();

        // Export counters
        {
            let counters = self.counters.read().await;
            for (label, counter) in counters.iter() {
                output.push_str(&format!(
                    "# HELP oxirs_cluster_{}_total Total count of {} operations\n",
                    label, label
                ));
                output.push_str(&format!("# TYPE oxirs_cluster_{}_total counter\n", label));
                output.push_str(&format!(
                    "oxirs_cluster_{}_total{{node_id=\"{}\"}} {}\n",
                    label,
                    self.node_id,
                    counter.get()
                ));
            }
        }

        // Export gauges
        {
            let gauges = self.gauges.read().await;
            for (label, gauge) in gauges.iter() {
                output.push_str(&format!(
                    "# HELP oxirs_cluster_{} Current value of {}\n",
                    label, label
                ));
                output.push_str(&format!("# TYPE oxirs_cluster_{} gauge\n", label));
                output.push_str(&format!(
                    "oxirs_cluster_{}{{node_id=\"{}\"}} {}\n",
                    label,
                    self.node_id,
                    gauge.get()
                ));
            }
        }

        // Export latency statistics
        {
            let stats = self.latency_stats.read().await;
            for (label, stat) in stats.iter() {
                output.push_str(&format!(
                    "# HELP oxirs_cluster_{}_latency_us Latency in microseconds\n",
                    label
                ));
                output.push_str(&format!(
                    "# TYPE oxirs_cluster_{}_latency_us summary\n",
                    label
                ));

                // Quantiles
                output.push_str(&format!(
                    "oxirs_cluster_{}_latency_us{{node_id=\"{}\",quantile=\"0.5\"}} {}\n",
                    label,
                    self.node_id,
                    stat.percentile(50.0)
                ));
                output.push_str(&format!(
                    "oxirs_cluster_{}_latency_us{{node_id=\"{}\",quantile=\"0.9\"}} {}\n",
                    label,
                    self.node_id,
                    stat.percentile(90.0)
                ));
                output.push_str(&format!(
                    "oxirs_cluster_{}_latency_us{{node_id=\"{}\",quantile=\"0.95\"}} {}\n",
                    label,
                    self.node_id,
                    stat.percentile(95.0)
                ));
                output.push_str(&format!(
                    "oxirs_cluster_{}_latency_us{{node_id=\"{}\",quantile=\"0.99\"}} {}\n",
                    label,
                    self.node_id,
                    stat.percentile(99.0)
                ));
                output.push_str(&format!(
                    "oxirs_cluster_{}_latency_us_sum{{node_id=\"{}\"}} {}\n",
                    label, self.node_id, stat.sum
                ));
                output.push_str(&format!(
                    "oxirs_cluster_{}_latency_us_count{{node_id=\"{}\"}} {}\n",
                    label, self.node_id, stat.count
                ));
            }
        }

        output
    }

    /// Generate comprehensive metrics report
    pub async fn generate_report(&self) -> String {
        let metrics = self.get_all_metrics().await;

        let mut report = format!("=== Cluster Metrics Report (Node {}) ===\n", self.node_id);
        report.push_str(&format!(
            "Generated: {}\n\n",
            chrono::Utc::now().to_rfc3339()
        ));

        for metric in metrics {
            report.push_str(&format!(
                "Operation: {}\n\
                 - Count: {} operations\n\
                 - Mean: {:.2}ms (±{:.2}ms, CV={:.2}%)\n\
                 - Percentiles: p50={:.2}ms, p90={:.2}ms, p95={:.2}ms, p99={:.2}ms\n\
                 - Range: [{:.2}ms - {:.2}ms], IQR={:.2}ms\n\
                 - Distribution: skewness={:.3}, kurtosis={:.3}\n\
                 - Trend: {:.4}μs/sample, EMA={:.2}ms\n\
                 - Throughput: {:.2} ops/sec\n\n",
                metric.operation,
                metric.count,
                metric.mean_micros / 1000.0,
                metric.std_dev_micros / 1000.0,
                metric.cv * 100.0,
                metric.p50_micros / 1000.0,
                metric.p90_micros / 1000.0,
                metric.p95_micros / 1000.0,
                metric.p99_micros / 1000.0,
                metric.min_micros / 1000.0,
                metric.max_micros / 1000.0,
                metric.iqr_micros / 1000.0,
                metric.skewness,
                metric.kurtosis,
                metric.trend,
                metric.ema_micros / 1000.0,
                metric.rate_ops_per_sec,
            ));
        }

        report
    }

    /// Establish baseline for an operation
    pub async fn establish_baseline(&self, operation: ClusterOperation) -> Result<(), String> {
        let label = operation.as_str().to_string();
        let stats = self.latency_stats.read().await;

        let stat = stats
            .get(&label)
            .ok_or_else(|| format!("No metrics available for operation {}", operation.as_str()))?;

        if stat.values.len() < 30 {
            return Err(format!(
                "Insufficient samples for baseline (need 30, have {})",
                stat.values.len()
            ));
        }

        let baseline = OperationBaseline {
            operation: label.clone(),
            mean_micros: stat.mean(),
            std_dev_micros: stat.std_dev(),
            p50_micros: stat.percentile(50.0),
            p95_micros: stat.percentile(95.0),
            p99_micros: stat.percentile(99.0),
            sample_size: stat.values.len(),
            timestamp: SystemTime::now(),
        };

        let mut baselines = self.baselines.write().await;
        baselines.insert(label, baseline);

        info!(
            "Established baseline for {} on node {}",
            operation.as_str(),
            self.node_id
        );

        Ok(())
    }

    /// Detect performance regressions
    pub async fn detect_regressions(&self) -> Vec<PerformanceRegression> {
        let mut regressions = Vec::new();
        let stats = self.latency_stats.read().await;
        let baselines = self.baselines.read().await;

        for (label, baseline) in baselines.iter() {
            if let Some(current) = stats.get(label) {
                // Perform statistical tests for regression detection

                // 1. Mean comparison with t-test
                if current.values.len() >= 10 {
                    let _current_values: &[f64] = &current.values;
                    let baseline_mean = baseline.mean_micros;
                    let baseline_std = baseline.std_dev_micros;

                    // Calculate t-statistic
                    let current_mean = current.mean();
                    let current_std = current.std_dev();
                    let n = current.values.len() as f64;

                    // Welch's t-test for unequal variances
                    let se = ((current_std.powi(2) / n)
                        + (baseline_std.powi(2) / baseline.sample_size as f64))
                        .sqrt();

                    if se > 0.0 {
                        let t_stat = (current_mean - baseline_mean) / se;

                        // Use SciRS2 for statistical testing
                        let df = n - 1.0;
                        if df > 0.0 {
                            // Calculate p-value using Student's t distribution
                            let t_dist = StudentT::new(0.0, 1.0, df);
                            let p_value = if let Ok(dist) = t_dist {
                                // One-tailed test for regression (slower)
                                // Use StudentT's cdf method directly
                                1.0 - dist.cdf(t_stat)
                            } else {
                                1.0 // Assume no regression if distribution fails
                            };

                            let change_pct =
                                ((current_mean - baseline_mean) / baseline_mean) * 100.0;

                            // Significant regression if p < 0.05 and >10% slower
                            if p_value < 0.05 && change_pct > 10.0 {
                                regressions.push(PerformanceRegression {
                                    operation: label.clone(),
                                    metric_name: "mean_latency".to_string(),
                                    baseline_value: baseline_mean,
                                    current_value: current_mean,
                                    change_percentage: change_pct,
                                    p_value,
                                    t_statistic: t_stat,
                                    severity: if change_pct > 100.0 {
                                        RegressionSeverity::Critical
                                    } else if change_pct > 50.0 {
                                        RegressionSeverity::High
                                    } else if change_pct > 25.0 {
                                        RegressionSeverity::Medium
                                    } else {
                                        RegressionSeverity::Low
                                    },
                                    detection_method: "Welch's t-test".to_string(),
                                });
                            }
                        }
                    }
                }

                // 2. Check p99 latency regression
                let current_p99 = current.percentile(99.0);
                let p99_change =
                    ((current_p99 - baseline.p99_micros) / baseline.p99_micros) * 100.0;

                if p99_change > 25.0 {
                    regressions.push(PerformanceRegression {
                        operation: label.clone(),
                        metric_name: "p99_latency".to_string(),
                        baseline_value: baseline.p99_micros,
                        current_value: current_p99,
                        change_percentage: p99_change,
                        p_value: 0.0,
                        t_statistic: 0.0,
                        severity: if p99_change > 100.0 {
                            RegressionSeverity::Critical
                        } else if p99_change > 50.0 {
                            RegressionSeverity::High
                        } else {
                            RegressionSeverity::Medium
                        },
                        detection_method: "Percentile comparison".to_string(),
                    });
                }

                // 3. Check trend (increasing latency over time)
                let trend = current.trend();
                if trend > baseline.std_dev_micros * 0.1 {
                    regressions.push(PerformanceRegression {
                        operation: label.clone(),
                        metric_name: "latency_trend".to_string(),
                        baseline_value: 0.0,
                        current_value: trend,
                        change_percentage: (trend / baseline.mean_micros) * 100.0,
                        p_value: 0.0,
                        t_statistic: 0.0,
                        severity: RegressionSeverity::Low,
                        detection_method: "Trend analysis".to_string(),
                    });
                }
            }
        }

        if !regressions.is_empty() {
            warn!(
                "Detected {} performance regressions on node {}",
                regressions.len(),
                self.node_id
            );
        }

        regressions
    }

    /// Run benchmarks for cluster operations
    pub async fn run_benchmarks(&self) -> Vec<BenchmarkResultRecord> {
        let mut results = Vec::new();

        info!("Running cluster benchmarks on node {}", self.node_id);

        // Benchmark various operations with synthetic workloads
        let operations_to_benchmark = vec![
            ClusterOperation::AppendEntries,
            ClusterOperation::QueryExecution,
            ClusterOperation::BatchProcessing,
            ClusterOperation::DataReplication,
            ClusterOperation::MerkleVerification,
        ];

        for operation in operations_to_benchmark {
            let result = self.benchmark_operation(operation, 1000).await;
            results.push(result);
        }

        // Store results
        let mut stored = self.benchmark_results.write().await;
        stored.extend(results.clone());

        info!(
            "Completed {} benchmarks on node {}",
            results.len(),
            self.node_id
        );

        results
    }

    /// Benchmark a specific operation
    async fn benchmark_operation(
        &self,
        operation: ClusterOperation,
        iterations: u64,
    ) -> BenchmarkResultRecord {
        let mut latencies = Vec::with_capacity(iterations as usize);

        for _ in 0..iterations {
            let start = Instant::now();
            // Simulate operation workload
            std::hint::black_box(self.simulate_operation_workload(operation));
            latencies.push(start.elapsed().as_nanos() as f64);
        }

        let sum: f64 = latencies.iter().sum();
        let mean = sum / iterations as f64;
        let variance: f64 =
            latencies.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / iterations as f64;
        let std_dev = variance.sqrt();

        BenchmarkResultRecord {
            name: format!("cluster_{}", operation.as_str()),
            timestamp: SystemTime::now(),
            mean_ns: mean,
            std_dev_ns: std_dev,
            iterations,
            throughput: 1_000_000_000.0 / mean,
        }
    }

    /// Simulate operation workload for benchmarking
    fn simulate_operation_workload(&self, operation: ClusterOperation) -> u64 {
        // Simulate CPU-bound work based on operation type
        match operation {
            ClusterOperation::AppendEntries => {
                // Simulate log entry serialization
                let mut sum: u64 = 0;
                for i in 0..100 {
                    sum = sum.wrapping_add(i * i);
                }
                sum
            }
            ClusterOperation::QueryExecution => {
                // Simulate query parsing and planning
                let mut sum: u64 = 0;
                for i in 0..500 {
                    sum = sum.wrapping_add(i * i * i);
                }
                sum
            }
            ClusterOperation::BatchProcessing => {
                // Simulate batch aggregation
                let mut sum: u64 = 0;
                for i in 0..200 {
                    sum = sum.wrapping_add(i);
                }
                sum
            }
            ClusterOperation::DataReplication => {
                // Simulate data serialization
                let mut sum: u64 = 0;
                for i in 0..300 {
                    sum = sum.wrapping_add(i * 7);
                }
                sum
            }
            ClusterOperation::MerkleVerification => {
                // Simulate hash computation
                let mut sum: u64 = 0;
                for i in 0u64..150 {
                    sum = sum.wrapping_add(i.wrapping_mul(i));
                }
                sum
            }
            _ => 0,
        }
    }

    /// Get benchmark history
    pub async fn get_benchmark_history(&self) -> Vec<BenchmarkResultRecord> {
        self.benchmark_results.read().await.clone()
    }

    /// Compare benchmarks between runs
    pub async fn compare_benchmarks(
        &self,
        baseline_name: &str,
        current_name: &str,
    ) -> Option<BenchmarkComparison> {
        let results = self.benchmark_results.read().await;

        let baseline = results.iter().find(|r| r.name == baseline_name)?;
        let current = results.iter().rev().find(|r| r.name == current_name)?;

        let speedup = baseline.mean_ns / current.mean_ns;
        let throughput_improvement =
            ((current.throughput - baseline.throughput) / baseline.throughput) * 100.0;

        Some(BenchmarkComparison {
            baseline_name: baseline_name.to_string(),
            current_name: current_name.to_string(),
            baseline_mean_ns: baseline.mean_ns,
            current_mean_ns: current.mean_ns,
            speedup,
            throughput_improvement,
            is_improved: current.mean_ns < baseline.mean_ns,
        })
    }
}