kore_fileformat 1.1.3

KORE — Killer Optimized Record Exchange: standalone Rust crate (zero deps)
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
/// Real-world query benchmarking suite
///
/// Benchmarks optimizations using realistic query patterns and data sizes.
/// Measures performance improvements from:
/// - Query parallelization
/// - Memory pooling
/// - JOIN algorithm optimization

use crate::baseline_benchmarking::{
    BaselineMetrics, OptimizationComparison, PerformanceMetric,
    BaselineTracker,
};
use crate::query_optimization_engine::{
    OptimizedQueryContext, OptimizedQueryResult, ImprovementReport,
};
use std::time::Instant;

/// Real-world query patterns
#[derive(Clone, Debug, PartialEq)]
pub enum QueryPattern {
    /// Simple filtering with low selectivity
    FilterSelectiveSmall,
    /// Medium-sized JOIN operation
    JoinMedium,
    /// Aggregate with GROUP BY
    AggregateGroupBy,
    /// Complex multi-table JOIN
    ComplexMultiJoin,
    /// Large table scan with filter
    LargeScanFilter,
}

impl QueryPattern {
    pub fn description(&self) -> &str {
        match self {
            QueryPattern::FilterSelectiveSmall => "Small selective filter (10K rows, 10% selectivity)",
            QueryPattern::JoinMedium => "Medium JOIN operation (100K rows, 50% selectivity)",
            QueryPattern::AggregateGroupBy => "Aggregate with GROUP BY (50K rows, 20% selectivity)",
            QueryPattern::ComplexMultiJoin => "Complex multi-table JOIN (1M rows, 30% selectivity)",
            QueryPattern::LargeScanFilter => "Large table scan with filter (500K rows, 5% selectivity)",
        }
    }

    pub fn parameters(&self) -> (usize, f64) {
        match self {
            QueryPattern::FilterSelectiveSmall => (10000, 0.1),
            QueryPattern::JoinMedium => (100000, 0.5),
            QueryPattern::AggregateGroupBy => (50000, 0.2),
            QueryPattern::ComplexMultiJoin => (1000000, 0.3),
            QueryPattern::LargeScanFilter => (500000, 0.05),
        }
    }
}

/// Benchmark configuration
#[derive(Clone, Debug)]
pub struct BenchmarkConfig {
    pub iterations: usize,
    pub warmup_iterations: usize,
    pub enable_parallelization: bool,
    pub enable_memory_pooling: bool,
    pub patterns: Vec<QueryPattern>,
}

impl BenchmarkConfig {
    pub fn new() -> Self {
        Self {
            iterations: 5,
            warmup_iterations: 2,
            enable_parallelization: true,
            enable_memory_pooling: true,
            patterns: vec![
                QueryPattern::FilterSelectiveSmall,
                QueryPattern::JoinMedium,
                QueryPattern::AggregateGroupBy,
                QueryPattern::ComplexMultiJoin,
                QueryPattern::LargeScanFilter,
            ],
        }
    }

    pub fn with_iterations(mut self, iter: usize) -> Self {
        self.iterations = iter;
        self
    }

    pub fn with_warmup(mut self, warmup: usize) -> Self {
        self.warmup_iterations = warmup;
        self
    }
}

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

/// Single benchmark result
#[derive(Clone, Debug, PartialEq)]
pub struct BenchmarkResult {
    pub pattern: String,
    pub description: String,
    pub iterations: usize,
    pub avg_sequential_ms: f64,
    pub avg_parallel_ms: f64,
    pub avg_speedup: f64,
    pub memory_saved_mb: f64,
    pub consistency_score: f64, // 0-1, higher = more consistent
}

impl BenchmarkResult {
    pub fn improvement_percent(&self) -> f64 {
        if self.avg_sequential_ms > 0.0 {
            ((self.avg_sequential_ms - self.avg_parallel_ms)
                / self.avg_sequential_ms)
                * 100.0
        } else {
            0.0
        }
    }
}

/// Real-world benchmark suite
pub struct RealWorldBenchmarkSuite {
    config: BenchmarkConfig,
    context: OptimizedQueryContext,
    results: Vec<BenchmarkResult>,
}

impl RealWorldBenchmarkSuite {
    pub fn new(config: BenchmarkConfig) -> Self {
        let mut context = OptimizedQueryContext::new();
        context.enable_parallelization = config.enable_parallelization;
        context.enable_memory_pooling = config.enable_memory_pooling;

        Self {
            config,
            context,
            results: Vec::new(),
        }
    }

    /// Run complete benchmark suite
    pub fn run_all(&mut self) -> BenchmarkSuiteReport {
        let start = Instant::now();

        // Clone patterns to avoid borrow conflicts
        let patterns = self.config.patterns.clone();

        // Warmup phase
        for pattern in &patterns {
            self.warmup_pattern(pattern);
        }

        // Benchmark phase
        for pattern in &patterns {
            let result = self.benchmark_pattern(pattern);
            self.results.push(result);
        }

        let total_time_ms = start.elapsed().as_millis() as f64;

        BenchmarkSuiteReport {
            config: self.config.clone(),
            results: self.results.clone(),
            total_time_ms,
            generated_at: chrono_now(),
        }
    }

    fn warmup_pattern(&mut self, pattern: &QueryPattern) {
        for _ in 0..self.config.warmup_iterations {
            let (rows, selectivity) = pattern.parameters();
            let _ = self.context.execute_optimized_query(
                &format!("warmup_{:?}", pattern),
                rows,
                selectivity,
            );
        }
    }

    fn benchmark_pattern(&mut self, pattern: &QueryPattern) -> BenchmarkResult {
        let (rows, selectivity) = pattern.parameters();
        let mut seq_times = Vec::new();
        let mut par_times = Vec::new();
        let pattern_name = format!("{:?}", pattern);

        for i in 0..self.config.iterations {
            let result = self.context.execute_optimized_query(
                &format!("{}_iter{}", pattern_name, i),
                rows,
                selectivity,
            );

            seq_times.push(result.sequential_ms as f64);
            par_times.push(result.parallel_ms as f64);
        }

        let avg_seq = seq_times.iter().sum::<f64>() / seq_times.len() as f64;
        let avg_par = par_times.iter().sum::<f64>() / par_times.len() as f64;

        // Calculate consistency score (inverse of coefficient of variation)
        let variance = calculate_variance(&par_times, avg_par);
        let std_dev = variance.sqrt();
        let cv = if avg_par > 0.0 {
            std_dev / avg_par
        } else {
            0.0
        };
        let consistency = (1.0 / (1.0 + cv)).max(0.0).min(1.0);

        BenchmarkResult {
            pattern: pattern_name,
            description: pattern.description().to_string(),
            iterations: self.config.iterations,
            avg_sequential_ms: avg_seq,
            avg_parallel_ms: avg_par,
            avg_speedup: if avg_par > 0.0 {
                avg_seq / avg_par
            } else {
                1.0
            },
            memory_saved_mb: (rows as f64) * 0.05 / 1000000.0,
            consistency_score: consistency,
        }
    }

    pub fn get_results(&self) -> Vec<BenchmarkResult> {
        self.results.clone()
    }

    pub fn get_improvement_summary(&self) -> ImprovementReport {
        self.context.get_improvement_summary()
    }
}

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

/// Complete benchmark report
#[derive(Clone, Debug)]
pub struct BenchmarkSuiteReport {
    pub config: BenchmarkConfig,
    pub results: Vec<BenchmarkResult>,
    pub total_time_ms: f64,
    pub generated_at: String,
}

impl BenchmarkSuiteReport {
    pub fn summary(&self) -> BenchmarkSummary {
        let avg_speedup = if !self.results.is_empty() {
            self.results.iter().map(|r| r.avg_speedup).sum::<f64>()
                / self.results.len() as f64
        } else {
            1.0
        };

        let total_memory_saved = self.results.iter()
            .map(|r| r.memory_saved_mb)
            .sum::<f64>();

        let best_speedup = self.results.iter()
            .max_by(|a, b| {
                a.avg_speedup.partial_cmp(&b.avg_speedup).unwrap()
            })
            .map(|r| r.avg_speedup)
            .unwrap_or(1.0);

        let worst_speedup = self.results.iter()
            .min_by(|a, b| {
                a.avg_speedup.partial_cmp(&b.avg_speedup).unwrap()
            })
            .map(|r| r.avg_speedup)
            .unwrap_or(1.0);

        BenchmarkSummary {
            total_patterns: self.results.len(),
            average_speedup: avg_speedup,
            best_speedup,
            worst_speedup,
            total_memory_savings_mb: total_memory_saved,
            avg_consistency: self.results.iter()
                .map(|r| r.consistency_score)
                .sum::<f64>() / self.results.len() as f64,
        }
    }

    pub fn format_report(&self) -> String {
        let summary = self.summary();
        
        format!(
            "=== Real-World Benchmark Report ===\n\
            Generated: {}\n\
            Total Time: {:.2}ms\n\
            Iterations per pattern: {}\n\
            \n\
            Summary:\n\
            - Average Speedup: {:.2}x\n\
            - Best Speedup: {:.2}x\n\
            - Worst Speedup: {:.2}x\n\
            - Total Memory Saved: {:.2} MB\n\
            - Consistency Score: {:.2}\n\
            \n\
            Individual Results:\n{}",
            self.generated_at,
            self.total_time_ms,
            self.config.iterations,
            summary.average_speedup,
            summary.best_speedup,
            summary.worst_speedup,
            summary.total_memory_savings_mb,
            summary.avg_consistency,
            self.results.iter()
                .map(|r| format!(
                    "  {} ({:.2}x speedup, {:.1}% improvement, {:.2} consistency)",
                    r.pattern,
                    r.avg_speedup,
                    r.improvement_percent(),
                    r.consistency_score
                ))
                .collect::<Vec<_>>()
                .join("\n")
        )
    }
}

/// Benchmark summary statistics
#[derive(Clone, Debug, PartialEq)]
pub struct BenchmarkSummary {
    pub total_patterns: usize,
    pub average_speedup: f64,
    pub best_speedup: f64,
    pub worst_speedup: f64,
    pub total_memory_savings_mb: f64,
    pub avg_consistency: f64,
}

fn calculate_variance(values: &[f64], mean: f64) -> f64 {
    if values.is_empty() {
        return 0.0;
    }
    values.iter()
        .map(|v| (v - mean).powi(2))
        .sum::<f64>()
        / values.len() as f64
}

fn chrono_now() -> String {
    // Simplified - just use a timestamp format
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs();
    format!("2026-05-10 {:02}:{:02}:{:02}",
        (now / 3600) % 24,
        (now / 60) % 60,
        now % 60)
}

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

    #[test]
    fn test_query_pattern_parameters() {
        let (rows, sel) = QueryPattern::FilterSelectiveSmall.parameters();
        assert_eq!(rows, 10000);
        assert_eq!(sel, 0.1);

        let (rows, sel) = QueryPattern::ComplexMultiJoin.parameters();
        assert_eq!(rows, 1000000);
        assert_eq!(sel, 0.3);
    }

    #[test]
    fn test_benchmark_config() {
        let config = BenchmarkConfig::new()
            .with_iterations(10)
            .with_warmup(3);

        assert_eq!(config.iterations, 10);
        assert_eq!(config.warmup_iterations, 3);
    }

    #[test]
    fn test_benchmark_result_improvement() {
        let result = BenchmarkResult {
            pattern: "test".to_string(),
            description: "test".to_string(),
            iterations: 5,
            avg_sequential_ms: 100.0,
            avg_parallel_ms: 50.0,
            avg_speedup: 2.0,
            memory_saved_mb: 10.0,
            consistency_score: 0.95,
        };

        assert_eq!(result.improvement_percent(), 50.0);
    }

    #[test]
    fn test_real_world_benchmark_suite() {
        let config = BenchmarkConfig::new()
            .with_iterations(2)
            .with_warmup(1);
        let mut suite = RealWorldBenchmarkSuite::new(config);

        let report = suite.run_all();
        assert!(!report.results.is_empty());
        assert!(report.total_time_ms > 0.0);
    }

    #[test]
    fn test_benchmark_suite_report_summary() {
        let results = vec![
            BenchmarkResult {
                pattern: "p1".to_string(),
                description: "p1".to_string(),
                iterations: 5,
                avg_sequential_ms: 100.0,
                avg_parallel_ms: 50.0,
                avg_speedup: 2.0,
                memory_saved_mb: 5.0,
                consistency_score: 0.9,
            },
            BenchmarkResult {
                pattern: "p2".to_string(),
                description: "p2".to_string(),
                iterations: 5,
                avg_sequential_ms: 200.0,
                avg_parallel_ms: 80.0,
                avg_speedup: 2.5,
                memory_saved_mb: 10.0,
                consistency_score: 0.85,
            },
        ];

        let report = BenchmarkSuiteReport {
            config: BenchmarkConfig::default(),
            results,
            total_time_ms: 1000.0,
            generated_at: "2026-05-10 12:00:00".to_string(),
        };

        let summary = report.summary();
        assert_eq!(summary.total_patterns, 2);
        assert!(summary.average_speedup > 2.0);
        assert!(summary.best_speedup >= 2.0);
    }

    #[test]
    fn test_benchmark_suite_report_format() {
        let report = BenchmarkSuiteReport {
            config: BenchmarkConfig::default(),
            results: vec![],
            total_time_ms: 500.0,
            generated_at: "2026-05-10 12:00:00".to_string(),
        };

        let formatted = report.format_report();
        assert!(formatted.contains("Real-World Benchmark Report"));
        assert!(formatted.contains("Average Speedup"));
    }

    #[test]
    fn test_variance_calculation() {
        let values = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let mean = 3.0;
        let variance = calculate_variance(&values, mean);
        assert!(variance > 0.0);
    }

    #[test]
    fn test_benchmark_summary() {
        let summary = BenchmarkSummary {
            total_patterns: 5,
            average_speedup: 2.5,
            best_speedup: 3.5,
            worst_speedup: 1.5,
            total_memory_savings_mb: 100.0,
            avg_consistency: 0.9,
        };

        assert_eq!(summary.total_patterns, 5);
        assert!(summary.average_speedup > 1.0);
        assert!(summary.best_speedup > summary.worst_speedup);
    }
}