Skip to main content

oxirs_vec/
bench_tests.rs

1//! Tests for the advanced benchmarking framework.
2
3#[cfg(test)]
4mod tests {
5    use crate::bench_metrics::{
6        AdvancedBenchmarkConfig, BuildTimeMetrics, IndexSizeMetrics, LatencyMetrics,
7        PerformanceMetrics, ThroughputMetrics,
8    };
9    use crate::bench_runner::{AdvancedBenchmarkSuite, StatisticalAnalyzer};
10    use crate::Vector;
11    use anyhow::Result;
12    use std::collections::HashMap;
13
14    #[test]
15    fn test_advanced_benchmark_config() {
16        let config = AdvancedBenchmarkConfig::new();
17        assert_eq!(config.confidence_level, 0.95);
18        assert_eq!(config.min_runs, 10);
19
20        let ann_config = AdvancedBenchmarkConfig::ann_benchmarks_compatible();
21        assert!(ann_config.ann_benchmarks_mode);
22    }
23
24    #[test]
25    fn test_dataset_analysis() -> Result<()> {
26        let config = AdvancedBenchmarkConfig::new();
27        let suite = AdvancedBenchmarkSuite::new(config);
28
29        let vectors = vec![
30            Vector::new(vec![1.0, 0.0, 0.0]),
31            Vector::new(vec![0.0, 1.0, 0.0]),
32            Vector::new(vec![0.0, 0.0, 1.0]),
33        ];
34
35        let stats = suite.compute_dataset_statistics(&vectors)?;
36        assert_eq!(stats.vector_count, 3);
37        assert_eq!(stats.dimensions, 3);
38        assert!(stats.mean_magnitude > 0.0);
39        Ok(())
40    }
41
42    #[test]
43    fn test_statistical_analyzer() -> Result<()> {
44        let analyzer = StatisticalAnalyzer::new(0.95, 10, 2.0);
45
46        let latency = LatencyMetrics {
47            mean_ms: 1.0,
48            std_ms: 0.1,
49            percentiles: HashMap::new(),
50            distribution: vec![
51                0.9, 1.0, 1.1, 0.95, 1.05, 0.98, 1.02, 0.92, 1.08, 0.97, 1.03,
52            ],
53            max_ms: 1.1,
54            min_ms: 0.9,
55        };
56
57        let performance = PerformanceMetrics {
58            latency,
59            throughput: ThroughputMetrics {
60                qps: 1000.0,
61                batch_qps: HashMap::new(),
62                concurrent_qps: HashMap::new(),
63                saturation_qps: 1200.0,
64            },
65            build_time: BuildTimeMetrics {
66                total_seconds: 10.0,
67                per_vector_ms: 0.1,
68                allocation_seconds: 1.0,
69                construction_seconds: 8.0,
70                optimization_seconds: 1.0,
71            },
72            index_size: IndexSizeMetrics {
73                total_bytes: 1024,
74                per_vector_bytes: 100.0,
75                overhead_ratio: 0.2,
76                compression_ratio: 0.8,
77                serialized_bytes: 800,
78            },
79        };
80
81        let stats = analyzer.analyze_metrics(&performance)?;
82        assert_eq!(stats.sample_size, 11);
83        assert!(stats.confidence_intervals.contains_key("mean_latency_ms"));
84        Ok(())
85    }
86}