codex_memory/performance/
mod.rs

1//! Performance testing and optimization module
2
3pub mod benchmarks;
4pub mod capacity_planning;
5pub mod load_testing;
6pub mod metrics;
7pub mod optimization;
8pub mod stress_testing;
9
10use serde::{Deserialize, Serialize};
11use std::time::Duration;
12
13/// Performance testing configuration
14#[derive(Debug, Clone, Serialize, Deserialize, Default)]
15pub struct PerformanceConfig {
16    /// Enable performance testing
17    pub enabled: bool,
18
19    /// Load testing configuration
20    pub load_testing: LoadTestConfig,
21
22    /// Stress testing configuration
23    pub stress_testing: StressTestConfig,
24
25    /// Performance SLA thresholds
26    pub sla_thresholds: SlaThresholds,
27
28    /// Profiling configuration
29    pub profiling: ProfilingConfig,
30}
31
32/// Load testing configuration
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct LoadTestConfig {
35    /// Number of concurrent users to simulate
36    pub concurrent_users: usize,
37
38    /// Duration of load test
39    pub test_duration: Duration,
40
41    /// Ramp-up time to reach target load
42    pub ramp_up_time: Duration,
43
44    /// Target requests per second
45    pub target_rps: u32,
46}
47
48/// Stress testing configuration
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct StressTestConfig {
51    /// Maximum concurrent connections to test
52    pub max_connections: usize,
53
54    /// Memory pressure threshold (percentage)
55    pub memory_pressure_threshold: u8,
56
57    /// CPU pressure threshold (percentage)
58    pub cpu_pressure_threshold: u8,
59
60    /// Enable chaos testing
61    pub chaos_testing_enabled: bool,
62}
63
64/// SLA performance thresholds
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct SlaThresholds {
67    /// P50 latency threshold (ms)
68    pub p50_latency_ms: u64,
69
70    /// P95 latency threshold (ms)
71    pub p95_latency_ms: u64,
72
73    /// P99 latency threshold (ms)
74    pub p99_latency_ms: u64,
75
76    /// Minimum acceptable throughput (requests/sec)
77    pub min_throughput_rps: u32,
78
79    /// Maximum error rate (percentage)
80    pub max_error_rate: f64,
81
82    /// Cache hit ratio target (percentage)
83    pub cache_hit_ratio_target: f64,
84}
85
86/// Profiling configuration
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct ProfilingConfig {
89    /// Enable CPU profiling
90    pub cpu_profiling: bool,
91
92    /// Enable memory profiling
93    pub memory_profiling: bool,
94
95    /// Enable I/O profiling
96    pub io_profiling: bool,
97
98    /// Sampling rate for profiling (Hz)
99    pub sampling_rate_hz: u32,
100
101    /// Profile output directory
102    pub output_directory: String,
103}
104
105impl Default for LoadTestConfig {
106    fn default() -> Self {
107        Self {
108            concurrent_users: 100,
109            test_duration: Duration::from_secs(300), // 5 minutes
110            ramp_up_time: Duration::from_secs(60),   // 1 minute
111            target_rps: 1000,
112        }
113    }
114}
115
116impl Default for StressTestConfig {
117    fn default() -> Self {
118        Self {
119            max_connections: 10000,
120            memory_pressure_threshold: 80,
121            cpu_pressure_threshold: 90,
122            chaos_testing_enabled: false,
123        }
124    }
125}
126
127impl Default for SlaThresholds {
128    fn default() -> Self {
129        Self {
130            p50_latency_ms: 10,
131            p95_latency_ms: 100,
132            p99_latency_ms: 500,
133            min_throughput_rps: 100,
134            max_error_rate: 1.0,
135            cache_hit_ratio_target: 90.0,
136        }
137    }
138}
139
140impl Default for ProfilingConfig {
141    fn default() -> Self {
142        Self {
143            cpu_profiling: true,
144            memory_profiling: true,
145            io_profiling: true,
146            sampling_rate_hz: 100,
147            output_directory: "./profiles".to_string(),
148        }
149    }
150}
151
152/// Performance test result
153#[derive(Debug, Clone, Serialize, Deserialize)]
154pub struct PerformanceTestResult {
155    pub test_name: String,
156    pub test_type: TestType,
157    pub start_time: chrono::DateTime<chrono::Utc>,
158    pub end_time: chrono::DateTime<chrono::Utc>,
159    pub duration: Duration,
160    pub metrics: PerformanceMetrics,
161    pub sla_violations: Vec<SlaViolation>,
162    pub passed: bool,
163}
164
165/// Type of performance test
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub enum TestType {
168    Load,
169    Stress,
170    Spike,
171    Soak,
172    Capacity,
173}
174
175/// Performance metrics collected during testing
176#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct PerformanceMetrics {
178    pub total_requests: u64,
179    pub successful_requests: u64,
180    pub failed_requests: u64,
181    pub throughput_rps: f64,
182    pub latency_p50_ms: u64,
183    pub latency_p95_ms: u64,
184    pub latency_p99_ms: u64,
185    pub latency_max_ms: u64,
186    pub error_rate: f64,
187    pub cpu_usage_avg: f64,
188    pub memory_usage_avg: f64,
189    pub cache_hit_ratio: f64,
190    pub db_connections_used: u32,
191    pub network_bytes_sent: u64,
192    pub network_bytes_received: u64,
193}
194
195/// SLA violation details
196#[derive(Debug, Clone, Serialize, Deserialize)]
197pub struct SlaViolation {
198    pub metric: String,
199    pub threshold: f64,
200    pub actual_value: f64,
201    pub severity: ViolationSeverity,
202    pub timestamp: chrono::DateTime<chrono::Utc>,
203}
204
205/// Severity of SLA violation
206#[derive(Debug, Clone, Serialize, Deserialize)]
207pub enum ViolationSeverity {
208    Warning,
209    Critical,
210}