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