codex_memory/performance/
mod.rs

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