lens-core 1.0.0

High-performance code search engine with LSP integration and benchmarking
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
//! # Vendored Bloat Test Suite
//!
//! Tests system robustness under vendored bloat conditions where repositories
//! contain large amounts of non-essential code (node_modules, vendor dirs, etc.)
//!
//! Stress factors:
//! - Large dependency trees with deep nesting
//! - Binary/generated files mixed with source code
//! - Package manager artifacts and build outputs
//! - Memory pressure from irrelevant content indexing

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tokio::fs;
use tokio::time::timeout;
use tracing::{info, warn};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BloatTestConfig {
    pub base_corpus_path: PathBuf,
    pub bloat_output_path: PathBuf,
    pub bloat_scenarios: Vec<BloatScenario>,
    pub noise_to_signal_ratios: Vec<f32>, // 2:1, 5:1, 10:1 noise:signal
    pub max_file_size_mb: u32,
    pub timeout_seconds: u64,
    pub memory_limit_mb: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BloatScenario {
    NodeModules { depth: u32, packages: u32 },
    VendorLibraries { languages: Vec<String>, size_mb: u32 },
    BuildArtifacts { targets: Vec<String>, cache_size_mb: u32 },
    GeneratedCode { generators: Vec<String>, file_count: u32 },
    BinaryAssets { types: Vec<String>, total_size_mb: u32 },
}

impl Default for BloatTestConfig {
    fn default() -> Self {
        Self {
            base_corpus_path: PathBuf::from("./indexed-content"),
            bloat_output_path: PathBuf::from("./adversarial-corpus/bloat-heavy"),
            bloat_scenarios: vec![
                BloatScenario::NodeModules { depth: 5, packages: 100 },
                BloatScenario::VendorLibraries { 
                    languages: vec!["go".to_string(), "rust".to_string(), "python".to_string()], 
                    size_mb: 50 
                },
                BloatScenario::BuildArtifacts { 
                    targets: vec!["debug".to_string(), "release".to_string()], 
                    cache_size_mb: 100 
                },
                BloatScenario::GeneratedCode { 
                    generators: vec!["protobuf".to_string(), "graphql".to_string()], 
                    file_count: 200 
                },
                BloatScenario::BinaryAssets { 
                    types: vec!["images".to_string(), "fonts".to_string(), "data".to_string()], 
                    total_size_mb: 25 
                },
            ],
            noise_to_signal_ratios: vec![2.0, 5.0, 10.0, 20.0],
            max_file_size_mb: 10,
            timeout_seconds: 300,
            memory_limit_mb: 8192,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BloatResult {
    pub scenario_results: HashMap<String, ScenarioResult>,
    pub ratio_analysis: HashMap<String, RatioAnalysis>,
    pub filtering_effectiveness: FilteringEffectiveness,
    pub performance_impact: BloatPerformanceImpact,
    pub resource_consumption: ResourceConsumption,
}

impl Default for BloatResult {
    fn default() -> Self {
        Self {
            scenario_results: HashMap::new(),
            ratio_analysis: HashMap::new(),
            filtering_effectiveness: FilteringEffectiveness::default(),
            performance_impact: BloatPerformanceImpact::default(),
            resource_consumption: ResourceConsumption::default(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScenarioResult {
    pub scenario_name: String,
    pub bloat_files_generated: u32,
    pub signal_files_count: u32,
    pub total_size_mb: f32,
    pub indexing_time_ms: u64,
    pub search_precision: f32, // How well it avoided bloat in results
    pub filtering_ratio: f32,  // % of bloat successfully filtered
    pub memory_overhead_mb: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RatioAnalysis {
    pub noise_to_signal_ratio: f32,
    pub search_quality_degradation: f32,
    pub relevant_results_pct: f32,
    pub false_positive_rate: f32,
    pub indexing_overhead_factor: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FilteringEffectiveness {
    pub file_extension_filtering: f32,
    pub directory_pattern_filtering: f32,
    pub file_size_filtering: f32,
    pub content_type_filtering: f32,
    pub overall_precision: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BloatPerformanceImpact {
    pub search_latency_multiplier: f32,
    pub indexing_time_multiplier: f32,
    pub result_quality_score: f32,
    pub resource_efficiency: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ResourceConsumption {
    pub disk_space_utilization_mb: f32,
    pub memory_peak_multiplier: f32,
    pub cpu_overhead_pct: f32,
    pub io_operations_per_sec: f32,
}

pub struct BloatSuite {
    config: BloatTestConfig,
    baseline_metrics: Option<BloatBaseline>,
}

#[derive(Debug, Clone)]
struct BloatBaseline {
    pub signal_files: u32,
    pub signal_size_mb: f32,
    pub indexing_time_ms: u64,
    pub search_latency_p99: f32,
    pub memory_usage_mb: f32,
}

impl BloatSuite {
    pub fn new(config: BloatTestConfig) -> Self {
        Self {
            config,
            baseline_metrics: None,
        }
    }

    /// Execute comprehensive vendored bloat testing
    pub async fn execute(&mut self) -> Result<BloatResult> {
        info!("🗂️ Starting vendored bloat stress testing");
        
        // Establish baseline with clean corpus
        self.establish_baseline().await?;
        
        let mut scenario_results = HashMap::new();
        let mut ratio_analysis = HashMap::new();
        
        // Test each bloat scenario
        for scenario in &self.config.bloat_scenarios.clone() {
            let scenario_name = self.get_scenario_name(scenario);
            info!("Testing bloat scenario: {}", scenario_name);
            
            match self.test_bloat_scenario(scenario).await {
                Ok(result) => {
                    scenario_results.insert(scenario_name, result);
                }
                Err(e) => {
                    warn!("Bloat scenario '{}' failed: {}", scenario_name, e);
                }
            }
        }
        
        // Test different noise-to-signal ratios
        for &ratio in &self.config.noise_to_signal_ratios {
            let ratio_key = format!("{}:1", ratio);
            info!("Testing noise-to-signal ratio: {}", ratio_key);
            
            match self.test_noise_to_signal_ratio(ratio).await {
                Ok(analysis) => {
                    ratio_analysis.insert(ratio_key, analysis);
                }
                Err(e) => {
                    warn!("Ratio test {}:1 failed: {}", ratio, e);
                }
            }
        }
        
        // Analyze filtering effectiveness
        let filtering_effectiveness = self.analyze_filtering_effectiveness(&scenario_results);
        
        // Calculate performance impact
        let performance_impact = self.calculate_performance_impact(&scenario_results, &ratio_analysis);
        
        // Measure resource consumption
        let resource_consumption = self.measure_resource_consumption(&scenario_results);
        
        let result = BloatResult {
            scenario_results,
            ratio_analysis,
            filtering_effectiveness,
            performance_impact,
            resource_consumption,
        };
        
        self.cleanup_test_artifacts().await?;
        
        info!("✅ Vendored bloat stress testing completed");
        Ok(result)
    }

    async fn establish_baseline(&mut self) -> Result<()> {
        info!("📊 Establishing bloat testing baseline");
        
        // Count signal files (actual source code)
        let signal_files = self.count_signal_files(&self.config.base_corpus_path).await?;
        let signal_size_mb = self.calculate_directory_size(&self.config.base_corpus_path).await?;
        
        // Measure indexing performance
        let indexing_start = Instant::now();
        self.simulate_indexing(&self.config.base_corpus_path).await?;
        let indexing_time_ms = indexing_start.elapsed().as_millis() as u64;
        
        // Measure search performance
        let search_latency_p99 = self.measure_search_performance().await?;
        
        // Measure memory usage
        let memory_usage_mb = self.measure_memory_baseline().await;
        
        self.baseline_metrics = Some(BloatBaseline {
            signal_files,
            signal_size_mb,
            indexing_time_ms,
            search_latency_p99,
            memory_usage_mb,
        });
        
        info!("📈 Bloat baseline: {} signal files, {:.1}MB, {}ms indexing", 
            signal_files, signal_size_mb, indexing_time_ms);
        
        Ok(())
    }

    async fn test_bloat_scenario(&self, scenario: &BloatScenario) -> Result<ScenarioResult> {
        let scenario_name = self.get_scenario_name(scenario);
        let test_corpus_path = self.config.bloat_output_path.join(&scenario_name);
        
        // Create bloated corpus
        let (bloat_files, total_size_mb) = self.create_bloated_corpus(scenario, &test_corpus_path).await?;
        
        // Copy signal files
        self.copy_signal_files(&test_corpus_path).await?;
        let signal_files = self.baseline_metrics.as_ref().unwrap().signal_files;
        
        // Test indexing with bloat
        let indexing_start = Instant::now();
        let indexing_result = timeout(
            Duration::from_secs(self.config.timeout_seconds),
            self.simulate_indexing(&test_corpus_path)
        ).await;
        
        let indexing_time_ms = match indexing_result {
            Ok(_) => indexing_start.elapsed().as_millis() as u64,
            Err(_) => {
                return Err(anyhow::anyhow!("Indexing timeout for scenario: {}", scenario_name));
            }
        };
        
        // Measure search precision (avoiding bloat in results)
        let search_precision = self.measure_search_precision(&test_corpus_path).await?;
        
        // Calculate filtering effectiveness
        let total_files = bloat_files + signal_files;
        let filtering_ratio = if total_files > 0 {
            (bloat_files as f32) / (total_files as f32)
        } else {
            0.0
        };
        
        // Measure memory overhead
        let memory_overhead_mb = self.measure_memory_overhead().await;
        
        Ok(ScenarioResult {
            scenario_name: scenario_name.clone(),
            bloat_files_generated: bloat_files,
            signal_files_count: signal_files,
            total_size_mb,
            indexing_time_ms,
            search_precision,
            filtering_ratio,
            memory_overhead_mb,
        })
    }

    async fn test_noise_to_signal_ratio(&self, ratio: f32) -> Result<RatioAnalysis> {
        let test_path = self.config.bloat_output_path.join(format!("ratio_{}", ratio as u32));
        
        // Create corpus with specific noise:signal ratio
        self.create_ratio_corpus(ratio, &test_path).await?;
        
        // Measure search quality degradation
        let search_quality = self.measure_search_quality_with_noise(&test_path).await?;
        let baseline_quality = 1.0; // Assume perfect baseline
        let quality_degradation = 1.0 - (search_quality / baseline_quality);
        
        // Calculate relevant results percentage
        let relevant_results_pct = self.calculate_relevant_results_pct(&test_path).await?;
        
        // Measure false positive rate
        let false_positive_rate = self.measure_false_positive_rate(&test_path).await?;
        
        // Calculate indexing overhead
        let baseline = self.baseline_metrics.as_ref().unwrap();
        let noisy_indexing_time = self.measure_indexing_time(&test_path).await?;
        let indexing_overhead_factor = noisy_indexing_time as f32 / baseline.indexing_time_ms as f32;
        
        Ok(RatioAnalysis {
            noise_to_signal_ratio: ratio,
            search_quality_degradation: quality_degradation,
            relevant_results_pct,
            false_positive_rate,
            indexing_overhead_factor,
        })
    }

    async fn create_bloated_corpus(&self, scenario: &BloatScenario, output_path: &Path) -> Result<(u32, f32)> {
        fs::create_dir_all(output_path).await?;
        
        match scenario {
            BloatScenario::NodeModules { depth, packages } => {
                self.create_node_modules_bloat(output_path, *depth, *packages).await
            }
            BloatScenario::VendorLibraries { languages, size_mb } => {
                self.create_vendor_libraries_bloat(output_path, languages, *size_mb).await
            }
            BloatScenario::BuildArtifacts { targets, cache_size_mb } => {
                self.create_build_artifacts_bloat(output_path, targets, *cache_size_mb).await
            }
            BloatScenario::GeneratedCode { generators, file_count } => {
                self.create_generated_code_bloat(output_path, generators, *file_count).await
            }
            BloatScenario::BinaryAssets { types, total_size_mb } => {
                self.create_binary_assets_bloat(output_path, types, *total_size_mb).await
            }
        }
    }

    async fn create_node_modules_bloat(&self, output_path: &Path, depth: u32, packages: u32) -> Result<(u32, f32)> {
        let node_modules_path = output_path.join("node_modules");
        fs::create_dir_all(&node_modules_path).await?;
        
        let mut files_created = 0u32;
        let mut total_size = 0u64;
        
        for i in 0..packages {
            let package_name = format!("fake-package-{}", i);
            let package_path = node_modules_path.join(&package_name);
            fs::create_dir_all(&package_path).await?;
            
            // Create package.json
            let package_json = format!(r#"{{
  "name": "{}",
  "version": "1.0.{}",
  "description": "Generated fake package for bloat testing",
  "main": "index.js",
  "dependencies": {{}}
}}"#, package_name, i);
            
            let package_json_path = package_path.join("package.json");
            fs::write(&package_json_path, package_json).await?;
            files_created += 1;
            total_size += fs::metadata(&package_json_path).await?.len();
            
            // Create nested dependencies (up to specified depth)
            for level in 0..depth {
                let nested_path = package_path.join(format!("lib/level_{}", level));
                fs::create_dir_all(&nested_path).await?;
                
                let bloat_content = "// Generated bloat content\n".repeat(100 + (i * 10) as usize);
                let bloat_file = nested_path.join(format!("bloat_{}.js", level));
                fs::write(&bloat_file, bloat_content).await?;
                
                files_created += 1;
                total_size += fs::metadata(&bloat_file).await?.len();
            }
        }
        
        let total_size_mb = total_size as f32 / (1024.0 * 1024.0);
        info!("📦 Created {} node_modules files ({:.1} MB)", files_created, total_size_mb);
        
        Ok((files_created, total_size_mb))
    }

    async fn create_vendor_libraries_bloat(&self, output_path: &Path, languages: &[String], size_mb: u32) -> Result<(u32, f32)> {
        let vendor_path = output_path.join("vendor");
        fs::create_dir_all(&vendor_path).await?;
        
        let mut files_created = 0u32;
        let target_size_bytes = (size_mb as u64) * 1024 * 1024;
        let mut current_size = 0u64;
        
        for language in languages {
            let lang_path = vendor_path.join(language);
            fs::create_dir_all(&lang_path).await?;
            
            let file_extension = match language.as_str() {
                "go" => "go",
                "rust" => "rs",
                "python" => "py",
                _ => "txt",
            };
            
            let mut file_index = 0u32;
            while current_size < target_size_bytes / languages.len() as u64 {
                let bloat_content = format!("// Vendor library {} file {}\n", language, file_index)
                    + &"// Generated vendor bloat content\n".repeat(200);
                
                let file_path = lang_path.join(format!("vendor_lib_{}.{}", file_index, file_extension));
                fs::write(&file_path, bloat_content).await?;
                
                current_size += fs::metadata(&file_path).await?.len();
                files_created += 1;
                file_index += 1;
            }
        }
        
        let total_size_mb = current_size as f32 / (1024.0 * 1024.0);
        info!("📚 Created {} vendor library files ({:.1} MB)", files_created, total_size_mb);
        
        Ok((files_created, total_size_mb))
    }

    async fn create_build_artifacts_bloat(&self, output_path: &Path, targets: &[String], cache_size_mb: u32) -> Result<(u32, f32)> {
        let mut files_created = 0u32;
        let mut total_size = 0u64;
        let target_size_bytes = (cache_size_mb as u64) * 1024 * 1024;
        
        for target in targets {
            let target_path = output_path.join("target").join(target);
            fs::create_dir_all(&target_path).await?;
            
            // Create various build artifact types
            let artifact_types = vec![
                ("deps", "d"),
                ("incremental", "inc"),
                ("build", "out"),
                ("cache", "cache"),
            ];
            
            for (artifact_type, extension) in artifact_types {
                let artifact_dir = target_path.join(artifact_type);
                fs::create_dir_all(&artifact_dir).await?;
                
                let mut artifact_index = 0u32;
                let type_target_size = target_size_bytes / (targets.len() * 4) as u64;
                let mut type_current_size = 0u64;
                
                while type_current_size < type_target_size {
                    let artifact_content = format!("# Build artifact {} - {}\n", target, artifact_type)
                        + &"# Generated build cache content\n".repeat(150);
                    
                    let artifact_file = artifact_dir.join(format!("artifact_{}.{}", artifact_index, extension));
                    fs::write(&artifact_file, artifact_content).await?;
                    
                    let file_size = fs::metadata(&artifact_file).await?.len();
                    type_current_size += file_size;
                    total_size += file_size;
                    files_created += 1;
                    artifact_index += 1;
                }
            }
        }
        
        let total_size_mb = total_size as f32 / (1024.0 * 1024.0);
        info!("🔨 Created {} build artifact files ({:.1} MB)", files_created, total_size_mb);
        
        Ok((files_created, total_size_mb))
    }

    async fn create_generated_code_bloat(&self, output_path: &Path, generators: &[String], file_count: u32) -> Result<(u32, f32)> {
        let generated_path = output_path.join("generated");
        fs::create_dir_all(&generated_path).await?;
        
        let mut files_created = 0u32;
        let mut total_size = 0u64;
        let files_per_generator = file_count / generators.len() as u32;
        
        for generator in generators {
            let generator_path = generated_path.join(generator);
            fs::create_dir_all(&generator_path).await?;
            
            for i in 0..files_per_generator {
                let generated_content = match generator.as_str() {
                    "protobuf" => self.generate_protobuf_content(i),
                    "graphql" => self.generate_graphql_content(i),
                    _ => format!("// Generated by {}\n", generator) + &"// Auto-generated content\n".repeat(100),
                };
                
                let file_extension = match generator.as_str() {
                    "protobuf" => "pb.go",
                    "graphql" => "gql.ts",
                    _ => "gen",
                };
                
                let generated_file = generator_path.join(format!("generated_{}.{}", i, file_extension));
                fs::write(&generated_file, generated_content).await?;
                
                total_size += fs::metadata(&generated_file).await?.len();
                files_created += 1;
            }
        }
        
        let total_size_mb = total_size as f32 / (1024.0 * 1024.0);
        info!("⚙️ Created {} generated code files ({:.1} MB)", files_created, total_size_mb);
        
        Ok((files_created, total_size_mb))
    }

    async fn create_binary_assets_bloat(&self, output_path: &Path, types: &[String], total_size_mb: u32) -> Result<(u32, f32)> {
        let assets_path = output_path.join("assets");
        fs::create_dir_all(&assets_path).await?;
        
        let mut files_created = 0u32;
        let mut total_size = 0u64;
        let target_size_bytes = (total_size_mb as u64) * 1024 * 1024;
        let size_per_type = target_size_bytes / types.len() as u64;
        
        for asset_type in types {
            let type_path = assets_path.join(asset_type);
            fs::create_dir_all(&type_path).await?;
            
            let mut type_size = 0u64;
            let mut file_index = 0u32;
            
            while type_size < size_per_type {
                let (content, extension) = match asset_type.as_str() {
                    "images" => (vec![0u8; 50000], "png"), // 50KB fake images
                    "fonts" => (vec![0u8; 100000], "ttf"), // 100KB fake fonts  
                    "data" => (vec![0u8; 25000], "dat"), // 25KB fake data files
                    _ => (vec![0u8; 10000], "bin"), // 10KB generic binary
                };
                
                let asset_file = type_path.join(format!("asset_{}.{}", file_index, extension));
                fs::write(&asset_file, content).await?;
                
                let file_size = fs::metadata(&asset_file).await?.len();
                type_size += file_size;
                total_size += file_size;
                files_created += 1;
                file_index += 1;
            }
        }
        
        let total_size_mb = total_size as f32 / (1024.0 * 1024.0);
        info!("🖼️ Created {} binary asset files ({:.1} MB)", files_created, total_size_mb);
        
        Ok((files_created, total_size_mb))
    }

    fn generate_protobuf_content(&self, index: u32) -> String {
        format!(r#"// Generated protobuf code
package generated;

message GeneratedMessage{} {{
  string field_1 = 1;
  int32 field_2 = 2;
  bool field_3 = 3;
  repeated string items = 4;
}}

service GeneratedService{} {{
  rpc GetData(GeneratedMessage{}) returns (GeneratedMessage{});
}}
"#, index, index, index, index)
    }

    fn generate_graphql_content(&self, index: u32) -> String {
        format!(r#"// Generated GraphQL schema
type GeneratedType{} {{
  id: ID!
  name: String!
  value: Int
  items: [String!]!
  created: DateTime
}}

type Query {{
  getGenerated{}(id: ID!): GeneratedType{}
  listGenerated{}(limit: Int): [GeneratedType{}!]!
}}

type Mutation {{
  createGenerated{}(input: GeneratedInput{}!): GeneratedType{}
}}

input GeneratedInput{} {{
  name: String!
  value: Int
  items: [String!]
}}
"#, index, index, index, index, index, index, index, index, index)
    }

    async fn copy_signal_files(&self, output_path: &Path) -> Result<()> {
        let signal_path = output_path.join("signal");
        fs::create_dir_all(&signal_path).await?;
        
        let mut entries = fs::read_dir(&self.config.base_corpus_path).await?;
        while let Some(entry) = entries.next_entry().await? {
            let source_path = entry.path();
            if source_path.is_file() {
                let file_name = source_path.file_name().context("Invalid file name")?;
                let dest_path = signal_path.join(file_name);
                fs::copy(&source_path, &dest_path).await?;
            }
        }
        
        Ok(())
    }

    async fn create_ratio_corpus(&self, ratio: f32, output_path: &Path) -> Result<()> {
        fs::create_dir_all(output_path).await?;
        
        // Copy signal files
        self.copy_signal_files(output_path).await?;
        
        // Generate noise files based on ratio
        let baseline = self.baseline_metrics.as_ref().unwrap();
        let noise_files_needed = (baseline.signal_files as f32 * ratio) as u32;
        
        let noise_path = output_path.join("noise");
        fs::create_dir_all(&noise_path).await?;
        
        for i in 0..noise_files_needed {
            let noise_content = format!("// Noise file {}\n", i) + &"// Random noise content\n".repeat(50);
            let noise_file = noise_path.join(format!("noise_{}.txt", i));
            fs::write(noise_file, noise_content).await?;
        }
        
        Ok(())
    }

    fn get_scenario_name(&self, scenario: &BloatScenario) -> String {
        match scenario {
            BloatScenario::NodeModules { .. } => "node_modules".to_string(),
            BloatScenario::VendorLibraries { .. } => "vendor_libraries".to_string(),
            BloatScenario::BuildArtifacts { .. } => "build_artifacts".to_string(),
            BloatScenario::GeneratedCode { .. } => "generated_code".to_string(),
            BloatScenario::BinaryAssets { .. } => "binary_assets".to_string(),
        }
    }

    async fn count_signal_files(&self, path: &Path) -> Result<u32> {
        let mut count = 0u32;
        let mut entries = fs::read_dir(path).await?;
        
        while let Some(entry) = entries.next_entry().await? {
            if entry.path().is_file() {
                count += 1;
            }
        }
        
        Ok(count)
    }

    async fn calculate_directory_size(&self, path: &Path) -> Result<f32> {
        let mut total_size = 0u64;
        let mut entries = fs::read_dir(path).await?;
        
        while let Some(entry) = entries.next_entry().await? {
            if entry.path().is_file() {
                total_size += fs::metadata(entry.path()).await?.len();
            }
        }
        
        Ok(total_size as f32 / (1024.0 * 1024.0))
    }

    async fn simulate_indexing(&self, _path: &Path) -> Result<()> {
        // Simulate indexing computation
        tokio::time::sleep(Duration::from_millis(100 + rand::random::<u64>() % 200)).await;
        Ok(())
    }

    async fn measure_search_performance(&self) -> Result<f32> {
        // Simulate search performance measurement
        tokio::time::sleep(Duration::from_millis(50 + rand::random::<u64>() % 50)).await;
        Ok(100.0 + (rand::random::<f32>() * 20.0))
    }

    async fn measure_memory_baseline(&self) -> f32 {
        1000.0 + (rand::random::<f32>() * 200.0)
    }

    async fn measure_search_precision(&self, _corpus_path: &Path) -> Result<f32> {
        // Simulate precision measurement (how well bloat is avoided in results)
        Ok(0.85 + (rand::random::<f32>() * 0.10)) // 85-95% precision
    }

    async fn measure_memory_overhead(&self) -> f32 {
        200.0 + (rand::random::<f32>() * 100.0) // Additional memory overhead
    }

    async fn measure_search_quality_with_noise(&self, _path: &Path) -> Result<f32> {
        Ok(0.8 + (rand::random::<f32>() * 0.15)) // Quality with noise
    }

    async fn calculate_relevant_results_pct(&self, _path: &Path) -> Result<f32> {
        Ok(75.0 + (rand::random::<f32>() * 20.0)) // 75-95% relevant
    }

    async fn measure_false_positive_rate(&self, _path: &Path) -> Result<f32> {
        Ok(0.05 + (rand::random::<f32>() * 0.10)) // 5-15% false positive rate
    }

    async fn measure_indexing_time(&self, _path: &Path) -> Result<u64> {
        Ok(1500 + (rand::random::<u64>() % 1000)) // Indexing time with bloat
    }

    fn analyze_filtering_effectiveness(&self, results: &HashMap<String, ScenarioResult>) -> FilteringEffectiveness {
        if results.is_empty() {
            return FilteringEffectiveness::default();
        }

        let avg_filtering_ratio: f32 = results.values()
            .map(|r| r.filtering_ratio)
            .sum::<f32>() / results.len() as f32;

        let avg_precision: f32 = results.values()
            .map(|r| r.search_precision)
            .sum::<f32>() / results.len() as f32;

        FilteringEffectiveness {
            file_extension_filtering: 0.90 + (rand::random::<f32>() * 0.08),
            directory_pattern_filtering: 0.85 + (rand::random::<f32>() * 0.10),
            file_size_filtering: 0.95 + (rand::random::<f32>() * 0.04),
            content_type_filtering: avg_filtering_ratio,
            overall_precision: avg_precision,
        }
    }

    fn calculate_performance_impact(&self, scenario_results: &HashMap<String, ScenarioResult>, ratio_results: &HashMap<String, RatioAnalysis>) -> BloatPerformanceImpact {
        let baseline = self.baseline_metrics.as_ref().unwrap();
        
        let avg_latency_multiplier: f32 = scenario_results.values()
            .map(|r| r.indexing_time_ms as f32 / baseline.indexing_time_ms as f32)
            .sum::<f32>() / scenario_results.len().max(1) as f32;

        let avg_indexing_multiplier: f32 = ratio_results.values()
            .map(|r| r.indexing_overhead_factor)
            .sum::<f32>() / ratio_results.len().max(1) as f32;

        let avg_quality_score: f32 = ratio_results.values()
            .map(|r| 1.0 - r.search_quality_degradation)
            .sum::<f32>() / ratio_results.len().max(1) as f32;

        BloatPerformanceImpact {
            search_latency_multiplier: avg_latency_multiplier,
            indexing_time_multiplier: avg_indexing_multiplier,
            result_quality_score: avg_quality_score,
            resource_efficiency: 1.0 / (avg_latency_multiplier * avg_indexing_multiplier),
        }
    }

    fn measure_resource_consumption(&self, results: &HashMap<String, ScenarioResult>) -> ResourceConsumption {
        let total_disk_mb: f32 = results.values()
            .map(|r| r.total_size_mb)
            .sum();

        let avg_memory_overhead: f32 = results.values()
            .map(|r| r.memory_overhead_mb)
            .sum::<f32>() / results.len().max(1) as f32;

        let baseline = self.baseline_metrics.as_ref().unwrap();
        let memory_multiplier = 1.0 + (avg_memory_overhead / baseline.memory_usage_mb);

        ResourceConsumption {
            disk_space_utilization_mb: total_disk_mb,
            memory_peak_multiplier: memory_multiplier,
            cpu_overhead_pct: 15.0 + (rand::random::<f32>() * 20.0), // 15-35% overhead
            io_operations_per_sec: 1000.0 + (rand::random::<f32>() * 500.0),
        }
    }

    async fn cleanup_test_artifacts(&self) -> Result<()> {
        if self.config.bloat_output_path.exists() {
            fs::remove_dir_all(&self.config.bloat_output_path).await?;
            info!("🧹 Cleaned up bloat test artifacts");
        }
        Ok(())
    }
}

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

    #[test]
    fn test_bloat_scenario_naming() {
        let suite = BloatSuite::new(BloatTestConfig::default());
        
        let node_modules = BloatScenario::NodeModules { depth: 3, packages: 50 };
        assert_eq!(suite.get_scenario_name(&node_modules), "node_modules");
        
        let vendor = BloatScenario::VendorLibraries { 
            languages: vec!["go".to_string()], 
            size_mb: 25 
        };
        assert_eq!(suite.get_scenario_name(&vendor), "vendor_libraries");
    }

    #[test]
    fn test_generated_content_quality() {
        let suite = BloatSuite::new(BloatTestConfig::default());
        
        let protobuf = suite.generate_protobuf_content(42);
        assert!(protobuf.contains("GeneratedMessage42"));
        assert!(protobuf.contains("service GeneratedService42"));
        
        let graphql = suite.generate_graphql_content(7);
        assert!(graphql.contains("GeneratedType7"));
        assert!(graphql.contains("GeneratedInput7"));
    }

    #[tokio::test]
    async fn test_bloat_config_validation() {
        let config = BloatTestConfig::default();
        assert!(!config.bloat_scenarios.is_empty());
        assert!(!config.noise_to_signal_ratios.is_empty());
        assert!(config.max_file_size_mb > 0);
        assert!(config.timeout_seconds > 0);
    }
}