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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Performance optimization and metrics for fuzzy queries
//!
//! This module provides performance monitoring, optimization strategies, and
//! resource management for fuzzy query operations.

use crate::fuzzy::{constants, FuzzyError, FuzzyResult};
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

/// Comprehensive performance metrics for fuzzy query operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Variant generation metrics
    pub variant_generation: VariantGenerationMetrics,

    /// Database query metrics
    pub database_queries: DatabaseQueryMetrics,

    /// Memory usage metrics
    pub memory_usage: MemoryUsageMetrics,
}

/// Metrics for variant generation process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariantGenerationMetrics {
    /// Time to generate all variants
    pub total_generation_time_ms: u64,

    /// Number of variants generated
    pub variants_generated: usize,

    /// Generation rate (variants per second)
    pub generation_rate: f64,

    /// Peak memory usage during generation
    pub peak_memory_mb: f64,

    /// Wildcard expansion time
    pub wildcard_expansion_time_ms: u64,

    /// Mutation generation time
    pub mutation_generation_time_ms: u64,

    /// Length normalization time
    pub normalization_time_ms: u64,
}

/// Metrics for database query operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseQueryMetrics {
    /// Total number of database queries executed
    pub total_queries: usize,

    /// Average query time per k-mer lookup
    pub avg_query_time_ms: f64,

    /// Number of queries that returned results
    pub successful_queries: usize,

    /// Database hit rate
    pub hit_rate: f64,

    /// Total query time
    pub total_query_time_ms: u64,

    /// Fastest query time
    pub fastest_query_ms: u64,

    /// Slowest query time
    pub slowest_query_ms: u64,
}

/// Memory usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryUsageMetrics {
    /// Peak memory usage during entire operation
    pub peak_memory_mb: f64,

    /// Memory used for storing variants
    pub variants_memory_mb: f64,

    /// Memory used for result storage
    pub results_memory_mb: f64,

    /// Memory efficiency score (results / memory)
    pub memory_efficiency: f64,

    /// Memory growth rate
    pub memory_growth_rate_mb_per_sec: f64,
}

/// Performance monitor for tracking operations
pub struct PerformanceMonitor {
    start_time: Instant,
    checkpoints: Vec<PerformanceCheckpoint>,
    memory_tracker: MemoryTracker,
    query_tracker: QueryTracker,
}

/// Checkpoint for measuring performance at different stages
#[derive(Debug, Clone)]
struct PerformanceCheckpoint {
    name: String,
    timestamp: Instant,
    #[allow(dead_code)]
    memory_mb: f64,
    #[allow(dead_code)]
    operation_count: usize,
}

/// Memory usage tracker
#[derive(Debug)]
struct MemoryTracker {
    peak_memory_mb: f64,
    current_memory_mb: f64,
    start_memory_mb: f64,
}

/// Database query tracker
#[derive(Debug)]
struct QueryTracker {
    total_queries: usize,
    successful_queries: usize,
    total_time_ms: u64,
    fastest_query_ms: u64,
    slowest_query_ms: u64,
    query_times: Vec<u64>,
}

impl Default for PerformanceMonitor {
    fn default() -> Self {
        Self::new()
    }
}

impl PerformanceMonitor {
    /// Create a new performance monitor
    pub fn new() -> Self {
        Self {
            start_time: Instant::now(),
            checkpoints: Vec::new(),
            memory_tracker: MemoryTracker {
                peak_memory_mb: 0.0,
                current_memory_mb: get_memory_usage_mb(),
                start_memory_mb: get_memory_usage_mb(),
            },
            query_tracker: QueryTracker {
                total_queries: 0,
                successful_queries: 0,
                total_time_ms: 0,
                fastest_query_ms: u64::MAX,
                slowest_query_ms: 0,
                query_times: Vec::new(),
            },
        }
    }

    /// Add a performance checkpoint
    pub fn checkpoint(&mut self, name: &str) {
        let memory_mb = get_memory_usage_mb();
        self.memory_tracker.current_memory_mb = memory_mb;
        self.memory_tracker.peak_memory_mb = self.memory_tracker.peak_memory_mb.max(memory_mb);

        self.checkpoints.push(PerformanceCheckpoint {
            name: name.to_string(),
            timestamp: Instant::now(),
            memory_mb,
            operation_count: 0,
        });
    }

    /// Record a database query
    pub fn record_query(&mut self, query_time_ms: u64, success: bool) {
        self.query_tracker.total_queries += 1;
        self.query_tracker.total_time_ms += query_time_ms;
        self.query_tracker.query_times.push(query_time_ms);

        if success {
            self.query_tracker.successful_queries += 1;
        }

        self.query_tracker.fastest_query_ms =
            self.query_tracker.fastest_query_ms.min(query_time_ms);
        self.query_tracker.slowest_query_ms =
            self.query_tracker.slowest_query_ms.max(query_time_ms);
    }

    /// Generate comprehensive performance metrics
    pub fn generate_metrics(
        &self,
        variant_count: usize,
        result_count: usize,
    ) -> PerformanceMetrics {
        let total_elapsed = self.start_time.elapsed().as_millis() as u64;

        // Calculate variant generation metrics
        let (wildcard_time, mutation_time, normalization_time) = self.extract_timing_breakdown();
        let generation_rate = if total_elapsed > 0 {
            variant_count as f64 * 1000.0 / total_elapsed as f64
        } else {
            0.0
        };

        let variant_generation = VariantGenerationMetrics {
            total_generation_time_ms: total_elapsed,
            variants_generated: variant_count,
            generation_rate,
            peak_memory_mb: self.memory_tracker.peak_memory_mb,
            wildcard_expansion_time_ms: wildcard_time,
            mutation_generation_time_ms: mutation_time,
            normalization_time_ms: normalization_time,
        };

        // Calculate database query metrics
        let avg_query_time = if self.query_tracker.total_queries > 0 {
            self.query_tracker.total_time_ms as f64 / self.query_tracker.total_queries as f64
        } else {
            0.0
        };

        let hit_rate = if self.query_tracker.total_queries > 0 {
            self.query_tracker.successful_queries as f64 / self.query_tracker.total_queries as f64
        } else {
            0.0
        };

        let database_queries = DatabaseQueryMetrics {
            total_queries: self.query_tracker.total_queries,
            avg_query_time_ms: avg_query_time,
            successful_queries: self.query_tracker.successful_queries,
            hit_rate,
            total_query_time_ms: self.query_tracker.total_time_ms,
            fastest_query_ms: if self.query_tracker.fastest_query_ms == u64::MAX {
                0
            } else {
                self.query_tracker.fastest_query_ms
            },
            slowest_query_ms: self.query_tracker.slowest_query_ms,
        };

        // Calculate memory metrics
        let memory_growth_rate = if total_elapsed > 0 {
            (self.memory_tracker.peak_memory_mb - self.memory_tracker.start_memory_mb) * 1000.0
                / total_elapsed as f64
        } else {
            0.0
        };

        let memory_usage = MemoryUsageMetrics {
            peak_memory_mb: self.memory_tracker.peak_memory_mb,
            variants_memory_mb: (variant_count as f64 * 13.0) / 1024.0 / 1024.0, // Rough estimate
            results_memory_mb: (result_count as f64 * 32.0) / 1024.0 / 1024.0,   // Rough estimate
            memory_efficiency: if self.memory_tracker.peak_memory_mb > 0.0 {
                result_count as f64 / self.memory_tracker.peak_memory_mb
            } else {
                0.0
            },
            memory_growth_rate_mb_per_sec: memory_growth_rate,
        };

        PerformanceMetrics {
            variant_generation,
            database_queries,
            memory_usage,
        }
    }

    /// Extract timing breakdown from checkpoints
    fn extract_timing_breakdown(&self) -> (u64, u64, u64) {
        let mut wildcard_time = 0u64;
        let mut mutation_time = 0u64;
        let mut normalization_time = 0u64;

        let mut prev_time = self.start_time;

        for checkpoint in &self.checkpoints {
            let elapsed = checkpoint.timestamp.duration_since(prev_time).as_millis() as u64;

            match checkpoint.name.as_str() {
                name if name.contains("wildcard") => wildcard_time += elapsed,
                name if name.contains("mutation") => mutation_time += elapsed,
                name if name.contains("normalization") => normalization_time += elapsed,
                _ => {}
            }

            prev_time = checkpoint.timestamp;
        }

        (wildcard_time, mutation_time, normalization_time)
    }
}

/// Get current memory usage in MB
fn get_memory_usage_mb() -> f64 {
    // This is a simplified implementation
    // In a real scenario, you would use platform-specific APIs
    // or crates like `memory-stats` for accurate measurement
    0.0 // Placeholder
}

/// Performance configuration for optimization
#[derive(Debug, Clone)]
pub struct PerformanceConfig {
    /// Enable parallel processing
    pub enable_parallel: bool,

    /// Number of worker threads (None = auto-detect)
    pub worker_threads: Option<usize>,

    /// Batch size for processing
    pub batch_size: usize,

    /// Memory limit in MB
    pub memory_limit_mb: f64,

    /// Enable performance monitoring
    pub enable_monitoring: bool,

    /// Timeout in seconds
    pub timeout_seconds: u64,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            enable_parallel: false, // Always sequential processing
            worker_threads: None,
            batch_size: constants::DEFAULT_BATCH_SIZE,
            memory_limit_mb: constants::DEFAULT_MEMORY_LIMIT_MB,
            enable_monitoring: true,
            timeout_seconds: constants::DEFAULT_TIMEOUT_SECONDS,
        }
    }
}

/// Performance optimizer for fuzzy queries
pub struct PerformanceOptimizer {
    config: PerformanceConfig,
    monitor: Option<PerformanceMonitor>,
}

impl PerformanceOptimizer {
    /// Create a new performance optimizer
    pub fn new(config: PerformanceConfig) -> Self {
        let monitor = if config.enable_monitoring {
            Some(PerformanceMonitor::new())
        } else {
            None
        };

        Self { config, monitor }
    }

    /// Get optimal batch size based on system characteristics
    pub fn optimal_batch_size(&self, variant_count: usize) -> usize {
        let base_size = self.config.batch_size;

        // Adjust based on variant count
        if variant_count < 100 {
            base_size.min(variant_count)
        } else if variant_count < 1000 {
            base_size
        } else if variant_count < 10000 {
            base_size * 2
        } else {
            base_size * 4
        }
    }

    /// Get optimal number of worker threads
    pub fn optimal_thread_count(&self, _variant_count: usize) -> usize {
        // Always use sequential processing (1 thread)
        1
    }

    /// Check memory constraints
    pub fn check_memory_constraints(&self, variant_count: usize) -> FuzzyResult<()> {
        let estimated_memory_mb = (variant_count as f64 * 64.0) / 1024.0 / 1024.0; // Rough estimate

        if estimated_memory_mb > self.config.memory_limit_mb {
            return Err(FuzzyError::MemoryLimitExceeded {
                usage_mb: estimated_memory_mb,
                limit_mb: self.config.memory_limit_mb,
            });
        }

        Ok(())
    }

    /// Validate performance configuration
    pub fn validate_config(&self) -> FuzzyResult<()> {
        if self.config.batch_size == 0 {
            return Err(FuzzyError::InvalidParameters(
                "Batch size must be greater than 0".to_string(),
            ));
        }

        if self.config.memory_limit_mb <= 0.0 {
            return Err(FuzzyError::InvalidParameters(
                "Memory limit must be greater than 0".to_string(),
            ));
        }

        if self.config.timeout_seconds == 0 {
            return Err(FuzzyError::InvalidParameters(
                "Timeout must be greater than 0".to_string(),
            ));
        }

        Ok(())
    }

    /// Get performance monitor reference
    pub fn monitor(&mut self) -> Option<&mut PerformanceMonitor> {
        self.monitor.as_mut()
    }

    /// Take the performance monitor (consumes it)
    pub fn take_monitor(&mut self) -> Option<PerformanceMonitor> {
        self.monitor.take()
    }
}

/// Performance optimization utilities
pub mod utils {
    use super::*;

    /// Estimate query time based on database size and variant count
    pub fn estimate_query_time(
        database_size: u64,
        variant_count: usize,
        use_parallel: bool,
    ) -> Duration {
        let base_time_per_query_ms = if database_size < 1_000_000 {
            1.0 // Small database
        } else if database_size < 100_000_000 {
            5.0 // Medium database
        } else {
            20.0 // Large database
        };

        let total_time_ms = variant_count as f64 * base_time_per_query_ms;

        // Apply parallel speedup factor
        let speedup_factor = if use_parallel { 4.0 } else { 1.0 };
        Duration::from_millis((total_time_ms / speedup_factor) as u64)
    }

    /// Determine if query should be aborted based on performance
    pub fn should_abort_query(
        elapsed_time: Duration,
        _variant_count: usize,
        timeout_seconds: u64,
    ) -> bool {
        elapsed_time.as_secs() > timeout_seconds
    }

    /// Optimize variant processing order for better cache performance
    pub fn optimize_variant_order(variants: &mut [String]) {
        // Sort by first character for better cache locality
        variants.sort();

        // TODO: Implement more sophisticated ordering based on database structure
    }

    /// Calculate optimal chunk size for parallel processing
    pub fn calculate_optimal_chunk_size(
        total_items: usize,
        num_threads: usize,
        min_chunk_size: usize,
    ) -> usize {
        let base_chunk_size = total_items / num_threads;
        base_chunk_size.max(min_chunk_size)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_performance_config_default() {
        let config = PerformanceConfig::default();
        assert!(!config.enable_parallel); // Always sequential processing
        assert_eq!(config.batch_size, constants::DEFAULT_BATCH_SIZE);
        assert_eq!(config.memory_limit_mb, constants::DEFAULT_MEMORY_LIMIT_MB);
        assert!(config.enable_monitoring);
    }

    #[test]
    fn test_performance_optimizer() {
        let config = PerformanceConfig::default();
        let optimizer = PerformanceOptimizer::new(config);

        assert!(optimizer.validate_config().is_ok());

        // Test batch size optimization
        assert_eq!(optimizer.optimal_batch_size(50), 50);
        assert!(optimizer.optimal_batch_size(5000) >= constants::DEFAULT_BATCH_SIZE);

        // Test thread count optimization (always 1 for sequential processing)
        assert_eq!(optimizer.optimal_thread_count(50), 1);
        assert!(optimizer.optimal_thread_count(10000) >= 1);
    }

    #[test]
    fn test_performance_monitor() {
        let mut monitor = PerformanceMonitor::new();

        monitor.checkpoint("test_checkpoint");
        monitor.record_query(10, true);
        monitor.record_query(5, false);

        let metrics = monitor.generate_metrics(100, 50);
        assert_eq!(metrics.variant_generation.variants_generated, 100);
        assert_eq!(metrics.database_queries.total_queries, 2);
        assert_eq!(metrics.database_queries.successful_queries, 1);
    }

    #[test]
    fn test_utils_estimate_query_time() {
        let small_db_time = utils::estimate_query_time(100_000, 100, false);
        let large_db_time = utils::estimate_query_time(1_000_000_000, 100, false);

        assert!(large_db_time > small_db_time);

        let parallel_time = utils::estimate_query_time(1_000_000_000, 100, true);
        let sequential_time = utils::estimate_query_time(1_000_000_000, 100, false);

        assert!(parallel_time < sequential_time); // Parallel should be faster
    }

    #[test]
    fn test_utils_optimize_variant_order() {
        let mut variants = vec![
            "TTTT".to_string(),
            "AAAA".to_string(),
            "CCCC".to_string(),
            "GGGG".to_string(),
        ];

        utils::optimize_variant_order(&mut variants);

        // Should be sorted alphabetically
        assert_eq!(variants[0], "AAAA");
        assert_eq!(variants[1], "CCCC");
        assert_eq!(variants[2], "GGGG");
        assert_eq!(variants[3], "TTTT");
    }

    #[test]
    fn test_utils_calculate_optimal_chunk_size() {
        let chunk_size = utils::calculate_optimal_chunk_size(1000, 4, 10);
        assert!(chunk_size >= 10);
        assert!(chunk_size <= 1000);
    }

    #[test]
    fn test_should_abort_query() {
        let timeout = 5u64;
        let short_time = Duration::from_secs(1);
        let long_time = Duration::from_secs(10);

        assert!(!utils::should_abort_query(short_time, 100, timeout));
        assert!(utils::should_abort_query(long_time, 100, timeout));
    }
}