Skip to main content

oxirs_stream/performance_optimizer/
config.rs

1//! Performance optimization configuration
2//!
3//! This module provides configuration structures for various performance optimization
4//! features including adaptive batching, memory pooling, zero-copy operations, and parallel processing.
5
6use serde::{Deserialize, Serialize};
7
8/// Performance optimization configuration
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct PerformanceConfig {
11    /// Enable adaptive batching
12    pub enable_adaptive_batching: bool,
13    /// Maximum batch size for processing
14    pub max_batch_size: usize,
15    /// Target latency for adaptive batching
16    pub target_latency_ms: u64,
17    /// Enable memory pooling
18    pub enable_memory_pooling: bool,
19    /// Memory pool size
20    pub memory_pool_size: usize,
21    /// Enable zero-copy optimizations
22    pub enable_zero_copy: bool,
23    /// Enable parallel processing
24    pub enable_parallel_processing: bool,
25    /// Number of parallel workers
26    pub parallel_workers: usize,
27    /// Enable event pre-filtering
28    pub enable_event_filtering: bool,
29    /// Enable compression
30    pub enable_compression: bool,
31    /// Compression threshold (bytes)
32    pub compression_threshold: usize,
33    /// Enable adaptive compression based on network conditions
34    pub enable_adaptive_compression: bool,
35    /// Network bandwidth estimation (bytes/sec)
36    pub estimated_bandwidth: u64,
37}
38
39impl Default for PerformanceConfig {
40    fn default() -> Self {
41        Self {
42            enable_adaptive_batching: true,
43            max_batch_size: 1000,
44            target_latency_ms: 10,
45            enable_memory_pooling: true,
46            memory_pool_size: 1024 * 1024 * 10, // 10MB
47            enable_zero_copy: true,
48            enable_parallel_processing: true,
49            parallel_workers: num_cpus::get().max(1),
50            enable_event_filtering: true,
51            enable_compression: true,
52            compression_threshold: 1024, // 1KB
53            enable_adaptive_compression: true,
54            estimated_bandwidth: 100 * 1024 * 1024, // 100MB/s
55        }
56    }
57}
58
59/// Enhanced ML configuration for performance prediction
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct EnhancedMLConfig {
62    /// Enable enhanced ML features
63    pub enable_enhanced_ml: bool,
64    /// Learning rate for neural network
65    pub learning_rate: f64,
66    /// Number of training epochs
67    pub training_epochs: usize,
68    /// Batch size for ML training
69    pub ml_batch_size: usize,
70    /// Neural network hidden layer size
71    pub hidden_layer_size: usize,
72    /// Enable feature engineering
73    pub enable_feature_engineering: bool,
74    /// Enable polynomial features
75    pub enable_polynomial_features: bool,
76    /// Polynomial degree for feature engineering
77    pub polynomial_degree: usize,
78    /// Enable interaction features
79    pub enable_interaction_features: bool,
80    /// Enable temporal features
81    pub enable_temporal_features: bool,
82    /// Enable statistical features
83    pub enable_statistical_features: bool,
84    /// Enable model selection
85    pub enable_model_selection: bool,
86    /// Cross-validation folds
87    pub cv_folds: usize,
88    /// Enable feature scaling
89    pub enable_feature_scaling: bool,
90    /// Model performance tracking window
91    pub performance_window: usize,
92}
93
94impl Default for EnhancedMLConfig {
95    fn default() -> Self {
96        Self {
97            enable_enhanced_ml: true,
98            learning_rate: 0.01,
99            training_epochs: 100,
100            ml_batch_size: 32,
101            hidden_layer_size: 64,
102            enable_feature_engineering: true,
103            enable_polynomial_features: true,
104            polynomial_degree: 2,
105            enable_interaction_features: true,
106            enable_temporal_features: true,
107            enable_statistical_features: true,
108            enable_model_selection: true,
109            cv_folds: 5,
110            enable_feature_scaling: true,
111            performance_window: 1000,
112        }
113    }
114}
115
116/// Configuration for batch size prediction
117#[derive(Debug, Clone, Serialize, Deserialize)]
118pub struct BatchConfig {
119    /// Initial batch size
120    pub initial_batch_size: usize,
121    /// Minimum batch size
122    pub min_batch_size: usize,
123    /// Maximum batch size
124    pub max_batch_size: usize,
125    /// Batch size adjustment factor
126    pub adjustment_factor: f64,
127    /// Target latency for batch processing
128    pub target_latency_ms: u64,
129    /// Latency tolerance
130    pub latency_tolerance_ms: u64,
131}
132
133impl Default for BatchConfig {
134    fn default() -> Self {
135        Self {
136            initial_batch_size: 100,
137            min_batch_size: 10,
138            max_batch_size: 1000,
139            adjustment_factor: 1.2,
140            target_latency_ms: 10,
141            latency_tolerance_ms: 2,
142        }
143    }
144}
145
146/// Configuration for memory pool
147#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct MemoryPoolConfig {
149    /// Initial pool size
150    pub initial_size: usize,
151    /// Maximum pool size
152    pub max_size: usize,
153    /// Growth factor when expanding pool
154    pub growth_factor: f64,
155    /// Shrink threshold (percentage of unused memory)
156    pub shrink_threshold: f64,
157    /// Enable memory compaction
158    pub enable_compaction: bool,
159    /// Compaction interval (seconds)
160    pub compaction_interval: u64,
161}
162
163impl Default for MemoryPoolConfig {
164    fn default() -> Self {
165        Self {
166            initial_size: 1024 * 1024,   // 1MB
167            max_size: 100 * 1024 * 1024, // 100MB
168            growth_factor: 1.5,
169            shrink_threshold: 0.7,
170            enable_compaction: true,
171            compaction_interval: 60,
172        }
173    }
174}
175
176/// Configuration for parallel processing
177#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct ParallelConfig {
179    /// Number of worker threads
180    pub worker_threads: usize,
181    /// Queue capacity per worker
182    pub queue_capacity: usize,
183    /// Enable work stealing
184    pub enable_work_stealing: bool,
185    /// Load balancing strategy
186    pub load_balancing: LoadBalancingStrategy,
187    /// Enable thread pinning
188    pub enable_thread_pinning: bool,
189}
190
191/// Load balancing strategies for parallel processing
192#[derive(Debug, Clone, Serialize, Deserialize)]
193pub enum LoadBalancingStrategy {
194    RoundRobin,
195    LeastLoaded,
196    Random,
197    Weighted(Vec<f64>),
198}
199
200impl Default for ParallelConfig {
201    fn default() -> Self {
202        Self {
203            worker_threads: num_cpus::get().max(1),
204            queue_capacity: 1000,
205            enable_work_stealing: true,
206            load_balancing: LoadBalancingStrategy::LeastLoaded,
207            enable_thread_pinning: false,
208        }
209    }
210}
211
212/// Configuration for compression
213#[derive(Debug, Clone, Serialize, Deserialize)]
214pub struct CompressionConfig {
215    /// Enable compression
216    pub enable_compression: bool,
217    /// Compression algorithm
218    pub algorithm: CompressionAlgorithm,
219    /// Compression level (0-9)
220    pub level: u32,
221    /// Compression threshold (bytes)
222    pub threshold: usize,
223    /// Enable adaptive compression
224    pub enable_adaptive: bool,
225    /// Bandwidth threshold for compression (bytes/sec)
226    pub bandwidth_threshold: u64,
227}
228
229/// Compression algorithms
230#[derive(Debug, Clone, Serialize, Deserialize)]
231pub enum CompressionAlgorithm {
232    Gzip,
233    Zstd,
234    Lz4,
235    Snappy,
236    Brotli,
237}
238
239impl Default for CompressionConfig {
240    fn default() -> Self {
241        Self {
242            enable_compression: true,
243            algorithm: CompressionAlgorithm::Zstd,
244            level: 3,
245            threshold: 1024, // 1KB
246            enable_adaptive: true,
247            bandwidth_threshold: 10 * 1024 * 1024, // 10MB/s
248        }
249    }
250}