rustkmer 0.5.2

High-performance k-mer counting tool in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Performance monitoring infrastructure with conditional compilation
//!
//! This module provides low-overhead performance monitoring that can be
//! enabled/disabled at compile time using the "profiling" feature flag.

#[cfg(feature = "profiling")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "profiling")]
use std::sync::{Arc, Mutex};
#[cfg(feature = "profiling")]
use std::time::{Duration, Instant};

/// Performance monitoring configuration
#[cfg(feature = "profiling")]
#[derive(Debug, Clone)]
pub struct MonitoringConfig {
    pub enabled: bool,
    pub track_memory: bool,
    pub track_timing: bool,
    pub track_operations: bool,
    pub max_samples: usize,
}

#[cfg(feature = "profiling")]
impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            track_memory: true,
            track_timing: true,
            track_operations: true,
            max_samples: 10_000,
        }
    }
}

/// Performance monitoring configuration (non-profiling stub)
#[cfg(not(feature = "profiling"))]
#[derive(Debug, Clone)]
pub struct MonitoringConfig {
    pub enabled: bool,
}

#[cfg(not(feature = "profiling"))]
impl Default for MonitoringConfig {
    fn default() -> Self {
        Self { enabled: false }
    }
}

/// Core performance metrics structure
#[cfg(feature = "profiling")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationMetrics {
    /// Operation name (e.g., "kmer_counting", "database_query")
    pub operation_name: String,
    /// Total duration of the operation
    pub duration: Duration,
    /// Memory usage statistics (in bytes)
    pub memory_stats: MemoryUsage,
    /// Number of items processed (k-mers, sequences, etc.)
    pub items_processed: u64,
    /// Number of successful operations
    pub successful_operations: u64,
    /// Number of failed operations
    pub failed_operations: u64,
    /// Additional custom metrics
    pub custom_metrics: std::collections::HashMap<String, f64>,
}

#[cfg(feature = "profiling")]
impl OperationMetrics {
    pub fn new(operation_name: String) -> Self {
        Self {
            operation_name,
            duration: Duration::default(),
            memory_stats: MemoryUsage::default(),
            items_processed: 0,
            successful_operations: 0,
            failed_operations: 0,
            custom_metrics: std::collections::HashMap::new(),
        }
    }

    pub fn add_custom_metric(&mut self, key: String, value: f64) {
        self.custom_metrics.insert(key, value);
    }

    pub fn success_rate(&self) -> f64 {
        let total = self.successful_operations + self.failed_operations;
        if total == 0 {
            0.0
        } else {
            self.successful_operations as f64 / total as f64
        }
    }

    pub fn items_per_second(&self) -> f64 {
        if self.duration.as_secs_f64() > 0.0 {
            self.items_processed as f64 / self.duration.as_secs_f64()
        } else {
            0.0
        }
    }
}

/// Memory usage tracking structure
#[cfg(feature = "profiling")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryUsage {
    /// Peak memory usage in bytes
    pub peak_bytes: u64,
    /// Current memory usage in bytes
    pub current_bytes: u64,
    /// Total allocations in bytes
    pub total_allocated: u64,
    /// Total deallocations in bytes
    pub total_deallocated: u64,
}

#[cfg(feature = "profiling")]
impl Default for MemoryUsage {
    fn default() -> Self {
        Self {
            peak_bytes: 0,
            current_bytes: 0,
            total_allocated: 0,
            total_deallocated: 0,
        }
    }
}

#[cfg(feature = "profiling")]
impl MemoryUsage {
    pub fn update_allocation(&mut self, size: u64) {
        self.current_bytes += size;
        self.total_allocated += size;
        if self.current_bytes > self.peak_bytes {
            self.peak_bytes = self.current_bytes;
        }
    }

    pub fn update_deallocation(&mut self, size: u64) {
        self.current_bytes = self.current_bytes.saturating_sub(size);
        self.total_deallocated += size;
    }
}

/// Performance timer for measuring operation durations
#[cfg(feature = "profiling")]
pub struct PerformanceTimer {
    start_time: Instant,
    operation_name: String,
    metrics_collector: Option<Arc<Mutex<MetricsCollector>>>,
}

#[cfg(feature = "profiling")]
impl PerformanceTimer {
    /// Create a new performance timer
    pub fn new(operation_name: String) -> Self {
        log::debug!("Starting performance timer for: {}", operation_name);
        Self {
            start_time: Instant::now(),
            operation_name,
            metrics_collector: None,
        }
    }

    /// Create a new performance timer with metrics collector
    pub fn with_collector(operation_name: String, collector: Arc<Mutex<MetricsCollector>>) -> Self {
        log::debug!(
            "Starting performance timer for: {} (with collection)",
            operation_name
        );
        Self {
            start_time: Instant::now(),
            operation_name,
            metrics_collector: Some(collector),
        }
    }

    /// Finish timing and record the duration
    pub fn finish(self) -> Duration {
        let duration = self.start_time.elapsed();
        log::debug!(
            "Operation '{}' completed in {:?}",
            self.operation_name,
            duration
        );

        if let Some(collector) = self.metrics_collector {
            let mut collector = collector.lock().unwrap();
            collector.record_timing(self.operation_name, duration);
        }

        duration
    }

    /// Get elapsed time without finishing
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }
}

#[cfg(feature = "profiling")]
impl Drop for PerformanceTimer {
    fn drop(&mut self) {
        let elapsed = self.elapsed();
        log::debug!(
            "Performance timer for '{}' dropped after {:?}",
            self.operation_name,
            elapsed
        );

        if let Some(collector) = &self.metrics_collector {
            let mut collector = collector.lock().unwrap();
            collector.record_timing(self.operation_name.clone(), elapsed);
        }
    }
}

/// Global metrics collector for aggregating performance data
#[cfg(feature = "profiling")]
#[derive(Debug)]
pub struct MetricsCollector {
    config: MonitoringConfig,
    operations: Vec<OperationMetrics>,
    active_timers: std::collections::HashMap<String, Vec<Duration>>,
    total_samples: usize,
}

#[cfg(feature = "profiling")]
impl MetricsCollector {
    pub fn new(config: MonitoringConfig) -> Self {
        Self {
            config,
            operations: Vec::new(),
            active_timers: std::collections::HashMap::new(),
            total_samples: 0,
        }
    }

    pub fn record_timing(&mut self, operation: String, duration: Duration) {
        if !self.config.enabled {
            return;
        }

        let timings = self
            .active_timers
            .entry(operation.clone())
            .or_insert_with(Vec::new);
        timings.push(duration);

        // Maintain maximum sample limit
        if timings.len() > self.config.max_samples {
            timings.remove(0);
        }

        self.total_samples += 1;

        // Log periodic statistics
        if self.total_samples % 1000 == 0 {
            self.log_summary();
        }
    }

    pub fn record_operation(&mut self, metrics: OperationMetrics) {
        if !self.config.enabled {
            return;
        }

        self.operations.push(metrics);

        // Maintain maximum operation limit
        if self.operations.len() > self.config.max_samples {
            self.operations.remove(0);
        }
    }

    pub fn get_average_timing(&self, operation: &str) -> Option<Duration> {
        if let Some(timings) = self.active_timers.get(operation) {
            if timings.is_empty() {
                return None;
            }
            let total: Duration = timings.iter().sum();
            Some(total / timings.len() as u32)
        } else {
            None
        }
    }

    pub fn get_operation_stats(&self, operation: &str) -> Option<OperationStats> {
        if let Some(timings) = self.active_timers.get(operation) {
            if timings.is_empty() {
                return None;
            }

            let total: Duration = timings.iter().sum();
            let average = total / timings.len() as u32;
            let min = *timings.iter().min().unwrap();
            let max = *timings.iter().max().unwrap();

            Some(OperationStats {
                operation: operation.to_string(),
                count: timings.len(),
                total,
                average,
                min,
                max,
            })
        } else {
            None
        }
    }

    pub fn log_summary(&self) {
        log::info!(
            "Performance Summary - {} samples recorded",
            self.total_samples
        );

        for (operation, timings) in &self.active_timings {
            if let Some(stats) = self.get_operation_stats(operation) {
                log::info!(
                    "  {}: {} ops, avg {:?}, min {:?}, max {:?}",
                    operation,
                    stats.count,
                    stats.average,
                    stats.min,
                    stats.max
                );
            }
        }
    }

    pub fn export_metrics(&self) -> Vec<u8> {
        serde_json::to_vec_pretty(&self.operations).unwrap_or_default()
    }
}

#[cfg(feature = "profiling")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationStats {
    pub operation: String,
    pub count: usize,
    pub total: Duration,
    pub average: Duration,
    pub min: Duration,
    pub max: Duration,
}

/// Global metrics collector instance
#[cfg(feature = "profiling")]
static GLOBAL_COLLECTOR: std::sync::OnceLock<Arc<Mutex<MetricsCollector>>> =
    std::sync::OnceLock::new();

/// Get the global metrics collector
#[cfg(feature = "profiling")]
pub fn get_global_collector() -> Arc<Mutex<MetricsCollector>> {
    GLOBAL_COLLECTOR
        .get_or_init(|| {
            let config = MonitoringConfig::default();
            Arc::new(Mutex::new(MetricsCollector::new(config)))
        })
        .clone()
}

/// Initialize global performance monitoring
#[cfg(feature = "profiling")]
pub fn initialize_monitoring(config: MonitoringConfig) {
    let collector = MetricsCollector::new(config);
    let _ = GLOBAL_COLLECTOR.set(Arc::new(Mutex::new(collector)));
    log::info!("Performance monitoring initialized");
}

/// Start a performance timer for an operation
#[cfg(feature = "profiling")]
pub fn start_timer(operation: &str) -> PerformanceTimer {
    let collector = get_global_collector();
    PerformanceTimer::with_collector(operation.to_string(), collector)
}

/// Record a custom metric
#[cfg(feature = "profiling")]
pub fn record_metric(operation: &str, metric_name: &str, value: f64) {
    let collector = get_global_collector();
    let mut collector = collector.lock().unwrap();

    // Find existing operation metrics or create new one
    if let Some(op_metrics) = collector
        .operations
        .iter_mut()
        .find(|op| op.operation_name == operation)
    {
        op_metrics.add_custom_metric(format!("{}.{}", operation, metric_name), value);
    } else {
        let mut new_metrics = OperationMetrics::new(operation.to_string());
        new_metrics.add_custom_metric(format!("{}.{}", operation, metric_name), value);
        collector.record_operation(new_metrics);
    }
}

// =============================================================================
// Stub implementations when profiling feature is disabled
// =============================================================================

#[cfg(not(feature = "profiling"))]
#[derive(Debug, Clone)]
pub struct OperationMetrics {
    pub operation_name: String,
}

#[cfg(not(feature = "profiling"))]
impl OperationMetrics {
    pub fn new(operation_name: String) -> Self {
        Self { operation_name }
    }
}

#[cfg(not(feature = "profiling"))]
pub struct PerformanceTimer {
    _private: (),
}

#[cfg(not(feature = "profiling"))]
impl PerformanceTimer {
    pub fn new(operation_name: String) -> Self {
        let _ = operation_name; // Suppress unused warning
        Self { _private: () }
    }

    pub fn finish(self) -> std::time::Duration {
        std::time::Duration::default()
    }

    pub fn elapsed(&self) -> std::time::Duration {
        std::time::Duration::default()
    }
}

#[cfg(not(feature = "profiling"))]
pub fn start_timer(_operation: &str) -> PerformanceTimer {
    PerformanceTimer::new(String::new())
}

#[cfg(not(feature = "profiling"))]
pub fn initialize_monitoring(_config: MonitoringConfig) {
    // No-op when profiling is disabled
}

#[cfg(not(feature = "profiling"))]
pub fn record_metric(_operation: &str, _metric_name: &str, _value: f64) {
    // No-op when profiling is disabled
}

/// Convenience macro for timing operations
#[macro_export]
macro_rules! time_operation {
    ($operation:expr, $block:block) => {{
        #[cfg(feature = "profiling")]
        {
            let _timer = $crate::core::monitoring::start_timer($operation);
            $block
        }
        #[cfg(not(feature = "profiling"))]
        {
            $block
        }
    }};
}

/// Convenience macro for conditional monitoring code
#[macro_export]
macro_rules! if_profiling {
    ($block:block) => {{
        #[cfg(feature = "profiling")]
        $block
    }};
}