rusty_common/websocket/
stats.rs

1//! WebSocket connection statistics tracking
2//!
3//! Provides performance monitoring and statistics collection for WebSocket connections.
4
5use parking_lot::RwLock;
6use std::sync::Arc;
7
8/// Connection statistics for performance monitoring
9#[derive(Debug, Clone, Default)]
10#[repr(align(64))] // Cache-line alignment for better CPU cache efficiency
11pub struct ConnectionStats {
12    /// Number of messages received
13    pub messages_received: u64,
14
15    /// Number of messages sent
16    pub messages_sent: u64,
17
18    /// Number of successful reconnections
19    pub reconnections: u32,
20
21    /// Total bytes received
22    pub bytes_received: u64,
23
24    /// Total bytes sent
25    pub bytes_sent: u64,
26
27    /// Last message received timestamp (nanoseconds)
28    pub last_message_time: u64,
29
30    /// Last ping timestamp (nanoseconds)
31    pub last_ping_time: u64,
32
33    /// Last pong timestamp (nanoseconds)
34    pub last_pong_time: u64,
35
36    /// Average message processing latency (nanoseconds)
37    pub avg_latency_ns: u64,
38
39    /// Connection established timestamp (nanoseconds)
40    pub connected_time: u64,
41
42    /// Number of errors encountered
43    pub errors: u32,
44}
45
46/// Thread-safe statistics container
47pub type SharedStats = Arc<RwLock<ConnectionStats>>;
48
49/// Create a new shared statistics instance
50pub fn new_shared_stats() -> SharedStats {
51    Arc::new(RwLock::new(ConnectionStats::default()))
52}