qudag_protocol/
metrics.rs

1use parking_lot::RwLock;
2use std::sync::atomic::{AtomicU64, Ordering};
3use std::sync::Arc;
4use std::time::{Duration, Instant};
5
6/// Performance metrics for the QuDAG protocol
7pub struct ProtocolMetrics {
8    // Cryptographic metrics
9    pub crypto_operations: AtomicU64,
10    pub key_cache_hits: AtomicU64,
11    pub key_cache_misses: AtomicU64,
12
13    // Network metrics
14    pub messages_processed: AtomicU64,
15    pub active_connections: AtomicU64,
16    pub connection_errors: AtomicU64,
17    pub route_cache_hits: AtomicU64,
18
19    // Consensus metrics
20    pub consensus_rounds: AtomicU64,
21    pub dag_updates: AtomicU64,
22    pub node_count: AtomicU64,
23
24    // Resource metrics
25    pub memory_usage: AtomicU64,
26    pub thread_count: AtomicU64,
27    pub queue_depth: AtomicU64,
28
29    // Last update timestamp
30    last_update: Arc<RwLock<Instant>>,
31    update_interval: Duration,
32}
33
34impl Default for ProtocolMetrics {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl ProtocolMetrics {
41    /// Create new metrics instance
42    pub fn new() -> Self {
43        Self {
44            // Crypto metrics
45            crypto_operations: AtomicU64::new(0),
46            key_cache_hits: AtomicU64::new(0),
47            key_cache_misses: AtomicU64::new(0),
48
49            // Network metrics
50            messages_processed: AtomicU64::new(0),
51            active_connections: AtomicU64::new(0),
52            connection_errors: AtomicU64::new(0),
53            route_cache_hits: AtomicU64::new(0),
54
55            // Consensus metrics
56            consensus_rounds: AtomicU64::new(0),
57            dag_updates: AtomicU64::new(0),
58            node_count: AtomicU64::new(0),
59
60            // Resource metrics
61            memory_usage: AtomicU64::new(0),
62            thread_count: AtomicU64::new(0),
63            queue_depth: AtomicU64::new(0),
64
65            // Update tracking
66            last_update: Arc::new(RwLock::new(Instant::now())),
67            update_interval: Duration::from_secs(1),
68        }
69    }
70
71    /// Record cryptographic operation
72    pub fn record_crypto_op(&self, _latency: Duration) {
73        self.crypto_operations.fetch_add(1, Ordering::Relaxed);
74        self.maybe_flush_metrics();
75    }
76
77    /// Record message processing
78    pub fn record_message(&self, _latency: Duration) {
79        self.messages_processed.fetch_add(1, Ordering::Relaxed);
80        self.maybe_flush_metrics();
81    }
82
83    /// Record consensus round
84    pub fn record_consensus(&self, _latency: Duration) {
85        self.consensus_rounds.fetch_add(1, Ordering::Relaxed);
86        self.maybe_flush_metrics();
87    }
88
89    /// Update resource metrics
90    pub fn update_resources(&self, memory: u64, threads: u64, queue: u64) {
91        self.memory_usage.store(memory, Ordering::Relaxed);
92        self.thread_count.store(threads, Ordering::Relaxed);
93        self.queue_depth.store(queue, Ordering::Relaxed);
94        self.maybe_flush_metrics();
95    }
96
97    /// Get performance summary
98    pub fn get_summary(&self) -> PerformanceSummary {
99        PerformanceSummary {
100            messages_per_second: self.messages_processed.load(Ordering::Relaxed) as f64
101                / self.last_update.read().elapsed().as_secs_f64(),
102            avg_message_latency: 0.0, // TODO: Implement proper latency tracking
103            avg_consensus_latency: 0.0, // TODO: Implement proper latency tracking
104            active_connections: self.active_connections.load(Ordering::Relaxed),
105            memory_usage: self.memory_usage.load(Ordering::Relaxed),
106        }
107    }
108
109    // Flush metrics if update interval elapsed
110    fn maybe_flush_metrics(&self) {
111        let mut last_update = self.last_update.write();
112        if last_update.elapsed() >= self.update_interval {
113            *last_update = Instant::now();
114        }
115    }
116}
117
118/// Performance summary
119#[derive(Debug, Clone)]
120pub struct PerformanceSummary {
121    pub messages_per_second: f64,
122    pub avg_message_latency: f64,
123    pub avg_consensus_latency: f64,
124    pub active_connections: u64,
125    pub memory_usage: u64,
126}