kore_fileformat 1.1.4

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
493
494
495
496
/// Baseline performance benchmarking and measurement
///
/// Provides infrastructure for measuring query performance,
/// comparing optimization techniques, and tracking improvements.

use std::time::Instant;
use std::collections::HashMap;

/// Performance metric for a single operation
#[derive(Clone, Debug, PartialEq)]
pub struct PerformanceMetric {
    pub operation_name: String,
    pub duration_ms: u64,
    pub throughput_ops_per_sec: f64,
    pub memory_peak_mb: f64,
}

impl PerformanceMetric {
    pub fn new(
        name: &str,
        duration_ms: u64,
        ops: usize,
        peak_mem_mb: f64,
    ) -> Self {
        let throughput = if duration_ms > 0 {
            (ops as f64) / (duration_ms as f64) * 1000.0
        } else {
            0.0
        };

        Self {
            operation_name: name.to_string(),
            duration_ms,
            throughput_ops_per_sec: throughput,
            memory_peak_mb: peak_mem_mb,
        }
    }
}

/// Benchmark session for measuring operation performance
pub struct BenchmarkSession {
    name: String,
    start_time: Instant,
    operations_count: usize,
    memory_peak: f64,
}

impl BenchmarkSession {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            start_time: Instant::now(),
            operations_count: 0,
            memory_peak: 0.0,
        }
    }

    pub fn record_operation(&mut self) {
        self.operations_count += 1;
    }

    pub fn record_operations(&mut self, count: usize) {
        self.operations_count += count;
    }

    pub fn set_peak_memory(&mut self, mb: f64) {
        self.memory_peak = self.memory_peak.max(mb);
    }

    pub fn finish(&self) -> PerformanceMetric {
        let duration = self.start_time.elapsed().as_millis() as u64;

        PerformanceMetric::new(
            &self.name,
            duration,
            self.operations_count,
            self.memory_peak,
        )
    }
}

/// Baseline performance metrics
#[derive(Clone, Debug, PartialEq)]
pub struct BaselineMetrics {
    pub query_name: String,
    pub sequential_time_ms: u64,
    pub rows_processed: usize,
    pub selectivity: f64,
    pub memory_mb: f64,
}

impl BaselineMetrics {
    pub fn new(
        query: &str,
        time_ms: u64,
        rows: usize,
        sel: f64,
        mem: f64,
    ) -> Self {
        Self {
            query_name: query.to_string(),
            sequential_time_ms: time_ms,
            rows_processed: rows,
            selectivity: sel,
            memory_mb: mem,
        }
    }

    pub fn throughput_rows_per_sec(&self) -> f64 {
        if self.sequential_time_ms > 0 {
            (self.rows_processed as f64)
                / (self.sequential_time_ms as f64)
                * 1000.0
        } else {
            0.0
        }
    }
}

/// Optimization comparison result
#[derive(Clone, Debug, PartialEq)]
pub struct OptimizationComparison {
    pub operation: String,
    pub baseline_ms: u64,
    pub optimized_ms: u64,
    pub speedup_factor: f64,
    pub improvement_percent: f64,
    pub memory_savings_mb: f64,
}

impl OptimizationComparison {
    pub fn new(
        op: &str,
        baseline: u64,
        optimized: u64,
        mem_saved: f64,
    ) -> Self {
        let speedup = if optimized > 0 {
            (baseline as f64) / (optimized as f64)
        } else {
            0.0
        };

        let improvement =
            ((baseline as f64 - optimized as f64)
                / (baseline as f64))
                * 100.0;

        Self {
            operation: op.to_string(),
            baseline_ms: baseline,
            optimized_ms: optimized,
            speedup_factor: speedup,
            improvement_percent: improvement,
            memory_savings_mb: mem_saved,
        }
    }

    pub fn is_improvement(&self) -> bool {
        self.speedup_factor > 1.0
    }
}

/// Query performance baseline
#[derive(Clone, Debug)]
pub struct QueryBaseline {
    pub query_id: usize,
    pub query_text: String,
    pub metrics: BaselineMetrics,
}

/// Baseline performance tracker
pub struct BaselineTracker {
    baselines: HashMap<String, BaselineMetrics>,
    comparisons: Vec<OptimizationComparison>,
}

impl BaselineTracker {
    pub fn new() -> Self {
        Self {
            baselines: HashMap::new(),
            comparisons: Vec::new(),
        }
    }

    /// Record baseline metric
    pub fn record_baseline(&mut self, metrics: BaselineMetrics) {
        self.baselines
            .insert(metrics.query_name.clone(), metrics);
    }

    /// Get baseline for query
    pub fn get_baseline(&self, query: &str) -> Option<&BaselineMetrics> {
        self.baselines.get(query)
    }

    /// Record optimization comparison
    pub fn record_comparison(&mut self, comparison: OptimizationComparison) {
        self.comparisons.push(comparison);
    }

    /// Get average speedup across all comparisons
    pub fn average_speedup(&self) -> f64 {
        if self.comparisons.is_empty() {
            return 1.0;
        }

        let total: f64 =
            self.comparisons.iter().map(|c| c.speedup_factor).sum();
        total / (self.comparisons.len() as f64)
    }

    /// Get total memory savings
    pub fn total_memory_savings(&self) -> f64 {
        self.comparisons
            .iter()
            .map(|c| c.memory_savings_mb)
            .sum()
    }

    /// Get improvement summary
    pub fn improvement_summary(&self) -> ImprovementSummary {
        let total_ops = self.comparisons.len();
        let improved = self
            .comparisons
            .iter()
            .filter(|c| c.is_improvement())
            .count();

        let avg_improvement = if !self.comparisons.is_empty() {
            self.comparisons
                .iter()
                .map(|c| c.improvement_percent)
                .sum::<f64>()
                / (self.comparisons.len() as f64)
        } else {
            0.0
        };

        ImprovementSummary {
            total_optimizations: total_ops,
            improvements_found: improved,
            improvement_rate: if total_ops > 0 {
                (improved as f64) / (total_ops as f64) * 100.0
            } else {
                0.0
            },
            average_improvement_percent: avg_improvement,
            average_speedup: self.average_speedup(),
            total_memory_savings_mb: self.total_memory_savings(),
        }
    }
}

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

/// Performance improvement summary
#[derive(Clone, Debug, PartialEq)]
pub struct ImprovementSummary {
    pub total_optimizations: usize,
    pub improvements_found: usize,
    pub improvement_rate: f64,
    pub average_improvement_percent: f64,
    pub average_speedup: f64,
    pub total_memory_savings_mb: f64,
}

/// Sequential vs parallel performance comparison
#[derive(Clone, Debug, PartialEq)]
pub struct ParallelizationComparison {
    pub query_name: String,
    pub sequential_ms: u64,
    pub parallel_ms: u64,
    pub num_workers: usize,
    pub speedup: f64,
    pub efficiency: f64,
}

impl ParallelizationComparison {
    pub fn new(
        query: &str,
        seq_ms: u64,
        par_ms: u64,
        workers: usize,
    ) -> Self {
        let speedup = if par_ms > 0 {
            (seq_ms as f64) / (par_ms as f64)
        } else {
            0.0
        };

        let efficiency = if workers > 0 {
            speedup / (workers as f64)
        } else {
            0.0
        };

        Self {
            query_name: query.to_string(),
            sequential_ms: seq_ms,
            parallel_ms: par_ms,
            num_workers: workers,
            speedup,
            efficiency,
        }
    }
}

/// Memory pooling impact measurement
#[derive(Clone, Debug, PartialEq)]
pub struct MemoryPoolingImpact {
    pub operation: String,
    pub without_pooling_mb: f64,
    pub with_pooling_mb: f64,
    pub memory_savings_mb: f64,
    pub savings_percent: f64,
    pub allocation_count_without: usize,
    pub allocation_count_with: usize,
    pub allocations_avoided: usize,
}

impl MemoryPoolingImpact {
    pub fn new(
        op: &str,
        without_mb: f64,
        with_mb: f64,
        allocs_without: usize,
        allocs_with: usize,
    ) -> Self {
        let savings = without_mb - with_mb;
        let savings_percent = (savings / without_mb) * 100.0;

        Self {
            operation: op.to_string(),
            without_pooling_mb: without_mb,
            with_pooling_mb: with_mb,
            memory_savings_mb: savings,
            savings_percent,
            allocation_count_without: allocs_without,
            allocation_count_with: allocs_with,
            allocations_avoided: allocs_without - allocs_with,
        }
    }
}

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

    #[test]
    fn test_performance_metric() {
        let metric =
            PerformanceMetric::new("test_query", 1000, 10000, 50.0);

        assert_eq!(metric.operation_name, "test_query");
        assert_eq!(metric.duration_ms, 1000);
        assert!(metric.throughput_ops_per_sec > 0.0);
    }

    #[test]
    fn test_benchmark_session() {
        let mut session = BenchmarkSession::new("query_test");
        session.record_operations(1000);
        session.set_peak_memory(100.0);

        let metric = session.finish();
        assert_eq!(metric.operation_name, "query_test");
        assert_eq!(metric.memory_peak_mb, 100.0);
    }

    #[test]
    fn test_baseline_metrics() {
        let baseline = BaselineMetrics::new("q1", 500, 10000, 0.5, 75.0);

        assert_eq!(baseline.query_name, "q1");
        assert_eq!(baseline.sequential_time_ms, 500);
        assert!(baseline.throughput_rows_per_sec() > 0.0);
    }

    #[test]
    fn test_optimization_comparison() {
        let comp =
            OptimizationComparison::new("join_opt", 1000, 400, 20.0);

        assert!(comp.is_improvement());
        assert!(comp.speedup_factor > 1.0);
        assert!(comp.improvement_percent > 0.0);
    }

    #[test]
    fn test_baseline_tracker() {
        let mut tracker = BaselineTracker::new();
        let baseline1 =
            BaselineMetrics::new("q1", 100, 1000, 0.5, 50.0);
        let baseline2 =
            BaselineMetrics::new("q2", 200, 2000, 0.3, 75.0);

        tracker.record_baseline(baseline1);
        tracker.record_baseline(baseline2);

        assert!(tracker.get_baseline("q1").is_some());
        assert!(tracker.get_baseline("q2").is_some());
    }

    #[test]
    fn test_baseline_tracker_comparisons() {
        let mut tracker = BaselineTracker::new();
        tracker.record_comparison(OptimizationComparison::new(
            "op1", 1000, 500, 10.0,
        ));
        tracker.record_comparison(OptimizationComparison::new(
            "op2", 800, 200, 20.0,
        ));

        let summary = tracker.improvement_summary();
        assert_eq!(summary.total_optimizations, 2);
        assert_eq!(summary.improvements_found, 2);
        assert!(summary.average_speedup > 1.0);
    }

    #[test]
    fn test_improvement_summary() {
        let summary = ImprovementSummary {
            total_optimizations: 10,
            improvements_found: 8,
            improvement_rate: 80.0,
            average_improvement_percent: 40.0,
            average_speedup: 2.5,
            total_memory_savings_mb: 500.0,
        };

        assert_eq!(summary.improvement_rate, 80.0);
        assert!(summary.average_speedup > 1.0);
    }

    #[test]
    fn test_parallelization_comparison() {
        let comp =
            ParallelizationComparison::new("query1", 1000, 300, 4);

        assert!(comp.speedup > 1.0);
        assert!(comp.efficiency > 0.0);
        assert!(comp.efficiency <= 1.0);
    }

    #[test]
    fn test_memory_pooling_impact() {
        let impact = MemoryPoolingImpact::new(
            "batch_insert",
            100.0,
            30.0,
            1000,
            100,
        );

        assert_eq!(impact.allocations_avoided, 900);
        assert!(impact.savings_percent > 0.0);
        assert!(impact.memory_savings_mb > 0.0);
    }

    #[test]
    fn test_query_baseline() {
        let baseline = QueryBaseline {
            query_id: 1,
            query_text: "SELECT * FROM users".to_string(),
            metrics: BaselineMetrics::new("q", 100, 1000, 1.0, 50.0),
        };

        assert_eq!(baseline.query_id, 1);
        assert!(!baseline.query_text.is_empty());
    }

    #[test]
    fn test_average_speedup_empty_tracker() {
        let tracker = BaselineTracker::new();
        assert_eq!(tracker.average_speedup(), 1.0);
    }

    #[test]
    fn test_average_speedup_with_comparisons() {
        let mut tracker = BaselineTracker::new();
        tracker.record_comparison(OptimizationComparison::new(
            "op1", 1000, 500, 0.0,
        ));
        tracker.record_comparison(OptimizationComparison::new(
            "op2", 1000, 250, 0.0,
        ));

        let avg = tracker.average_speedup();
        assert!(avg > 1.5);
    }
}