codanna 0.9.21

Code Intelligence for Large Language Models
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
//! Kotlin Parser Performance Benchmarks
//!
//! This benchmark suite validates that the Kotlin parser meets performance targets:
//! - >10,000 symbols/second extraction speed
//! - Memory usage within acceptable limits
//! - Performance comparable to other language parsers
//! - Scalability with large codebases
//!
//! The benchmark suite measures:
//! 1. Symbol extraction performance with varying code complexity
//! 2. Memory usage patterns with different file sizes
//! 3. Parser initialization overhead
//! 4. Performance on specific Kotlin language constructs
//! 5. Scalability with systematically generated test data

use codanna::parsing::LanguageParser;
use codanna::parsing::kotlin::KotlinParser;
use codanna::types::{FileId, SymbolCounter};
use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use std::hint::black_box;

// Performance targets and constants
#[allow(dead_code)]
const TARGET_SYMBOLS_PER_SEC: u64 = 10_000;
#[allow(dead_code)]
const LARGE_FILE_SYMBOL_COUNT: usize = 1000;
#[allow(dead_code)]
const BENCHMARK_ITERATIONS: usize = 100;

/// Benchmark basic Kotlin symbol extraction performance
fn bench_kotlin_symbol_extraction(c: &mut Criterion) {
    let mut group = c.benchmark_group("kotlin_symbol_extraction");

    // Test with different Kotlin code samples of varying complexity
    let test_cases = vec![
        ("basic_kotlin", create_basic_kotlin_code()),
        ("medium_kotlin", create_medium_complexity_kotlin_code()),
        ("complex_kotlin", create_complex_kotlin_code()),
        ("real_world_kotlin", create_real_world_kotlin_code()),
    ];

    for (name, source_code) in test_cases {
        let symbol_count = count_expected_symbols(&source_code);
        group.throughput(Throughput::Elements(symbol_count as u64));

        group.bench_with_input(
            BenchmarkId::new("symbol_extraction", name),
            &source_code,
            |b, code| {
                let mut parser = KotlinParser::new().expect("Failed to create Kotlin parser");
                b.iter(|| {
                    let mut symbol_counter = SymbolCounter::new();
                    let file_id = FileId::new(1).expect("Failed to create file ID");
                    let symbols =
                        parser.parse(std::hint::black_box(code), file_id, &mut symbol_counter);
                    std::hint::black_box(symbols)
                });
            },
        );
    }

    group.finish();
}

/// Benchmark memory usage patterns
fn bench_kotlin_memory_usage(c: &mut Criterion) {
    let mut group = c.benchmark_group("kotlin_memory_usage");

    // Test memory usage with increasingly large Kotlin files
    let sizes = vec![100, 500, 1000, 2000, 5000];

    for size in sizes {
        let source_code = create_large_kotlin_file(size);
        group.throughput(Throughput::Elements(size as u64));

        group.bench_with_input(
            BenchmarkId::new("large_file_parsing", size),
            &source_code,
            |b, code| {
                b.iter(|| {
                    let mut parser = KotlinParser::new().expect("Failed to create Kotlin parser");
                    let mut symbol_counter = SymbolCounter::new();
                    let file_id = FileId::new(1).expect("Failed to create file ID");
                    let symbols =
                        parser.parse(std::hint::black_box(code), file_id, &mut symbol_counter);
                    std::hint::black_box(symbols)
                });
            },
        );
    }

    group.finish();
}

/// Benchmark parser initialization overhead
fn bench_parser_initialization(c: &mut Criterion) {
    let mut group = c.benchmark_group("parser_initialization");

    group.bench_function("kotlin_parser_creation", |b| {
        b.iter(|| {
            let parser = KotlinParser::new();
            std::hint::black_box(parser)
        });
    });

    group.finish();
}

/// Benchmark specific Kotlin language constructs
fn bench_kotlin_language_constructs(c: &mut Criterion) {
    let mut group = c.benchmark_group("kotlin_language_constructs");

    let construct_tests = vec![
        ("functions", create_many_functions(100)),
        ("classes", create_many_classes(50)),
        ("interfaces", create_many_interfaces(30)),
        ("methods", create_many_methods(100)),
        ("data_classes", create_many_data_classes(50)),
        ("objects", create_many_objects(30)),
        ("properties", create_many_properties(100)),
        ("enums", create_many_enums(20)),
    ];

    for (construct_name, source_code) in construct_tests {
        let symbol_count = count_expected_symbols(&source_code);
        group.throughput(Throughput::Elements(symbol_count as u64));

        group.bench_with_input(
            BenchmarkId::new("construct_parsing", construct_name),
            &source_code,
            |b, code| {
                let mut parser = KotlinParser::new().expect("Failed to create Kotlin parser");
                b.iter(|| {
                    let mut symbol_counter = SymbolCounter::new();
                    let file_id = FileId::new(1).expect("Failed to create file ID");
                    let symbols =
                        parser.parse(std::hint::black_box(code), file_id, &mut symbol_counter);
                    std::hint::black_box(symbols)
                });
            },
        );
    }

    group.finish();
}

/// Benchmark scalable test data generation
fn bench_scalable_test_data(c: &mut Criterion) {
    let mut group = c.benchmark_group("scalable_test_data");

    // Test with different data sizes to measure scalability
    let data_sizes = vec![100, 500, 1000, 2000, 5000, 10000];

    for size in data_sizes {
        let source_code = generate_scalable_kotlin_code(size);
        let symbol_count = count_expected_symbols(&source_code);
        group.throughput(Throughput::Elements(symbol_count as u64));

        group.bench_with_input(
            BenchmarkId::new("generated_data", size),
            &source_code,
            |b, code| {
                let mut parser = KotlinParser::new().expect("Failed to create Kotlin parser");
                b.iter(|| {
                    let mut symbol_counter = SymbolCounter::new();
                    let file_id = FileId::new(1).expect("Failed to create file ID");
                    let symbols =
                        parser.parse(std::hint::black_box(code), file_id, &mut symbol_counter);
                    std::hint::black_box(symbols.len()) // Return count to avoid large memory allocations
                });
            },
        );
    }

    group.finish();
}

/// Benchmark document comment extraction performance
fn bench_doc_comment_extraction(c: &mut Criterion) {
    let mut group = c.benchmark_group("doc_comment_extraction");

    let source_code = create_code_with_many_doc_comments(100);
    let symbol_count = count_expected_symbols(&source_code);
    group.throughput(Throughput::Elements(symbol_count as u64));

    group.bench_function("doc_comments", |b| {
        let mut parser = KotlinParser::new().expect("Failed to create Kotlin parser");
        b.iter(|| {
            let mut symbol_counter = SymbolCounter::new();
            let file_id = FileId::new(1).expect("Failed to create file ID");
            let symbols = parser.parse(black_box(&source_code), file_id, &mut symbol_counter);
            black_box(symbols)
        });
    });

    group.finish();
}

/// Benchmark method call extraction
fn bench_method_call_extraction(c: &mut Criterion) {
    let mut group = c.benchmark_group("method_call_extraction");

    let source_code = create_code_with_many_calls(100);

    group.bench_function("find_calls", |b| {
        let mut parser = KotlinParser::new().expect("Failed to create Kotlin parser");
        b.iter(|| {
            let calls = parser.find_calls(std::hint::black_box(&source_code));
            std::hint::black_box(calls)
        });
    });

    group.finish();
}

/// Benchmark type usage extraction
fn bench_type_usage_extraction(c: &mut Criterion) {
    let mut group = c.benchmark_group("type_usage_extraction");

    let source_code = create_code_with_type_usage(100);

    group.bench_function("find_uses", |b| {
        let mut parser = KotlinParser::new().expect("Failed to create Kotlin parser");
        b.iter(|| {
            let uses = parser.find_uses(std::hint::black_box(&source_code));
            std::hint::black_box(uses)
        });
    });

    group.finish();
}

/// Benchmark inheritance extraction
fn bench_inheritance_extraction(c: &mut Criterion) {
    let mut group = c.benchmark_group("inheritance_extraction");

    let source_code = create_code_with_inheritance(50);

    group.bench_function("find_extends", |b| {
        let mut parser = KotlinParser::new().expect("Failed to create Kotlin parser");
        b.iter(|| {
            let extends = parser.find_extends(std::hint::black_box(&source_code));
            std::hint::black_box(extends)
        });
    });

    group.finish();
}

// ============================================================================
// Test Data Generation Functions
// ============================================================================

/// Create basic Kotlin code for benchmarking
fn create_basic_kotlin_code() -> String {
    r#"
package com.example

/**
 * A simple person data class.
 */
data class Person(
    val name: String,
    val age: Int
) {
    /**
     * Returns a greeting message.
     */
    fun greet(): String {
        return "Hello, I'm $name"
    }
}

/**
 * Main entry point.
 */
fun main() {
    val person = Person("Alice", 30)
    println(person.greet())
}
"#
    .to_string()
}

/// Create medium complexity Kotlin code
fn create_medium_complexity_kotlin_code() -> String {
    r#"
package com.example.config

import java.time.Duration

/**
 * Configuration class for application settings.
 */
data class Config(
    val host: String,
    val port: Int,
    val timeout: Duration = Duration.ofSeconds(30)
) {
    /**
     * Returns the full address.
     */
    fun getAddress(): String = "$host:$port"

    /**
     * Updates the timeout value.
     */
    fun updateTimeout(newTimeout: Duration): Config {
        return copy(timeout = newTimeout)
    }
}

/**
 * Service interface defining lifecycle methods.
 */
interface Service {
    fun start()
    fun stop()
    fun isHealthy(): Boolean
}

/**
 * Web service implementation.
 */
class WebService(private val config: Config) : Service {
    private var running = false

    override fun start() {
        running = true
        println("Starting web service at ${config.getAddress()}")
    }

    override fun stop() {
        running = false
        println("Stopping web service")
    }

    override fun isHealthy(): Boolean = running
}

/**
 * Companion object with factory methods.
 */
companion object {
    fun createDefault(): WebService {
        return WebService(Config("localhost", 8080))
    }
}
"#
    .to_string()
}

/// Create complex Kotlin code with advanced features
fn create_complex_kotlin_code() -> String {
    r#"
package com.example.advanced

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.util.concurrent.ConcurrentHashMap

/**
 * Generic repository interface.
 */
interface Repository<T> {
    suspend fun store(key: String, value: T): Result<Unit>
    suspend fun get(key: String): Result<T?>
    suspend fun getAll(): Flow<T>
    suspend fun delete(key: String): Result<Unit>
}

/**
 * In-memory repository implementation using coroutines.
 */
class InMemoryRepository<T> : Repository<T> {
    private val storage = ConcurrentHashMap<String, T>()

    override suspend fun store(key: String, value: T): Result<Unit> =
        withContext(Dispatchers.IO) {
            runCatching {
                storage[key] = value
            }
        }

    override suspend fun get(key: String): Result<T?> =
        withContext(Dispatchers.IO) {
            runCatching {
                storage[key]
            }
        }

    override suspend fun getAll(): Flow<T> = flow {
        storage.values.forEach { emit(it) }
    }.flowOn(Dispatchers.IO)

    override suspend fun delete(key: String): Result<Unit> =
        withContext(Dispatchers.IO) {
            runCatching {
                storage.remove(key)
                Unit
            }
        }
}

/**
 * Sealed class for modeling results.
 */
sealed class DataResult<out T> {
    data class Success<T>(val data: T) : DataResult<T>()
    data class Error(val message: String, val cause: Throwable? = null) : DataResult<Nothing>()
    object Loading : DataResult<Nothing>()
}

/**
 * Extension function for mapping results.
 */
inline fun <T, R> DataResult<T>.map(transform: (T) -> R): DataResult<R> = when (this) {
    is DataResult.Success -> DataResult.Success(transform(data))
    is DataResult.Error -> this
    is DataResult.Loading -> this
}

/**
 * Data service with caching and error handling.
 */
class DataService<T>(
    private val repository: Repository<T>,
    private val cache: MutableMap<String, T> = mutableMapOf()
) {
    suspend fun fetchData(key: String): DataResult<T> = coroutineScope {
        try {
            // Check cache first
            cache[key]?.let { return@coroutineScope DataResult.Success(it) }

            // Fetch from repository
            repository.get(key).fold(
                onSuccess = { value ->
                    value?.let {
                        cache[key] = it
                        DataResult.Success(it)
                    } ?: DataResult.Error("Not found")
                },
                onFailure = { error ->
                    DataResult.Error("Fetch failed", error)
                }
            )
        } catch (e: Exception) {
            DataResult.Error("Unexpected error", e)
        }
    }
}

/**
 * Object declaration for singleton pattern.
 */
object Logger {
    private val logs = mutableListOf<String>()

    fun log(message: String) {
        logs.add(message)
        println("[LOG] $message")
    }

    fun getLogs(): List<String> = logs.toList()
}
"#
    .to_string()
}

/// Create real-world Kotlin code pattern
fn create_real_world_kotlin_code() -> String {
    r#"
package com.example.app

import kotlinx.coroutines.*
import java.time.LocalDateTime

/**
 * User domain model.
 */
data class User(
    val id: Long,
    val name: String,
    val email: String,
    val created: LocalDateTime = LocalDateTime.now()
)

/**
 * User service interface.
 */
interface UserService {
    suspend fun createUser(user: User): Result<User>
    suspend fun getUser(id: Long): Result<User?>
    suspend fun listUsers(limit: Int, offset: Int): Result<List<User>>
    suspend fun updateUser(user: User): Result<User>
    suspend fun deleteUser(id: Long): Result<Unit>
}

/**
 * User service implementation with validation.
 */
class UserServiceImpl(
    private val repository: UserRepository
) : UserService {

    override suspend fun createUser(user: User): Result<User> = runCatching {
        require(user.name.isNotBlank()) { "Name cannot be blank" }
        require(user.email.contains("@")) { "Invalid email format" }

        repository.save(user)
    }

    override suspend fun getUser(id: Long): Result<User?> = runCatching {
        require(id > 0) { "Invalid user ID" }
        repository.findById(id)
    }

    override suspend fun listUsers(limit: Int, offset: Int): Result<List<User>> = runCatching {
        require(limit > 0) { "Limit must be positive" }
        require(offset >= 0) { "Offset must be non-negative" }

        repository.findAll(limit, offset)
    }

    override suspend fun updateUser(user: User): Result<User> = runCatching {
        require(user.id > 0) { "Invalid user ID" }
        require(user.name.isNotBlank()) { "Name cannot be blank" }

        repository.update(user)
    }

    override suspend fun deleteUser(id: Long): Result<Unit> = runCatching {
        require(id > 0) { "Invalid user ID" }
        repository.delete(id)
    }
}

/**
 * User repository interface.
 */
interface UserRepository {
    suspend fun save(user: User): User
    suspend fun findById(id: Long): User?
    suspend fun findAll(limit: Int, offset: Int): List<User>
    suspend fun update(user: User): User
    suspend fun delete(id: Long)
}

/**
 * HTTP controller for user endpoints.
 */
class UserController(private val service: UserService) {

    suspend fun handleCreateUser(request: CreateUserRequest): Response<User> {
        val user = User(
            id = 0,
            name = request.name,
            email = request.email
        )

        return service.createUser(user).fold(
            onSuccess = { Response.success(it) },
            onFailure = { Response.error(it.message ?: "Unknown error") }
        )
    }

    suspend fun handleGetUser(id: Long): Response<User?> {
        return service.getUser(id).fold(
            onSuccess = { Response.success(it) },
            onFailure = { Response.error(it.message ?: "Unknown error") }
        )
    }
}

/**
 * Request data class.
 */
data class CreateUserRequest(
    val name: String,
    val email: String
)

/**
 * Response wrapper.
 */
sealed class Response<out T> {
    data class Success<T>(val data: T) : Response<T>()
    data class Error(val message: String) : Response<Nothing>()

    companion object {
        fun <T> success(data: T): Response<T> = Success(data)
        fun error(message: String): Response<Nothing> = Error(message)
    }
}
"#
    .to_string()
}

/// Generate a large Kotlin file with specified number of symbols
fn create_large_kotlin_file(symbol_count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    // Add data classes
    for i in 0..(symbol_count / 4) {
        code.push_str(&format!(
            "data class DataClass{i}(\n    val field1: String,\n    val field2: Int\n)\n\n"
        ));
    }

    // Add interfaces
    for i in 0..(symbol_count / 8) {
        code.push_str(&format!(
            "interface Interface{i} {{\n    fun method{i}(): String\n}}\n\n"
        ));
    }

    // Add functions
    for i in 0..(symbol_count / 2) {
        code.push_str(&format!(
            "fun function{i}(param: String): String {{\n    return \"Function{i}: $param\"\n}}\n\n"
        ));
    }

    // Add main function
    code.push_str("fun main() {\n    println(\"Generated code\")\n}\n");

    code
}

/// Create many functions for benchmarking
fn create_many_functions(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    for i in 0..count {
        code.push_str(&format!(
            "fun function{i}(param{i}: String): String {{\n    return \"Result: $param{i}\"\n}}\n\n"
        ));
    }

    code.push_str("fun main() {}\n");
    code
}

/// Create many classes for benchmarking
fn create_many_classes(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    for i in 0..count {
        code.push_str(&format!(
            "class Class{i} {{\n    val field1: String = \"value\"\n    val field2: Int = {i}\n}}\n\n"
        ));
    }

    code.push_str("fun main() {}\n");
    code
}

/// Create many interfaces for benchmarking
fn create_many_interfaces(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    for i in 0..count {
        code.push_str(&format!(
            "interface Interface{i} {{\n    fun method1{i}(): String\n    fun method2{i}(param: String)\n}}\n\n"
        ));
    }

    code.push_str("fun main() {}\n");
    code
}

/// Create many methods for benchmarking
fn create_many_methods(count: usize) -> String {
    let mut code = String::from("package com.example\n\nclass TestClass {\n");

    for i in 0..count {
        code.push_str(&format!(
            "    fun method{i}(param: String): String {{\n        return \"method{i}: $param\"\n    }}\n\n"
        ));
    }

    code.push_str("}\n\nfun main() {}\n");
    code
}

/// Create many data classes for benchmarking
fn create_many_data_classes(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    for i in 0..count {
        code.push_str(&format!(
            "data class DataClass{i}(\n    val field1: String,\n    val field2: Int,\n    val field3: Boolean\n)\n\n"
        ));
    }

    code.push_str("fun main() {}\n");
    code
}

/// Create many objects for benchmarking
fn create_many_objects(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    for i in 0..count {
        code.push_str(&format!(
            "object Object{i} {{\n    const val CONSTANT = \"value{i}\"\n    fun method(): String = CONSTANT\n}}\n\n"
        ));
    }

    code.push_str("fun main() {}\n");
    code
}

/// Create many properties for benchmarking
fn create_many_properties(count: usize) -> String {
    let mut code = String::from("package com.example\n\nclass PropertyClass {\n");

    for i in 0..count {
        code.push_str(&format!("    val property{i}: String = \"value{i}\"\n"));
    }

    code.push_str("}\n\nfun main() {}\n");
    code
}

/// Create many enums for benchmarking
fn create_many_enums(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    for i in 0..count {
        code.push_str(&format!(
            "enum class Enum{i} {{\n    ENTRY1,\n    ENTRY2,\n    ENTRY3\n}}\n\n"
        ));
    }

    code.push_str("fun main() {}\n");
    code
}

/// Generate scalable Kotlin code for systematic performance testing
fn generate_scalable_kotlin_code(target_symbols: usize) -> String {
    let mut code =
        String::from("package com.example.scalable\n\nimport java.time.LocalDateTime\n\n");

    // Calculate distribution of symbols
    let functions = target_symbols / 4;
    let classes = target_symbols / 8;
    let interfaces = target_symbols / 16;
    let methods = target_symbols / 4;
    let constants = target_symbols / 8;
    let properties = target_symbols / 8;

    // Add constants
    for i in 0..constants {
        code.push_str(&format!("const val CONSTANT{i} = \"value_{i}\"\n"));
    }
    code.push('\n');

    // Add data classes
    for i in 0..classes {
        code.push_str(&format!(
            "data class DataClass{i}(\n    val field1: String,\n    val field2: Int,\n    val field3: Boolean\n)\n\n"
        ));
    }

    // Add interfaces
    for i in 0..interfaces {
        code.push_str(&format!(
            "interface Interface{i} {{\n    fun method1(): String\n    fun method2(param: Int): Boolean\n}}\n\n"
        ));
    }

    // Add regular functions
    for i in 0..functions {
        code.push_str(&format!(
            "fun function{i}(param1: String, param2: Int): String {{\n    return \"func_{i}_${{param1}}_${{param2}}\"\n}}\n\n"
        ));
    }

    // Add class with many methods
    code.push_str("class MethodContainer {\n");
    for i in 0..methods {
        code.push_str(&format!("    fun method{i}(): String = \"method_{i}\"\n"));
    }
    code.push_str("}\n\n");

    // Add class with many properties
    code.push_str("class PropertyContainer {\n");
    for i in 0..properties {
        code.push_str(&format!("    val property{i}: String = \"value_{i}\"\n"));
    }
    code.push_str("}\n\n");

    code.push_str("fun main() {}\n");
    code
}

/// Create code with many doc comments for benchmarking
fn create_code_with_many_doc_comments(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    for i in 0..count {
        code.push_str(&format!(
            "/**\n * Documentation for function{i}.\n * This function does something important.\n * @param param The parameter\n * @return The result\n */\nfun function{i}(param: String): String {{\n    return param\n}}\n\n"
        ));
    }

    code.push_str("fun main() {}\n");
    code
}

/// Create code with many method calls for benchmarking
fn create_code_with_many_calls(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    // Add some functions to call
    for i in 0..10 {
        code.push_str(&format!("fun helper{i}() {{}}\n"));
    }
    code.push('\n');

    // Add a function with many calls
    code.push_str("fun processData() {\n");
    for i in 0..count {
        let helper = i % 10;
        code.push_str(&format!("    helper{helper}()\n"));
    }
    code.push_str("}\n\n");

    code.push_str("fun main() {}\n");
    code
}

/// Create code with type usage for benchmarking
fn create_code_with_type_usage(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    // Add some types
    for i in 0..10 {
        code.push_str(&format!("data class Type{i}(val value: String)\n"));
    }
    code.push('\n');

    // Add functions using those types
    for i in 0..count {
        let type_idx = i % 10;
        code.push_str(&format!(
            "fun process{i}(param: Type{type_idx}): Type{type_idx} = param\n"
        ));
    }

    code.push_str("\nfun main() {}\n");
    code
}

/// Create code with inheritance for benchmarking
fn create_code_with_inheritance(count: usize) -> String {
    let mut code = String::from("package com.example\n\n");

    // Base interface
    code.push_str("interface BaseInterface {\n    fun baseMethod(): String\n}\n\n");

    // Add classes implementing the interface
    for i in 0..count {
        code.push_str(&format!(
            "class Class{i} : BaseInterface {{\n    override fun baseMethod(): String = \"class{i}\"\n}}\n\n"
        ));
    }

    code.push_str("fun main() {}\n");
    code
}

/// Count expected symbols in Kotlin source code (rough estimate)
fn count_expected_symbols(source_code: &str) -> usize {
    let mut count = 0;

    // Count function declarations
    count += source_code.matches("fun ").count();

    // Count class declarations
    count += source_code.matches("class ").count();
    count += source_code.matches("data class ").count();
    count += source_code.matches("object ").count();

    // Count interface declarations
    count += source_code.matches("interface ").count();

    // Count property declarations (val/var)
    count += source_code.matches("val ").count();
    count += source_code.matches("var ").count();

    // Count enum declarations
    count += source_code.matches("enum class ").count();

    // Return at least 1 to avoid division by zero in benchmarks
    count.max(1)
}

criterion_group!(
    benches,
    bench_kotlin_symbol_extraction,
    bench_kotlin_memory_usage,
    bench_parser_initialization,
    bench_kotlin_language_constructs,
    bench_scalable_test_data,
    bench_doc_comment_extraction,
    bench_method_call_extraction,
    bench_type_usage_extraction,
    bench_inheritance_extraction
);
criterion_main!(benches);