sarif_rust 0.3.0

A comprehensive Rust library for parsing, generating, and manipulating SARIF (Static Analysis Results Interchange Format) v2.1.0 files
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
//! Performance optimizations and monitoring for SARIF processing
//!
//! This module provides performance optimizations, memory management utilities,
//! and benchmarking tools for SARIF operations.

use crate::parser::{SarifError, SarifResult as ParseResult};
use crate::types::{Result as SarifResult, SarifLog};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};

/// Performance monitoring and optimization utilities
#[derive(Debug, Clone)]
pub struct PerformanceMonitor {
    /// Operation timing records
    pub timings: HashMap<String, Vec<Duration>>,

    /// Memory usage statistics
    pub memory_stats: MemoryStats,

    /// Operation counters
    pub counters: HashMap<String, usize>,

    /// Configuration for performance optimizations
    pub config: PerformanceConfig,
}

/// Memory usage statistics
#[derive(Debug, Clone, Default)]
pub struct MemoryStats {
    /// Peak memory usage during operations
    pub peak_memory_mb: f64,

    /// Current estimated memory usage
    pub current_memory_mb: f64,

    /// Number of allocations tracked
    pub allocation_count: usize,

    /// Memory usage by operation type
    pub memory_by_operation: HashMap<String, f64>,
}

/// Configuration for performance optimizations
#[derive(Debug, Clone)]
pub struct PerformanceConfig {
    /// Enable memory usage tracking
    pub track_memory: bool,

    /// Enable detailed timing
    pub enable_timing: bool,

    /// Maximum memory usage before triggering optimizations (MB)
    pub memory_threshold_mb: f64,

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

    /// Enable parallel processing where possible
    pub enable_parallel: bool,

    /// Cache size limits
    pub cache_config: CacheConfig,
}

/// Cache configuration settings
#[derive(Debug, Clone)]
pub struct CacheConfig {
    /// Maximum number of parsed SARIF logs to cache
    pub max_logs: usize,

    /// Maximum number of indexed results to cache
    pub max_indexed_results: usize,

    /// Maximum number of query results to cache
    pub max_query_results: usize,

    /// Cache entry TTL in seconds
    pub ttl_seconds: u64,
}

/// Memory pool for efficient allocation of frequently used objects
pub struct MemoryPool<T> {
    /// Pool of reusable objects
    pool: Vec<T>,

    /// Factory function for creating new objects
    factory: Box<dyn Fn() -> T + Send + Sync>,

    /// Maximum pool size
    max_size: usize,

    /// Current pool usage statistics
    stats: PoolStats,
}

/// Statistics for memory pool usage
#[derive(Debug, Default)]
pub struct PoolStats {
    /// Number of objects borrowed from pool
    pub borrowed: AtomicUsize,

    /// Number of objects returned to pool
    pub returned: AtomicUsize,

    /// Number of new objects created
    pub created: AtomicUsize,

    /// Peak pool size
    pub peak_size: AtomicUsize,
}

/// Optimized SARIF processor with performance enhancements
#[allow(dead_code)]
pub struct OptimizedSarifProcessor {
    /// Performance monitor
    monitor: PerformanceMonitor,

    /// Memory pools for common objects
    result_pool: Arc<MemoryPool<SarifResult>>,

    /// String interning for reducing memory usage
    string_interner: StringInterner,

    /// Cached computations
    computation_cache: ComputationCache,
}

/// String interner for reducing memory usage of duplicate strings
#[derive(Debug)]
pub struct StringInterner {
    /// Interned strings with reference counting
    strings: HashMap<String, (Arc<String>, usize)>,

    /// Statistics
    stats: InternerStats,
}

/// Statistics for string interner
#[derive(Debug, Clone, Default)]
pub struct InternerStats {
    /// Total strings interned
    pub total_interned: usize,

    /// Number of cache hits
    pub cache_hits: usize,

    /// Memory saved (estimated in bytes)
    pub memory_saved_bytes: usize,
}

/// Cache for expensive computations
#[derive(Debug)]
pub struct ComputationCache {
    /// Cached fingerprints for results
    fingerprint_cache: HashMap<String, String>,

    /// Cached file path resolutions
    path_cache: HashMap<String, String>,

    /// Cached rule lookups
    rule_cache: HashMap<String, Option<Arc<crate::types::ReportingDescriptor>>>,

    /// Cache statistics
    stats: CacheStats,
}

/// Cache usage statistics
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Cache hits by type
    pub hits_by_type: HashMap<String, usize>,

    /// Cache misses by type
    pub misses_by_type: HashMap<String, usize>,

    /// Cache evictions by type
    pub evictions_by_type: HashMap<String, usize>,
}

/// Benchmark suite for SARIF operations
pub struct SarifBenchmark {
    /// Test data sets of varying sizes
    pub test_datasets: Vec<BenchmarkDataset>,

    /// Benchmark results
    pub results: Vec<BenchmarkResult>,

    /// Configuration for benchmarks
    pub config: BenchmarkConfig,
}

/// Dataset for benchmarking
#[derive(Debug, Clone)]
pub struct BenchmarkDataset {
    /// Dataset name
    pub name: String,

    /// SARIF log for testing
    pub log: SarifLog,

    /// Expected characteristics
    pub characteristics: DatasetCharacteristics,
}

/// Characteristics of a benchmark dataset
#[derive(Debug, Clone)]
pub struct DatasetCharacteristics {
    /// Number of runs
    pub run_count: usize,

    /// Total number of results
    pub result_count: usize,

    /// Number of unique files
    pub file_count: usize,

    /// Number of unique rules
    pub rule_count: usize,

    /// Average results per file
    pub avg_results_per_file: f64,

    /// Estimated size in MB
    pub size_mb: f64,
}

/// Result of a benchmark operation
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    /// Operation name
    pub operation: String,

    /// Dataset used
    pub dataset: String,

    /// Execution time
    pub duration: Duration,

    /// Memory usage during operation
    pub memory_usage_mb: f64,

    /// Throughput metrics
    pub throughput: ThroughputMetrics,

    /// Additional metrics
    pub custom_metrics: HashMap<String, f64>,
}

/// Throughput metrics for benchmarks
#[derive(Debug, Clone)]
pub struct ThroughputMetrics {
    /// Results processed per second
    pub results_per_second: f64,

    /// Files processed per second
    pub files_per_second: f64,

    /// MB processed per second
    pub mb_per_second: f64,
}

/// Configuration for benchmark runs
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Number of iterations per benchmark
    pub iterations: usize,

    /// Warmup iterations before measurement
    pub warmup_iterations: usize,

    /// Enable memory profiling during benchmarks
    pub profile_memory: bool,

    /// Operations to benchmark
    pub operations: Vec<String>,
}

impl PerformanceMonitor {
    /// Create a new performance monitor
    pub fn new(config: PerformanceConfig) -> Self {
        Self {
            timings: HashMap::new(),
            memory_stats: MemoryStats::default(),
            counters: HashMap::new(),
            config,
        }
    }

    /// Start timing an operation
    pub fn start_timing(&mut self, operation: &str) -> TimingHandle {
        TimingHandle {
            operation: operation.to_string(),
            start_time: Instant::now(),
            monitor: self as *mut PerformanceMonitor,
        }
    }

    /// Record a timing measurement
    pub fn record_timing(&mut self, operation: &str, duration: Duration) {
        if self.config.enable_timing {
            self.timings
                .entry(operation.to_string())
                .or_insert_with(Vec::new)
                .push(duration);
        }
    }

    /// Increment a counter
    pub fn increment_counter(&mut self, counter: &str) {
        *self.counters.entry(counter.to_string()).or_insert(0) += 1;
    }

    /// Update memory statistics
    pub fn update_memory_stats(&mut self, operation: &str, memory_mb: f64) {
        if self.config.track_memory {
            self.memory_stats.current_memory_mb = memory_mb;
            if memory_mb > self.memory_stats.peak_memory_mb {
                self.memory_stats.peak_memory_mb = memory_mb;
            }
            self.memory_stats
                .memory_by_operation
                .insert(operation.to_string(), memory_mb);
            self.memory_stats.allocation_count += 1;
        }
    }

    /// Get average timing for an operation
    pub fn get_average_timing(&self, operation: &str) -> Option<Duration> {
        self.timings.get(operation).map(|times| {
            let total: Duration = times.iter().sum();
            total / times.len() as u32
        })
    }

    /// Get percentile timing for an operation
    pub fn get_percentile_timing(&self, operation: &str, percentile: f64) -> Option<Duration> {
        self.timings.get(operation).and_then(|times| {
            if times.is_empty() {
                return None;
            }
            let mut sorted_times = times.clone();
            sorted_times.sort();
            let index = ((percentile / 100.0) * times.len() as f64) as usize;
            sorted_times.get(index.min(times.len() - 1)).copied()
        })
    }

    /// Generate performance report
    pub fn generate_report(&self) -> PerformanceReport {
        let mut operation_stats = HashMap::new();

        for (operation, times) in &self.timings {
            if !times.is_empty() {
                let total: Duration = times.iter().sum();
                let avg = total / times.len() as u32;
                let min = *times.iter().min().unwrap();
                let max = *times.iter().max().unwrap();

                operation_stats.insert(
                    operation.clone(),
                    OperationStats {
                        count: times.len(),
                        total_duration: total,
                        average_duration: avg,
                        min_duration: min,
                        max_duration: max,
                        p50: self.get_percentile_timing(operation, 50.0).unwrap_or(avg),
                        p95: self.get_percentile_timing(operation, 95.0).unwrap_or(max),
                        p99: self.get_percentile_timing(operation, 99.0).unwrap_or(max),
                    },
                );
            }
        }

        PerformanceReport {
            operation_stats,
            memory_stats: self.memory_stats.clone(),
            counters: self.counters.clone(),
            total_operations: self.counters.values().sum(),
        }
    }
}

/// Handle for timing operations
pub struct TimingHandle {
    operation: String,
    start_time: Instant,
    monitor: *mut PerformanceMonitor,
}

impl Drop for TimingHandle {
    fn drop(&mut self) {
        let duration = self.start_time.elapsed();
        unsafe {
            if !self.monitor.is_null() {
                (*self.monitor).record_timing(&self.operation, duration);
            }
        }
    }
}

/// Performance report
#[derive(Debug, Clone)]
pub struct PerformanceReport {
    /// Statistics by operation
    pub operation_stats: HashMap<String, OperationStats>,

    /// Memory usage statistics
    pub memory_stats: MemoryStats,

    /// Counter values
    pub counters: HashMap<String, usize>,

    /// Total number of operations
    pub total_operations: usize,
}

/// Statistics for a specific operation
#[derive(Debug, Clone)]
pub struct OperationStats {
    /// Number of times operation was performed
    pub count: usize,

    /// Total time spent on operation
    pub total_duration: Duration,

    /// Average duration per operation
    pub average_duration: Duration,

    /// Minimum duration observed
    pub min_duration: Duration,

    /// Maximum duration observed
    pub max_duration: Duration,

    /// 50th percentile (median)
    pub p50: Duration,

    /// 95th percentile
    pub p95: Duration,

    /// 99th percentile
    pub p99: Duration,
}

impl<T> MemoryPool<T> {
    /// Create a new memory pool
    pub fn new<F>(factory: F, max_size: usize) -> Self
    where
        F: Fn() -> T + Send + Sync + 'static,
    {
        Self {
            pool: Vec::with_capacity(max_size),
            factory: Box::new(factory),
            max_size,
            stats: PoolStats::default(),
        }
    }

    /// Borrow an object from the pool
    pub fn borrow(&mut self) -> T {
        self.stats.borrowed.fetch_add(1, Ordering::Relaxed);

        if let Some(obj) = self.pool.pop() {
            obj
        } else {
            self.stats.created.fetch_add(1, Ordering::Relaxed);
            (self.factory)()
        }
    }

    /// Return an object to the pool
    pub fn return_object(&mut self, obj: T) {
        self.stats.returned.fetch_add(1, Ordering::Relaxed);

        if self.pool.len() < self.max_size {
            self.pool.push(obj);
            let current_size = self.pool.len();
            let peak = self.stats.peak_size.load(Ordering::Relaxed);
            if current_size > peak {
                self.stats.peak_size.store(current_size, Ordering::Relaxed);
            }
        }
    }

    /// Get pool statistics
    pub fn get_stats(&self) -> PoolStats {
        PoolStats {
            borrowed: AtomicUsize::new(self.stats.borrowed.load(Ordering::Relaxed)),
            returned: AtomicUsize::new(self.stats.returned.load(Ordering::Relaxed)),
            created: AtomicUsize::new(self.stats.created.load(Ordering::Relaxed)),
            peak_size: AtomicUsize::new(self.stats.peak_size.load(Ordering::Relaxed)),
        }
    }
}

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

impl StringInterner {
    /// Create a new string interner
    pub fn new() -> Self {
        Self {
            strings: HashMap::new(),
            stats: InternerStats::default(),
        }
    }

    /// Intern a string, returning an Arc to the canonical version
    pub fn intern(&mut self, s: &str) -> Arc<String> {
        if let Some((arc_str, ref_count)) = self.strings.get_mut(s) {
            *ref_count += 1;
            self.stats.cache_hits += 1;
            return arc_str.clone();
        }

        let arc_str = Arc::new(s.to_string());
        self.strings.insert(s.to_string(), (arc_str.clone(), 1));
        self.stats.total_interned += 1;
        self.stats.memory_saved_bytes += s.len();

        arc_str
    }

    /// Release a reference to an interned string
    pub fn release(&mut self, s: &str) {
        if let Some((_, ref_count)) = self.strings.get_mut(s) {
            *ref_count -= 1;
            if *ref_count == 0 {
                self.strings.remove(s);
            }
        }
    }

    /// Get interner statistics
    pub fn get_stats(&self) -> &InternerStats {
        &self.stats
    }
}

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

impl ComputationCache {
    /// Create a new computation cache
    pub fn new() -> Self {
        Self {
            fingerprint_cache: HashMap::new(),
            path_cache: HashMap::new(),
            rule_cache: HashMap::new(),
            stats: CacheStats::default(),
        }
    }

    /// Get or compute a result fingerprint
    pub fn get_fingerprint<F>(&mut self, key: &str, compute_fn: F) -> String
    where
        F: FnOnce() -> String,
    {
        if let Some(fingerprint) = self.fingerprint_cache.get(key).cloned() {
            self.record_hit("fingerprint");
            return fingerprint;
        }

        self.record_miss("fingerprint");
        let fingerprint = compute_fn();
        self.fingerprint_cache
            .insert(key.to_string(), fingerprint.clone());
        fingerprint
    }

    /// Get or resolve a file path
    pub fn get_path<F>(&mut self, key: &str, resolve_fn: F) -> String
    where
        F: FnOnce() -> String,
    {
        if let Some(path) = self.path_cache.get(key).cloned() {
            self.record_hit("path");
            return path;
        }

        self.record_miss("path");
        let path = resolve_fn();
        self.path_cache.insert(key.to_string(), path.clone());
        path
    }

    /// Clear all caches
    pub fn clear(&mut self) {
        self.fingerprint_cache.clear();
        self.path_cache.clear();
        self.rule_cache.clear();
    }

    /// Get cache statistics
    pub fn get_stats(&self) -> &CacheStats {
        &self.stats
    }

    fn record_hit(&mut self, cache_type: &str) {
        *self
            .stats
            .hits_by_type
            .entry(cache_type.to_string())
            .or_insert(0) += 1;
    }

    fn record_miss(&mut self, cache_type: &str) {
        *self
            .stats
            .misses_by_type
            .entry(cache_type.to_string())
            .or_insert(0) += 1;
    }
}

impl SarifBenchmark {
    /// Create a new benchmark suite
    pub fn new(config: BenchmarkConfig) -> Self {
        Self {
            test_datasets: Vec::new(),
            results: Vec::new(),
            config,
        }
    }

    /// Add a test dataset
    pub fn add_dataset(&mut self, dataset: BenchmarkDataset) {
        self.test_datasets.push(dataset);
    }

    /// Run all benchmarks
    pub fn run_benchmarks(&mut self) -> ParseResult<Vec<BenchmarkResult>> {
        let mut all_results = Vec::new();

        for dataset in &self.test_datasets {
            for operation in &self.config.operations {
                let result = self.run_single_benchmark(operation, dataset)?;
                all_results.push(result);
            }
        }

        self.results = all_results.clone();
        Ok(all_results)
    }

    /// Run a single benchmark
    fn run_single_benchmark(
        &self,
        operation: &str,
        dataset: &BenchmarkDataset,
    ) -> ParseResult<BenchmarkResult> {
        let mut durations = Vec::new();
        let mut memory_usage = 0.0;

        // Warmup iterations
        for _ in 0..self.config.warmup_iterations {
            self.execute_operation(operation, dataset)?;
        }

        // Measured iterations
        for _ in 0..self.config.iterations {
            let start_memory = self.get_memory_usage();
            let start_time = Instant::now();

            self.execute_operation(operation, dataset)?;

            let duration = start_time.elapsed();
            let end_memory = self.get_memory_usage();

            durations.push(duration);
            memory_usage += end_memory - start_memory;
        }

        let avg_duration = durations.iter().sum::<Duration>() / durations.len() as u32;
        let avg_memory = memory_usage / self.config.iterations as f64;

        let throughput = ThroughputMetrics {
            results_per_second: dataset.characteristics.result_count as f64
                / avg_duration.as_secs_f64(),
            files_per_second: dataset.characteristics.file_count as f64
                / avg_duration.as_secs_f64(),
            mb_per_second: dataset.characteristics.size_mb / avg_duration.as_secs_f64(),
        };

        Ok(BenchmarkResult {
            operation: operation.to_string(),
            dataset: dataset.name.clone(),
            duration: avg_duration,
            memory_usage_mb: avg_memory,
            throughput,
            custom_metrics: HashMap::new(),
        })
    }

    /// Execute a specific operation on a dataset
    fn execute_operation(&self, operation: &str, dataset: &BenchmarkDataset) -> ParseResult<()> {
        match operation {
            "parse" => {
                // Simulate parsing by serializing and deserializing
                let json = serde_json::to_string(&dataset.log)
                    .map_err(|e| SarifError::custom(format!("Serialization error: {}", e)))?;
                let _: SarifLog = serde_json::from_str(&json)
                    .map_err(|e| SarifError::custom(format!("Deserialization error: {}", e)))?;
            }
            "index" => {
                // Simulate indexing
                let index = crate::utils::indexing::SarifIndex::from_sarif_log(&dataset.log);
                // Access some index data to ensure it's built
                let _ = index.stats.result_count;
            }
            "query" => {
                // Simulate querying
                let index = crate::utils::indexing::SarifIndex::from_sarif_log(&dataset.log);
                let executor =
                    crate::utils::query::SarifQueryExecutor::from_index(index, dataset.log.clone());
                let query = crate::utils::query::SarifQuery::default();
                let _ = executor.execute(&query)?;
            }
            "conversion" => {
                // Simulate conversion
                let converter = crate::utils::conversion::CsvConverter::new();
                let _ = converter.convert_to_csv(&dataset.log)?;
            }
            _ => {
                return Err(SarifError::custom(format!(
                    "Unknown operation: {}",
                    operation
                )));
            }
        }
        Ok(())
    }

    /// Get current memory usage (simplified implementation)
    fn get_memory_usage(&self) -> f64 {
        // In a real implementation, this would use system APIs to get actual memory usage
        // For now, return a placeholder value
        0.0
    }

    /// Generate a comprehensive benchmark report
    pub fn generate_report(&self) -> BenchmarkReport {
        BenchmarkReport {
            results: self.results.clone(),
            summary: self.generate_summary(),
            comparisons: self.generate_comparisons(),
        }
    }

    fn generate_summary(&self) -> BenchmarkSummary {
        let mut operation_summaries = HashMap::new();

        for result in &self.results {
            let summary = operation_summaries
                .entry(result.operation.clone())
                .or_insert_with(|| OperationSummary {
                    operation: result.operation.clone(),
                    total_runs: 0,
                    avg_duration: Duration::ZERO,
                    avg_throughput: 0.0,
                    avg_memory_mb: 0.0,
                });

            summary.total_runs += 1;
            summary.avg_duration += result.duration;
            summary.avg_throughput += result.throughput.results_per_second;
            summary.avg_memory_mb += result.memory_usage_mb;
        }

        // Calculate averages
        for summary in operation_summaries.values_mut() {
            summary.avg_duration /= summary.total_runs as u32;
            summary.avg_throughput /= summary.total_runs as f64;
            summary.avg_memory_mb /= summary.total_runs as f64;
        }

        BenchmarkSummary {
            operation_summaries,
            total_benchmarks: self.results.len(),
            fastest_operation: self.find_fastest_operation(),
            slowest_operation: self.find_slowest_operation(),
        }
    }

    fn generate_comparisons(&self) -> Vec<BenchmarkComparison> {
        // Generate comparisons between different datasets for the same operation
        let mut comparisons = Vec::new();

        for operation in &self.config.operations {
            let operation_results: Vec<_> = self
                .results
                .iter()
                .filter(|r| r.operation == *operation)
                .collect();

            if operation_results.len() > 1 {
                for i in 0..operation_results.len() {
                    for j in i + 1..operation_results.len() {
                        let baseline = operation_results[i];
                        let comparison = operation_results[j];

                        comparisons.push(BenchmarkComparison {
                            operation: operation.clone(),
                            baseline_dataset: baseline.dataset.clone(),
                            comparison_dataset: comparison.dataset.clone(),
                            duration_ratio: comparison.duration.as_secs_f64()
                                / baseline.duration.as_secs_f64(),
                            throughput_ratio: comparison.throughput.results_per_second
                                / baseline.throughput.results_per_second,
                            memory_ratio: comparison.memory_usage_mb / baseline.memory_usage_mb,
                        });
                    }
                }
            }
        }

        comparisons
    }

    fn find_fastest_operation(&self) -> Option<String> {
        self.results
            .iter()
            .min_by_key(|r| r.duration)
            .map(|r| r.operation.clone())
    }

    fn find_slowest_operation(&self) -> Option<String> {
        self.results
            .iter()
            .max_by_key(|r| r.duration)
            .map(|r| r.operation.clone())
    }
}

/// Comprehensive benchmark report
#[derive(Debug, Clone)]
pub struct BenchmarkReport {
    /// Individual benchmark results
    pub results: Vec<BenchmarkResult>,

    /// Summary statistics
    pub summary: BenchmarkSummary,

    /// Performance comparisons
    pub comparisons: Vec<BenchmarkComparison>,
}

/// Summary of benchmark results
#[derive(Debug, Clone)]
pub struct BenchmarkSummary {
    /// Summary by operation
    pub operation_summaries: HashMap<String, OperationSummary>,

    /// Total number of benchmarks run
    pub total_benchmarks: usize,

    /// Fastest operation overall
    pub fastest_operation: Option<String>,

    /// Slowest operation overall
    pub slowest_operation: Option<String>,
}

/// Summary for a specific operation
#[derive(Debug, Clone)]
pub struct OperationSummary {
    /// Operation name
    pub operation: String,

    /// Number of benchmark runs
    pub total_runs: usize,

    /// Average duration across all runs
    pub avg_duration: Duration,

    /// Average throughput across all runs
    pub avg_throughput: f64,

    /// Average memory usage across all runs
    pub avg_memory_mb: f64,
}

/// Comparison between benchmark results
#[derive(Debug, Clone)]
pub struct BenchmarkComparison {
    /// Operation being compared
    pub operation: String,

    /// Baseline dataset
    pub baseline_dataset: String,

    /// Comparison dataset
    pub comparison_dataset: String,

    /// Ratio of durations (comparison / baseline)
    pub duration_ratio: f64,

    /// Ratio of throughputs (comparison / baseline)
    pub throughput_ratio: f64,

    /// Ratio of memory usage (comparison / baseline)
    pub memory_ratio: f64,
}

// Default implementations

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            track_memory: true,
            enable_timing: true,
            memory_threshold_mb: 1024.0, // 1GB
            batch_size: 1000,
            enable_parallel: true,
            cache_config: CacheConfig::default(),
        }
    }
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            max_logs: 10,
            max_indexed_results: 100000,
            max_query_results: 10000,
            ttl_seconds: 3600, // 1 hour
        }
    }
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            iterations: 10,
            warmup_iterations: 3,
            profile_memory: true,
            operations: vec![
                "parse".to_string(),
                "index".to_string(),
                "query".to_string(),
                "conversion".to_string(),
            ],
        }
    }
}

/// Create a test dataset for benchmarking
pub fn create_test_dataset(
    name: &str,
    run_count: usize,
    results_per_run: usize,
) -> BenchmarkDataset {
    use crate::builder::SarifLogBuilder;

    let mut log_builder = SarifLogBuilder::new();

    for run_idx in 0..run_count {
        let tool_name = format!("test-tool-{}", run_idx);
        let mut run_builder = crate::builder::RunBuilder::with_tool(&tool_name, Some("1.0.0"));

        for result_idx in 0..results_per_run {
            let message = format!("Test result {} from run {}", result_idx, run_idx);
            let file_path = format!("test/file{}.rs", result_idx % 10);
            let line = (result_idx % 100) as i32 + 1;

            let result = crate::builder::ResultBuilder::with_text_message(&message)
                .with_rule_id(format!("RULE{:03}", result_idx % 50))
                .add_file_location(&file_path, line, 1)
                .build();

            run_builder = run_builder.add_result(result);
        }

        log_builder = log_builder.add_run(run_builder.build());
    }

    let log = log_builder.build_unchecked();
    let characteristics = DatasetCharacteristics {
        run_count,
        result_count: run_count * results_per_run,
        file_count: 10, // We cycle through 10 files
        rule_count: 50, // We cycle through 50 rules
        avg_results_per_file: (run_count * results_per_run) as f64 / 10.0,
        size_mb: estimate_log_size(&log),
    };

    BenchmarkDataset {
        name: name.to_string(),
        log,
        characteristics,
    }
}

/// Estimate the size of a SARIF log in MB
fn estimate_log_size(log: &SarifLog) -> f64 {
    // Rough estimation based on JSON serialization size
    match serde_json::to_string(log) {
        Ok(json) => json.len() as f64 / (1024.0 * 1024.0),
        Err(_) => 0.0,
    }
}

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

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

        // Test timing
        {
            let _handle = monitor.start_timing("test_operation");
            std::thread::sleep(std::time::Duration::from_millis(10));
        }

        // Test counter
        monitor.increment_counter("test_counter");
        monitor.increment_counter("test_counter");

        // Test memory tracking
        monitor.update_memory_stats("test_operation", 100.0);

        let report = monitor.generate_report();
        assert_eq!(report.counters["test_counter"], 2);
        assert!(report.operation_stats.contains_key("test_operation"));
        assert_eq!(report.memory_stats.current_memory_mb, 100.0);
    }

    #[test]
    fn test_memory_pool() {
        let mut pool = MemoryPool::new(|| String::new(), 5);

        // Borrow and return objects
        let obj1 = pool.borrow();
        let obj2 = pool.borrow();

        pool.return_object(obj1);
        pool.return_object(obj2);

        let stats = pool.get_stats();
        assert_eq!(stats.borrowed.load(Ordering::Relaxed), 2);
        assert_eq!(stats.returned.load(Ordering::Relaxed), 2);
    }

    #[test]
    fn test_string_interner() {
        let mut interner = StringInterner::new();

        let str1 = interner.intern("test");
        let str2 = interner.intern("test");

        assert!(Arc::ptr_eq(&str1, &str2));

        let stats = interner.get_stats();
        assert_eq!(stats.total_interned, 1);
        assert_eq!(stats.cache_hits, 1);
    }

    #[test]
    fn test_computation_cache() {
        let mut cache = ComputationCache::new();

        // Test fingerprint caching
        let fp1 = cache.get_fingerprint("key1", || "fingerprint1".to_string());
        let fp2 = cache.get_fingerprint("key1", || "fingerprint2".to_string());

        assert_eq!(fp1, "fingerprint1");
        assert_eq!(fp2, "fingerprint1"); // Should return cached value

        let stats = cache.get_stats();
        assert_eq!(stats.hits_by_type.get("fingerprint"), Some(&1));
        assert_eq!(stats.misses_by_type.get("fingerprint"), Some(&1));
    }

    #[test]
    fn test_benchmark_dataset_creation() {
        let dataset = create_test_dataset("test", 2, 10);

        assert_eq!(dataset.name, "test");
        assert_eq!(dataset.characteristics.run_count, 2);
        assert_eq!(dataset.characteristics.result_count, 20);
        assert_eq!(dataset.log.runs.len(), 2);
    }

    #[test]
    fn test_benchmark_execution() {
        let config = BenchmarkConfig {
            iterations: 2,
            warmup_iterations: 1,
            profile_memory: false,
            operations: vec!["parse".to_string()],
        };

        let mut benchmark = SarifBenchmark::new(config);
        let dataset = create_test_dataset("small", 1, 5);
        benchmark.add_dataset(dataset);

        let results = benchmark.run_benchmarks().unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].operation, "parse");
        assert_eq!(results[0].dataset, "small");
    }
}