oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
//! Benchmark command - Comprehensive benchmarking tools for RDF and SPARQL
//!
//! This module provides comprehensive benchmarking capabilities including:
//! - Running benchmark suites (SP2Bench, WatDiv, LDBC, BSBM)
//! - Generating synthetic benchmark datasets
//! - Analyzing query workload patterns
//! - Comparing benchmark results for regression detection

use super::CommandResult;
use scirs2_core::random::{Random, SeedableRng};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::time::{Duration, Instant};

// Placeholder Store type until oxirs_core is available
struct Store;

impl Store {
    fn open(_path: &std::path::Path) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(Store)
    }
}

/// Run performance benchmarks on a dataset
pub async fn run(
    dataset: String,
    suite: String,
    iterations: usize,
    output: Option<PathBuf>,
    detailed: bool,
    warmup: usize,
) -> CommandResult {
    println!("Running '{suite}' benchmark suite on dataset '{dataset}'");
    println!("Iterations: {iterations}, Warmup: {warmup}, Detailed: {detailed}");

    // Validate benchmark suite
    if !is_supported_benchmark_suite(&suite) {
        return Err(format!(
            "Unsupported benchmark suite '{suite}'. Supported suites: sp2bench, watdiv, ldbc, bsbm, custom"
        )
        .into());
    }

    // Load dataset
    let dataset_path = if PathBuf::from(&dataset).join("oxirs.toml").exists() {
        load_dataset_from_config(&dataset)?
    } else {
        PathBuf::from(&dataset)
    };

    let store = if dataset_path.is_dir() {
        Store::open(&dataset_path)?
    } else {
        return Err(format!(
            "Dataset '{dataset}' not found. Use 'oxirs init' to create a dataset."
        )
        .into());
    };

    println!("Dataset loaded successfully\n");

    // Run warmup iterations
    if warmup > 0 {
        println!("Running {warmup} warmup iterations...");
        run_warmup_iterations(&store, &suite, warmup)?;
        println!("Warmup complete\n");
    }

    // Run benchmark
    let benchmark_results = run_benchmark_suite(&store, &suite, iterations, detailed)?;

    // Display results
    display_benchmark_results(&benchmark_results, detailed);

    // Save results to file if specified
    if let Some(output_path) = output {
        save_benchmark_results(&benchmark_results, &output_path)?;
        println!("\nResults saved to: {}", output_path.display());
    }

    Ok(())
}

/// Generate synthetic benchmark datasets
pub async fn generate(
    output: PathBuf,
    size: String,
    dataset_type: String,
    seed: Option<u64>,
    triples: Option<usize>,
    schema: Option<PathBuf>,
) -> CommandResult {
    println!("Generating synthetic benchmark dataset");
    println!("Output: {}", output.display());
    println!("Size: {size}, Type: {dataset_type}");
    if let Some(s) = seed {
        println!("Random seed: {s}");
    }

    // Determine triple count
    let triple_count = if let Some(count) = triples {
        count
    } else {
        match size.as_str() {
            "tiny" => 1_000,
            "small" => 10_000,
            "medium" => 100_000,
            "large" => 1_000_000,
            "xlarge" => 10_000_000,
            _ => {
                return Err(format!(
                    "Invalid size '{size}'. Valid sizes: tiny, small, medium, large, xlarge"
                )
                .into())
            }
        }
    };

    println!("Generating {} triples...\n", triple_count);

    // Initialize random number generator (unused but kept for future seed-based generation)
    let mut _rng = if let Some(s) = seed {
        Random::seed_from_u64(s)
    } else {
        Random::seed_from_u64(42) // Use consistent RNG type
    };

    // Load schema if provided
    if let Some(schema_path) = schema {
        println!("Using schema: {}", schema_path.display());
        // Schema-based generation would go here
    }

    // Generate dataset based on type
    let dataset = match dataset_type.as_str() {
        "rdf" => generate_rdf_dataset(triple_count, &mut _rng)?,
        "graph" => generate_graph_dataset(triple_count, &mut _rng)?,
        "semantic" => generate_semantic_dataset(triple_count, &mut _rng)?,
        _ => {
            return Err(format!(
                "Invalid dataset type '{dataset_type}'. Valid types: rdf, graph, semantic"
            )
            .into())
        }
    };

    // Create output directory if needed
    if let Some(parent) = output.parent() {
        fs::create_dir_all(parent)?;
    }

    // Save dataset
    fs::write(&output, dataset)?;

    println!("✓ Dataset generated successfully");
    println!("  Total triples: {}", triple_count);
    println!("  Output file: {}", output.display());
    println!(
        "  File size: {:.2} MB",
        fs::metadata(&output)?.len() as f64 / 1_048_576.0
    );

    Ok(())
}

/// Analyze query workload from log files
pub async fn analyze(
    input: PathBuf,
    output: Option<PathBuf>,
    format: String,
    suggestions: bool,
    patterns: bool,
) -> CommandResult {
    println!("Analyzing query workload");
    println!("Input: {}", input.display());
    println!("Format: {format}\n");

    // Read query log
    let log_content = fs::read_to_string(&input)?;
    let queries = parse_query_log(&log_content)?;

    println!("Parsed {} queries from log\n", queries.len());

    // Analyze queries
    let analysis = analyze_query_workload(&queries, patterns)?;

    // Generate report
    let report = generate_workload_report(&analysis, suggestions, format.as_str())?;

    // Display or save report
    if let Some(output_path) = output {
        fs::write(&output_path, &report)?;
        println!("Analysis report saved to: {}", output_path.display());
    } else {
        println!("{}", report);
    }

    Ok(())
}

/// Compare benchmark results for regression detection
pub async fn compare(
    baseline: PathBuf,
    current: PathBuf,
    output: Option<PathBuf>,
    threshold: f64,
    format: String,
) -> CommandResult {
    println!("Comparing benchmark results");
    println!("Baseline: {}", baseline.display());
    println!("Current:  {}", current.display());
    println!("Regression threshold: {:.1}%\n", threshold);

    // Load results
    let baseline_results: BenchmarkResults = load_benchmark_results(&baseline)?;
    let current_results: BenchmarkResults = load_benchmark_results(&current)?;

    // Perform comparison
    let comparison = compare_benchmark_results(&baseline_results, &current_results, threshold)?;

    // Generate report
    let report = generate_comparison_report(&comparison, format.as_str())?;

    // Display or save report
    if let Some(output_path) = output {
        fs::write(&output_path, &report)?;
        println!("Comparison report saved to: {}", output_path.display());
    } else {
        println!("{}", report);
    }

    // Exit with error if regressions detected
    if comparison.has_regressions {
        return Err("Performance regressions detected!".into());
    }

    println!("\n✓ No performance regressions detected");
    Ok(())
}

// ===== Helper Functions =====

/// Check if benchmark suite is supported
fn is_supported_benchmark_suite(suite: &str) -> bool {
    matches!(suite, "sp2bench" | "watdiv" | "ldbc" | "bsbm" | "custom")
}

/// Load dataset configuration
fn load_dataset_from_config(dataset: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let config_path = PathBuf::from(dataset).join("oxirs.toml");

    if !config_path.exists() {
        return Err(format!("Configuration file '{}' not found", config_path.display()).into());
    }

    Ok(PathBuf::from(dataset))
}

/// Benchmark results container
#[derive(Debug, Serialize, Deserialize)]
struct BenchmarkResults {
    suite: String,
    total_queries: usize,
    iterations: usize,
    warmup_iterations: usize,
    total_duration: DurationSerde,
    query_results: Vec<QueryBenchmarkResult>,
    statistics: BenchmarkStatistics,
    timestamp: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct QueryBenchmarkResult {
    query_name: String,
    avg_time: DurationSerde,
    min_time: DurationSerde,
    max_time: DurationSerde,
    median_time: DurationSerde,
    p95_time: DurationSerde,
    p99_time: DurationSerde,
    success_rate: f64,
    stddev: f64,
}

#[derive(Debug, Serialize, Deserialize)]
struct BenchmarkStatistics {
    total_queries_executed: usize,
    avg_query_time: DurationSerde,
    queries_per_second: f64,
    success_rate: f64,
    total_errors: usize,
}

// Serializable Duration wrapper
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
struct DurationSerde {
    secs: u64,
    nanos: u32,
}

impl From<Duration> for DurationSerde {
    fn from(d: Duration) -> Self {
        DurationSerde {
            secs: d.as_secs(),
            nanos: d.subsec_nanos(),
        }
    }
}

impl From<DurationSerde> for Duration {
    fn from(d: DurationSerde) -> Self {
        Duration::new(d.secs, d.nanos)
    }
}

impl DurationSerde {
    fn as_secs_f64(&self) -> f64 {
        self.secs as f64 + self.nanos as f64 / 1_000_000_000.0
    }
}

/// Run warmup iterations
fn run_warmup_iterations(
    _store: &Store,
    suite: &str,
    warmup: usize,
) -> Result<(), Box<dyn std::error::Error>> {
    let queries = get_benchmark_queries(suite)?;

    for (_query_name, _query) in queries.iter().take(3) {
        // Run a few queries for warmup
        for _ in 0..warmup {
            simulate_query_execution();
        }
        print!(".");
        use std::io::Write;
        std::io::stdout().flush().ok();
    }
    println!();

    Ok(())
}

/// Run benchmark suite
fn run_benchmark_suite(
    _store: &Store,
    suite: &str,
    iterations: usize,
    detailed: bool,
) -> Result<BenchmarkResults, Box<dyn std::error::Error>> {
    let queries = get_benchmark_queries(suite)?;
    let mut query_results = Vec::new();
    let mut total_duration = Duration::new(0, 0);
    let mut total_queries_executed = 0;
    let mut successful_queries = 0;
    let mut total_errors = 0;

    for (i, (query_name, _query)) in queries.iter().enumerate() {
        if detailed {
            println!("Running query {}/{}: {}", i + 1, queries.len(), query_name);
        } else {
            print!("\rProgress: {}/{} queries", i + 1, queries.len());
            use std::io::Write;
            std::io::stdout().flush().ok();
        }

        let mut execution_times = Vec::new();
        let mut successes = 0;

        for iteration in 1..=iterations {
            if detailed && (iteration % 10 == 0 || iteration == 1) {
                print!("  Iteration {iteration}/{iterations}\r");
            }

            let start = Instant::now();
            let success = simulate_query_execution();
            let duration = start.elapsed();

            execution_times.push(duration);
            total_duration += duration;
            total_queries_executed += 1;

            if success {
                successes += 1;
                successful_queries += 1;
            } else {
                total_errors += 1;
            }
        }

        if detailed {
            println!("  Completed {iterations} iterations");
        }

        // Calculate statistics
        execution_times.sort();
        let avg_time = Duration::from_nanos(
            (execution_times.iter().map(|d| d.as_nanos()).sum::<u128>() / iterations as u128)
                as u64,
        );
        let min_time = *execution_times
            .first()
            .expect("execution_times should have at least one entry");
        let max_time = *execution_times
            .last()
            .expect("execution_times should have at least one entry");
        let median_time = execution_times[iterations / 2];
        let p95_time = execution_times[(iterations as f64 * 0.95) as usize];
        let p99_time = execution_times[(iterations as f64 * 0.99) as usize];
        let success_rate = successes as f64 / iterations as f64;

        // Calculate standard deviation
        let mean_nanos = avg_time.as_nanos() as f64;
        let variance: f64 = execution_times
            .iter()
            .map(|d| {
                let diff = d.as_nanos() as f64 - mean_nanos;
                diff * diff
            })
            .sum::<f64>()
            / iterations as f64;
        let stddev = variance.sqrt() / 1_000_000.0; // Convert to milliseconds

        query_results.push(QueryBenchmarkResult {
            query_name: query_name.clone(),
            avg_time: avg_time.into(),
            min_time: min_time.into(),
            max_time: max_time.into(),
            median_time: median_time.into(),
            p95_time: p95_time.into(),
            p99_time: p99_time.into(),
            success_rate,
            stddev,
        });
    }

    if !detailed {
        println!(); // New line after progress
    }

    let avg_query_time =
        Duration::from_nanos((total_duration.as_nanos() / total_queries_executed as u128) as u64);
    let queries_per_second = total_queries_executed as f64 / total_duration.as_secs_f64();
    let success_rate = successful_queries as f64 / total_queries_executed as f64;

    let statistics = BenchmarkStatistics {
        total_queries_executed,
        avg_query_time: avg_query_time.into(),
        queries_per_second,
        success_rate,
        total_errors,
    };

    Ok(BenchmarkResults {
        suite: suite.to_string(),
        total_queries: queries.len(),
        iterations,
        warmup_iterations: 0,
        total_duration: total_duration.into(),
        query_results,
        statistics,
        timestamp: chrono::Utc::now().to_rfc3339(),
    })
}

/// Get benchmark queries for a suite
fn get_benchmark_queries(suite: &str) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> {
    match suite {
        "sp2bench" => Ok(vec![
            ("Q1-Simple".to_string(), "SELECT * WHERE { ?s ?p ?o } LIMIT 10".to_string()),
            ("Q2-FOAF".to_string(), "SELECT ?name WHERE { ?person <http://xmlns.com/foaf/0.1/name> ?name }".to_string()),
            ("Q3-Creator".to_string(), "SELECT ?article WHERE { ?article <http://purl.org/dc/elements/1.1/creator> ?author }".to_string()),
            ("Q4-Filter".to_string(), "SELECT ?name WHERE { ?person <http://xmlns.com/foaf/0.1/name> ?name FILTER(REGEX(?name, 'Smith')) }".to_string()),
            ("Q5-Optional".to_string(), "SELECT ?name ?email WHERE { ?person <http://xmlns.com/foaf/0.1/name> ?name OPTIONAL { ?person <http://xmlns.com/foaf/0.1/mbox> ?email } }".to_string()),
        ]),
        "watdiv" => Ok(vec![
            ("C1-Caption".to_string(), "SELECT ?v0 WHERE { ?v0 <http://schema.org/caption> ?v1 }".to_string()),
            ("C2-Follows".to_string(), "SELECT ?v0 ?v1 WHERE { ?v0 <http://schema.org/follows> ?v1 }".to_string()),
            ("F1-Complex".to_string(), "SELECT ?v0 ?v2 WHERE { ?v0 <http://schema.org/likes> ?v1 . ?v1 <http://schema.org/friendOf> ?v2 }".to_string()),
        ]),
        "ldbc" => Ok(vec![
            ("Q1-FirstName".to_string(), "SELECT ?name WHERE { ?person <http://www.ldbc.eu/ldbc_socialnet/1.0/vocabulary/firstName> ?name }".to_string()),
            ("Q2-Friends".to_string(), "SELECT ?p1 ?p2 WHERE { ?p1 <http://www.ldbc.eu/ldbc_socialnet/1.0/vocabulary/knows> ?p2 }".to_string()),
        ]),
        "bsbm" => Ok(vec![
            ("Q1-Product".to_string(), "SELECT ?product ?label WHERE { ?product <http://www.w3.org/2000/01/rdf-schema#label> ?label }".to_string()),
            ("Q2-Features".to_string(), "SELECT ?product ?feature WHERE { ?product <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/productFeature> ?feature }".to_string()),
        ]),
        "custom" => Ok(vec![
            ("simple".to_string(), "SELECT * WHERE { ?s ?p ?o } LIMIT 1".to_string()),
        ]),
        _ => Err(format!("Unknown benchmark suite: {suite}").into()),
    }
}

/// Simulate query execution (placeholder)
fn simulate_query_execution() -> bool {
    use scirs2_core::random::Random;

    // Simulate some work with realistic timing
    let delay = {
        let mut random = Random::default();
        1 + random.random_range(0..15_u64) // 1-15ms
    };
    std::thread::sleep(Duration::from_millis(delay));

    // Simulate 95% success rate
    let mut random = Random::default();
    random.random_f64() < 0.95
}

/// Display benchmark results
fn display_benchmark_results(results: &BenchmarkResults, detailed: bool) {
    println!("\n==================== Benchmark Results ====================");
    println!("Suite: {}", results.suite);
    println!("Timestamp: {}", results.timestamp);
    println!("Total queries: {}", results.total_queries);
    println!("Iterations per query: {}", results.iterations);
    println!(
        "Total duration: {:.2}s",
        results.total_duration.as_secs_f64()
    );
    println!();

    println!("Overall Statistics:");
    println!(
        "  Total queries executed: {}",
        results.statistics.total_queries_executed
    );
    println!(
        "  Average query time: {:.3}ms",
        results.statistics.avg_query_time.as_secs_f64() * 1000.0
    );
    println!(
        "  Queries per second: {:.2}",
        results.statistics.queries_per_second
    );
    println!(
        "  Success rate: {:.1}%",
        results.statistics.success_rate * 100.0
    );
    if results.statistics.total_errors > 0 {
        println!("  Total errors: {}", results.statistics.total_errors);
    }
    println!();

    println!("Query Details:");
    for query_result in &results.query_results {
        println!("  {}:", query_result.query_name);
        println!(
            "    Average: {:.3}ms (±{:.2}ms)",
            query_result.avg_time.as_secs_f64() * 1000.0,
            query_result.stddev
        );

        if detailed {
            println!(
                "    Min: {:.3}ms",
                query_result.min_time.as_secs_f64() * 1000.0
            );
            println!(
                "    Max: {:.3}ms",
                query_result.max_time.as_secs_f64() * 1000.0
            );
            println!(
                "    Median: {:.3}ms",
                query_result.median_time.as_secs_f64() * 1000.0
            );
            println!(
                "    P95: {:.3}ms",
                query_result.p95_time.as_secs_f64() * 1000.0
            );
            println!(
                "    P99: {:.3}ms",
                query_result.p99_time.as_secs_f64() * 1000.0
            );
        }

        println!(
            "    Success rate: {:.1}%",
            query_result.success_rate * 100.0
        );
    }
    println!("==========================================================");
}

/// Save benchmark results to file
fn save_benchmark_results(
    results: &BenchmarkResults,
    output_path: &PathBuf,
) -> Result<(), Box<dyn std::error::Error>> {
    let json_results = serde_json::to_string_pretty(results)?;
    fs::write(output_path, json_results)?;
    Ok(())
}

/// Load benchmark results from file
fn load_benchmark_results(path: &PathBuf) -> Result<BenchmarkResults, Box<dyn std::error::Error>> {
    let content = fs::read_to_string(path)?;
    let results: BenchmarkResults = serde_json::from_str(&content)?;
    Ok(results)
}

// ===== Dataset Generation Functions =====

/// Generate RDF dataset
fn generate_rdf_dataset(
    triple_count: usize,
    _rng: &mut Random<scirs2_core::rngs::StdRng>,
) -> Result<String, Box<dyn std::error::Error>> {
    let mut output = String::new();
    output.push_str("# Generated RDF Dataset\n");
    output.push_str(&format!("# Triples: {}\n\n", triple_count));

    for i in 0..triple_count {
        let subject = format!("<http://example.org/resource/{}>", i);
        let predicate_choice = (i * 7 + 3) % 5; // Deterministic but varied
        let predicate = match predicate_choice {
            0 => "<http://www.w3.org/2000/01/rdf-schema#label>",
            1 => "<http://purl.org/dc/terms/title>",
            2 => "<http://xmlns.com/foaf/0.1/name>",
            3 => "<http://schema.org/name>",
            _ => "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>",
        };
        let object = format!("\"Resource {}\"", i);

        output.push_str(&format!("{} {} {} .\n", subject, predicate, object));
    }

    Ok(output)
}

/// Generate graph dataset
fn generate_graph_dataset(
    triple_count: usize,
    _rng: &mut Random<scirs2_core::rngs::StdRng>,
) -> Result<String, Box<dyn std::error::Error>> {
    let mut output = String::new();
    output.push_str("# Generated Graph Dataset\n\n");

    let node_count = (triple_count as f64).sqrt() as usize;

    for i in 0..triple_count {
        // Deterministic but varied node selection
        let from = (i * 13) % node_count;
        let to = (i * 17 + 7) % node_count;

        let subject = format!("<http://example.org/node/{}>", from);
        let predicate = "<http://example.org/edge>";
        let object = format!("<http://example.org/node/{}>", to);

        output.push_str(&format!("{} {} {} .\n", subject, predicate, object));
    }

    Ok(output)
}

/// Generate semantic dataset
fn generate_semantic_dataset(
    triple_count: usize,
    _rng: &mut Random<scirs2_core::rngs::StdRng>,
) -> Result<String, Box<dyn std::error::Error>> {
    let mut output = String::new();
    output.push_str("# Generated Semantic Dataset\n\n");

    let classes = ["Person", "Organization", "Place", "Event", "Document"];
    let properties = ["name", "description", "createdAt", "author", "location"];

    for i in 0..triple_count {
        // Deterministic but varied selection
        let class = classes[(i * 11) % classes.len()];
        let property = properties[(i * 13) % properties.len()];

        let subject = format!("<http://example.org/{}/{}>", class.to_lowercase(), i);
        let predicate = format!("<http://schema.org/{}>", property);
        let object = format!("\"{}_{}_value\"", class, property);

        output.push_str(&format!("{} {} {} .\n", subject, predicate, object));
    }

    Ok(output)
}

// ===== Workload Analysis Functions =====

#[derive(Debug)]
struct QueryLog {
    query: String,
    _timestamp: String,
    duration_ms: f64,
}

#[derive(Debug)]
struct WorkloadAnalysis {
    total_queries: usize,
    unique_queries: usize,
    _query_frequencies: HashMap<String, usize>,
    avg_duration_ms: f64,
    query_patterns: Vec<QueryPattern>,
}

#[derive(Debug)]
struct QueryPattern {
    pattern_type: String,
    count: usize,
    percentage: f64,
}

/// Parse query log
fn parse_query_log(content: &str) -> Result<Vec<QueryLog>, Box<dyn std::error::Error>> {
    let mut queries = Vec::new();

    for (i, line) in content.lines().enumerate() {
        if line.trim().is_empty() || line.starts_with('#') {
            continue;
        }

        // Simple parsing - in production, parse actual log format
        queries.push(QueryLog {
            query: line.to_string(),
            _timestamp: format!("2025-11-09T{:02}:00:00Z", i % 24),
            duration_ms: (i as f64 % 100.0) + 5.0,
        });
    }

    Ok(queries)
}

/// Analyze query workload
fn analyze_query_workload(
    queries: &[QueryLog],
    analyze_patterns: bool,
) -> Result<WorkloadAnalysis, Box<dyn std::error::Error>> {
    let mut query_frequencies: HashMap<String, usize> = HashMap::new();
    let mut total_duration = 0.0;

    for log in queries {
        *query_frequencies.entry(log.query.clone()).or_insert(0) += 1;
        total_duration += log.duration_ms;
    }

    let unique_queries = query_frequencies.len();
    let avg_duration_ms = total_duration / queries.len() as f64;

    let mut query_patterns = Vec::new();
    if analyze_patterns {
        query_patterns = detect_query_patterns(queries)?;
    }

    Ok(WorkloadAnalysis {
        total_queries: queries.len(),
        unique_queries,
        _query_frequencies: query_frequencies,
        avg_duration_ms,
        query_patterns,
    })
}

/// Detect query patterns
fn detect_query_patterns(
    queries: &[QueryLog],
) -> Result<Vec<QueryPattern>, Box<dyn std::error::Error>> {
    let mut patterns: HashMap<String, usize> = HashMap::new();

    for log in queries {
        let pattern_type = if log.query.contains("SELECT") {
            "SELECT"
        } else if log.query.contains("ASK") {
            "ASK"
        } else if log.query.contains("CONSTRUCT") {
            "CONSTRUCT"
        } else if log.query.contains("DESCRIBE") {
            "DESCRIBE"
        } else {
            "OTHER"
        };

        *patterns.entry(pattern_type.to_string()).or_insert(0) += 1;
    }

    let total = queries.len() as f64;
    Ok(patterns
        .into_iter()
        .map(|(pattern_type, count)| QueryPattern {
            pattern_type,
            count,
            percentage: (count as f64 / total) * 100.0,
        })
        .collect())
}

/// Generate workload report
fn generate_workload_report(
    analysis: &WorkloadAnalysis,
    include_suggestions: bool,
    format: &str,
) -> Result<String, Box<dyn std::error::Error>> {
    match format {
        "json" => Ok(serde_json::to_string_pretty(&serde_json::json!({
            "total_queries": analysis.total_queries,
            "unique_queries": analysis.unique_queries,
            "avg_duration_ms": analysis.avg_duration_ms,
            "patterns": analysis.query_patterns.iter().map(|p| {
                serde_json::json!({
                    "type": p.pattern_type,
                    "count": p.count,
                    "percentage": p.percentage
                })
            }).collect::<Vec<_>>()
        }))?),
        "html" => Ok(format!(
            r#"<html><body>
<h1>Query Workload Analysis</h1>
<p>Total Queries: {}</p>
<p>Unique Queries: {}</p>
<p>Average Duration: {:.2}ms</p>
</body></html>"#,
            analysis.total_queries, analysis.unique_queries, analysis.avg_duration_ms
        )),
        _ => {
            // Text format
            let mut report = String::new();
            report.push_str("===== Query Workload Analysis =====\n\n");
            report.push_str(&format!("Total queries: {}\n", analysis.total_queries));
            report.push_str(&format!("Unique queries: {}\n", analysis.unique_queries));
            report.push_str(&format!(
                "Average duration: {:.2}ms\n\n",
                analysis.avg_duration_ms
            ));

            if !analysis.query_patterns.is_empty() {
                report.push_str("Query Patterns:\n");
                for pattern in &analysis.query_patterns {
                    report.push_str(&format!(
                        "  {}: {} ({:.1}%)\n",
                        pattern.pattern_type, pattern.count, pattern.percentage
                    ));
                }
                report.push('\n');
            }

            if include_suggestions {
                report.push_str("Optimization Suggestions:\n");
                report.push_str("  • Consider caching frequently executed queries\n");
                report.push_str("  • Add indexes for commonly queried patterns\n");
                report
                    .push_str("  • Review slow queries (>100ms) for optimization opportunities\n");
            }

            Ok(report)
        }
    }
}

// ===== Comparison Functions =====

#[derive(Debug, Serialize)]
struct BenchmarkComparison {
    baseline_suite: String,
    current_suite: String,
    baseline_timestamp: String,
    current_timestamp: String,
    query_comparisons: Vec<QueryComparison>,
    overall_change_percent: f64,
    has_regressions: bool,
    regressions: Vec<String>,
    improvements: Vec<String>,
}

#[derive(Debug, Serialize)]
struct QueryComparison {
    query_name: String,
    baseline_avg_ms: f64,
    current_avg_ms: f64,
    change_percent: f64,
    is_regression: bool,
}

/// Compare benchmark results
fn compare_benchmark_results(
    baseline: &BenchmarkResults,
    current: &BenchmarkResults,
    threshold: f64,
) -> Result<BenchmarkComparison, Box<dyn std::error::Error>> {
    let mut query_comparisons = Vec::new();
    let mut regressions = Vec::new();
    let mut improvements = Vec::new();
    let mut total_baseline_time = 0.0;
    let mut total_current_time = 0.0;

    // Compare queries
    for baseline_query in &baseline.query_results {
        if let Some(current_query) = current
            .query_results
            .iter()
            .find(|q| q.query_name == baseline_query.query_name)
        {
            let baseline_ms = baseline_query.avg_time.as_secs_f64() * 1000.0;
            let current_ms = current_query.avg_time.as_secs_f64() * 1000.0;
            let change_percent = ((current_ms - baseline_ms) / baseline_ms) * 100.0;

            total_baseline_time += baseline_ms;
            total_current_time += current_ms;

            let is_regression = change_percent > threshold;

            if is_regression {
                regressions.push(format!(
                    "{}: {:.1}% slower ({:.2}ms → {:.2}ms)",
                    baseline_query.query_name, change_percent, baseline_ms, current_ms
                ));
            } else if change_percent < -5.0 {
                // Improvement threshold
                improvements.push(format!(
                    "{}: {:.1}% faster ({:.2}ms → {:.2}ms)",
                    baseline_query.query_name,
                    change_percent.abs(),
                    baseline_ms,
                    current_ms
                ));
            }

            query_comparisons.push(QueryComparison {
                query_name: baseline_query.query_name.clone(),
                baseline_avg_ms: baseline_ms,
                current_avg_ms: current_ms,
                change_percent,
                is_regression,
            });
        }
    }

    let overall_change_percent =
        ((total_current_time - total_baseline_time) / total_baseline_time) * 100.0;
    let has_regressions = !regressions.is_empty();

    Ok(BenchmarkComparison {
        baseline_suite: baseline.suite.clone(),
        current_suite: current.suite.clone(),
        baseline_timestamp: baseline.timestamp.clone(),
        current_timestamp: current.timestamp.clone(),
        query_comparisons,
        overall_change_percent,
        has_regressions,
        regressions,
        improvements,
    })
}

/// Generate comparison report
fn generate_comparison_report(
    comparison: &BenchmarkComparison,
    format: &str,
) -> Result<String, Box<dyn std::error::Error>> {
    match format {
        "json" => Ok(serde_json::to_string_pretty(comparison)?),
        "html" => {
            let mut html = String::new();
            html.push_str("<html><body>\n");
            html.push_str("<h1>Benchmark Comparison Report</h1>\n");
            html.push_str(&format!(
                "<p>Overall change: {:.1}%</p>\n",
                comparison.overall_change_percent
            ));
            html.push_str("</body></html>");
            Ok(html)
        }
        _ => {
            // Text format
            let mut report = String::new();
            report.push_str("===== Benchmark Comparison Report =====\n\n");
            report.push_str(&format!(
                "Baseline: {} ({})\n",
                comparison.baseline_suite, comparison.baseline_timestamp
            ));
            report.push_str(&format!(
                "Current:  {} ({})\n\n",
                comparison.current_suite, comparison.current_timestamp
            ));

            report.push_str(&format!(
                "Overall Performance Change: {:.1}%\n\n",
                comparison.overall_change_percent
            ));

            if !comparison.regressions.is_empty() {
                report.push_str("⚠️  REGRESSIONS DETECTED:\n");
                for regression in &comparison.regressions {
                    report.push_str(&format!("{}\n", regression));
                }
                report.push('\n');
            }

            if !comparison.improvements.is_empty() {
                report.push_str("✓ Improvements:\n");
                for improvement in &comparison.improvements {
                    report.push_str(&format!("{}\n", improvement));
                }
                report.push('\n');
            }

            report.push_str("Query-by-Query Comparison:\n");
            for comp in &comparison.query_comparisons {
                let status = if comp.is_regression {
                    "⚠️ REGRESSION"
                } else if comp.change_percent < -5.0 {
                    "✓ IMPROVED"
                } else {
                    "≈ UNCHANGED"
                };

                report.push_str(&format!("  {} {}:\n", status, comp.query_name));
                report.push_str(&format!(
                    "    Baseline: {:.2}ms → Current: {:.2}ms ({:+.1}%)\n",
                    comp.baseline_avg_ms, comp.current_avg_ms, comp.change_percent
                ));
            }

            Ok(report)
        }
    }
}

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

    #[test]
    fn test_benchmark_suite_validation() {
        assert!(is_supported_benchmark_suite("sp2bench"));
        assert!(is_supported_benchmark_suite("watdiv"));
        assert!(is_supported_benchmark_suite("ldbc"));
        assert!(is_supported_benchmark_suite("bsbm"));
        assert!(!is_supported_benchmark_suite("invalid"));
    }

    #[test]
    fn test_dataset_size_parsing() {
        assert_eq!(get_triple_count_for_size("tiny"), Some(1_000));
        assert_eq!(get_triple_count_for_size("small"), Some(10_000));
        assert_eq!(get_triple_count_for_size("medium"), Some(100_000));
        assert_eq!(get_triple_count_for_size("large"), Some(1_000_000));
        assert_eq!(get_triple_count_for_size("xlarge"), Some(10_000_000));
        assert_eq!(get_triple_count_for_size("invalid"), None);
    }

    fn get_triple_count_for_size(size: &str) -> Option<usize> {
        match size {
            "tiny" => Some(1_000),
            "small" => Some(10_000),
            "medium" => Some(100_000),
            "large" => Some(1_000_000),
            "xlarge" => Some(10_000_000),
            _ => None,
        }
    }

    #[test]
    fn test_duration_serde() {
        let duration = Duration::from_millis(1234);
        let serde: DurationSerde = duration.into();
        let back: Duration = serde.into();
        assert_eq!(duration, back);
    }
}