oxirs-rule 0.2.4

Forward/backward rule engine for RDFS, OWL, and SWRL reasoning
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
//! Comprehensive Benchmark Suite for Reasoning Engines
//!
//! Provides standardized benchmarks for measuring and comparing performance of
//! different reasoning strategies, algorithms, and optimizations.
//!
//! # Features
//!
//! - **Standard Benchmark Datasets**: Common reasoning tasks for comparison
//! - **Performance Metrics**: Throughput, latency, memory usage, scalability
//! - **Automated Testing**: Run full benchmark suites with one command
//! - **Comparative Analysis**: Compare different engines and configurations
//! - **Regression Detection**: Identify performance regressions
//! - **Reporting**: Generate detailed benchmark reports
//!
//! # Benchmark Categories
//!
//! - **Forward Chaining**: Data-driven inference performance
//! - **Backward Chaining**: Goal-driven query performance
//! - **RETE Network**: Pattern matching efficiency
//! - **Incremental Reasoning**: Delta computation performance
//! - **Parallel Execution**: Multi-threaded scaling
//! - **SPARQL Integration**: Query-driven reasoning
//! - **SHACL Validation**: Constraint checking performance
//!
//! # Example
//!
//! ```text
//! use oxirs_rule::benchmark_suite::{BenchmarkSuite, BenchmarkConfig, BenchmarkCategory};
//!
//! let config = BenchmarkConfig::default()
//!     .with_iterations(100)
//!     .with_warmup(10);
//!
//! let mut suite = BenchmarkSuite::new(config);
//!
//! // Run all benchmarks
//! let results = suite.run_all().expect("should succeed");
//!
//! // Print report
//! println!("{}", results.generate_report());
//!
//! // Run specific category
//! let forward_results = suite.run_category(BenchmarkCategory::ForwardChaining).expect("should succeed");
//! # Ok::<(), anyhow::Error>(())
//! ```

use crate::{Rule, RuleAtom, RuleEngine, Term};
use anyhow::Result;
use scirs2_core::metrics::{Counter, Timer};
use std::collections::HashMap;
use std::time::{Duration, Instant};
use tracing::{debug, info};

// Global metrics for benchmarking
lazy_static::lazy_static! {
    static ref BENCHMARK_RUNS: Counter = Counter::new("benchmark_runs".to_string());
    static ref BENCHMARK_FAILURES: Counter = Counter::new("benchmark_failures".to_string());
    static ref BENCHMARK_TIME: Timer = Timer::new("benchmark_total_time".to_string());
}

/// Benchmark category
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BenchmarkCategory {
    /// Forward chaining inference
    ForwardChaining,
    /// Backward chaining proof search
    BackwardChaining,
    /// RETE pattern matching
    ReteMatching,
    /// Incremental reasoning
    IncrementalReasoning,
    /// Parallel execution
    ParallelExecution,
    /// SPARQL integration
    SparqlIntegration,
    /// SHACL validation
    ShaclValidation,
    /// Rule optimization
    RuleOptimization,
    /// Memory usage
    MemoryUsage,
    /// Scalability testing
    Scalability,
}

impl BenchmarkCategory {
    /// Get all benchmark categories
    pub fn all() -> Vec<Self> {
        vec![
            Self::ForwardChaining,
            Self::BackwardChaining,
            Self::ReteMatching,
            Self::IncrementalReasoning,
            Self::ParallelExecution,
            Self::SparqlIntegration,
            Self::ShaclValidation,
            Self::RuleOptimization,
            Self::MemoryUsage,
            Self::Scalability,
        ]
    }

    /// Get category name
    pub fn name(&self) -> &str {
        match self {
            Self::ForwardChaining => "Forward Chaining",
            Self::BackwardChaining => "Backward Chaining",
            Self::ReteMatching => "RETE Matching",
            Self::IncrementalReasoning => "Incremental Reasoning",
            Self::ParallelExecution => "Parallel Execution",
            Self::SparqlIntegration => "SPARQL Integration",
            Self::ShaclValidation => "SHACL Validation",
            Self::RuleOptimization => "Rule Optimization",
            Self::MemoryUsage => "Memory Usage",
            Self::Scalability => "Scalability",
        }
    }
}

/// Benchmark configuration
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Number of iterations per benchmark
    pub iterations: usize,
    /// Warmup iterations (not included in results)
    pub warmup: usize,
    /// Enable detailed profiling
    pub detailed_profiling: bool,
    /// Maximum duration per benchmark (ms)
    pub max_duration_ms: u64,
    /// Minimum samples for statistical significance
    pub min_samples: usize,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            iterations: 100,
            warmup: 10,
            detailed_profiling: false,
            max_duration_ms: 60_000, // 1 minute
            min_samples: 10,
        }
    }
}

impl BenchmarkConfig {
    /// Set iterations
    pub fn with_iterations(mut self, iterations: usize) -> Self {
        self.iterations = iterations;
        self
    }

    /// Set warmup
    pub fn with_warmup(mut self, warmup: usize) -> Self {
        self.warmup = warmup;
        self
    }

    /// Enable detailed profiling
    pub fn with_detailed_profiling(mut self, enabled: bool) -> Self {
        self.detailed_profiling = enabled;
        self
    }
}

/// Benchmark result for a single test
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    /// Name of the benchmark
    pub name: String,
    /// Category
    pub category: BenchmarkCategory,
    /// Total time taken
    pub total_time: Duration,
    /// Average time per iteration
    pub avg_time: Duration,
    /// Minimum time
    pub min_time: Duration,
    /// Maximum time
    pub max_time: Duration,
    /// Standard deviation
    pub std_dev: Duration,
    /// Throughput (operations per second)
    pub throughput: f64,
    /// Number of samples
    pub samples: usize,
    /// Memory usage (bytes)
    pub memory_bytes: usize,
}

impl BenchmarkResult {
    /// Generate a summary string
    pub fn summary(&self) -> String {
        format!(
            "{}: avg={:.2}ms, throughput={:.0} ops/sec, mem={}KB",
            self.name,
            self.avg_time.as_secs_f64() * 1000.0,
            self.throughput,
            self.memory_bytes / 1024
        )
    }
}

/// Collection of benchmark results
#[derive(Debug, Clone)]
pub struct BenchmarkResults {
    /// All results
    pub results: Vec<BenchmarkResult>,
    /// Timestamp
    pub timestamp: std::time::SystemTime,
}

impl BenchmarkResults {
    /// Generate a detailed report
    pub fn generate_report(&self) -> String {
        let mut report = String::new();
        report.push_str("═══════════════════════════════════════════════════════════════\n");
        report.push_str("          OxiRS Rule Engine Benchmark Report\n");
        report.push_str("═══════════════════════════════════════════════════════════════\n\n");

        // Group by category
        let mut by_category: HashMap<BenchmarkCategory, Vec<&BenchmarkResult>> = HashMap::new();
        for result in &self.results {
            by_category.entry(result.category).or_default().push(result);
        }

        // Print results by category
        for category in BenchmarkCategory::all() {
            if let Some(results) = by_category.get(&category) {
                report.push_str(&format!("\n{}\n", category.name()));
                report.push_str(&format!("{}\n", "".repeat(60)));

                for result in results {
                    report.push_str(&format!(
                        "  {:<30} {:>10.2}ms  {:>12.0} ops/s\n",
                        result.name,
                        result.avg_time.as_secs_f64() * 1000.0,
                        result.throughput
                    ));
                }
            }
        }

        report.push_str("\n═══════════════════════════════════════════════════════════════\n");
        report
    }

    /// Get fastest benchmark in category
    pub fn fastest_in_category(&self, category: BenchmarkCategory) -> Option<&BenchmarkResult> {
        self.results
            .iter()
            .filter(|r| r.category == category)
            .min_by_key(|r| r.avg_time)
    }

    /// Get slowest benchmark in category
    pub fn slowest_in_category(&self, category: BenchmarkCategory) -> Option<&BenchmarkResult> {
        self.results
            .iter()
            .filter(|r| r.category == category)
            .max_by_key(|r| r.avg_time)
    }

    /// Calculate overall statistics
    pub fn overall_stats(&self) -> (Duration, f64) {
        let total_time: Duration = self.results.iter().map(|r| r.total_time).sum();
        let avg_throughput: f64 =
            self.results.iter().map(|r| r.throughput).sum::<f64>() / self.results.len() as f64;
        (total_time, avg_throughput)
    }
}

/// Comprehensive benchmark suite
pub struct BenchmarkSuite {
    /// Configuration
    config: BenchmarkConfig,
    /// Rule engine
    engine: RuleEngine,
    /// Test datasets
    datasets: HashMap<String, Vec<RuleAtom>>,
}

impl BenchmarkSuite {
    /// Create a new benchmark suite
    pub fn new(config: BenchmarkConfig) -> Self {
        let mut suite = Self {
            config,
            engine: RuleEngine::new(),
            datasets: HashMap::new(),
        };
        suite.load_datasets();
        suite
    }

    /// Load standard benchmark datasets
    fn load_datasets(&mut self) {
        // Small dataset (10 facts)
        self.datasets.insert(
            "small".to_string(),
            (0..10)
                .map(|i| RuleAtom::Triple {
                    subject: Term::Constant(format!("s{i}")),
                    predicate: Term::Constant("p".to_string()),
                    object: Term::Constant(format!("o{i}")),
                })
                .collect(),
        );

        // Medium dataset (100 facts)
        self.datasets.insert(
            "medium".to_string(),
            (0..100)
                .map(|i| RuleAtom::Triple {
                    subject: Term::Constant(format!("s{i}")),
                    predicate: Term::Constant("p".to_string()),
                    object: Term::Constant(format!("o{i}")),
                })
                .collect(),
        );

        // Large dataset (1000 facts)
        self.datasets.insert(
            "large".to_string(),
            (0..1000)
                .map(|i| RuleAtom::Triple {
                    subject: Term::Constant(format!("s{i}")),
                    predicate: Term::Constant("p".to_string()),
                    object: Term::Constant(format!("o{i}")),
                })
                .collect(),
        );
    }

    /// Run all benchmarks
    pub fn run_all(&mut self) -> Result<BenchmarkResults> {
        info!("Running all benchmarks");
        let _timer = BENCHMARK_TIME.start();

        let mut all_results = Vec::new();

        for category in BenchmarkCategory::all() {
            match self.run_category(category) {
                Ok(mut results) => all_results.append(&mut results.results),
                Err(e) => {
                    BENCHMARK_FAILURES.inc();
                    debug!("Category {:?} failed: {}", category, e);
                }
            }
        }

        BENCHMARK_RUNS.inc();

        Ok(BenchmarkResults {
            results: all_results,
            timestamp: std::time::SystemTime::now(),
        })
    }

    /// Run benchmarks for specific category
    pub fn run_category(&mut self, category: BenchmarkCategory) -> Result<BenchmarkResults> {
        info!("Running benchmarks for {:?}", category);

        let results = match category {
            BenchmarkCategory::ForwardChaining => self.bench_forward_chaining(),
            BenchmarkCategory::BackwardChaining => self.bench_backward_chaining(),
            BenchmarkCategory::ReteMatching => self.bench_rete_matching(),
            BenchmarkCategory::IncrementalReasoning => self.bench_incremental_reasoning(),
            BenchmarkCategory::ParallelExecution => self.bench_parallel_execution(),
            BenchmarkCategory::SparqlIntegration => self.bench_sparql_integration(),
            BenchmarkCategory::ShaclValidation => self.bench_shacl_validation(),
            BenchmarkCategory::RuleOptimization => self.bench_rule_optimization(),
            BenchmarkCategory::MemoryUsage => self.bench_memory_usage(),
            BenchmarkCategory::Scalability => self.bench_scalability(),
        }?;

        Ok(BenchmarkResults {
            results,
            timestamp: std::time::SystemTime::now(),
        })
    }

    /// Benchmark forward chaining
    fn bench_forward_chaining(&mut self) -> Result<Vec<BenchmarkResult>> {
        let mut results = Vec::new();

        // Simple rule benchmark
        let rule = Rule {
            name: "simple".to_string(),
            body: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("q".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
        };
        self.engine.add_rule(rule);

        // Clone datasets to avoid borrow checker issues
        let datasets: Vec<(String, Vec<RuleAtom>)> = self
            .datasets
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();

        for (name, dataset) in datasets {
            let result = self.run_benchmark_with_engine(
                &format!("forward_chain_{}", name),
                BenchmarkCategory::ForwardChaining,
                |engine| {
                    engine.clear();
                    engine.forward_chain(&dataset)
                },
            )?;
            results.push(result);
        }

        Ok(results)
    }

    /// Benchmark backward chaining
    ///
    /// Note: Uses very conservative settings (max_depth=5, minimal dataset) to prevent stack overflow
    fn bench_backward_chaining(&mut self) -> Result<Vec<BenchmarkResult>> {
        let mut results = Vec::new();

        // Configure backward chainer with very conservative max_depth to prevent stack overflow
        self.engine.set_backward_chain_max_depth(5);

        // Add test rule (simple non-recursive rule)
        let rule = Rule {
            name: "test".to_string(),
            body: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("q".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
        };
        self.engine.add_rule(rule);

        // Use minimal dataset (just 3 facts) to prevent exponential proof search
        let minimal_dataset = vec![
            RuleAtom::Triple {
                subject: Term::Constant("s0".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Constant("o0".to_string()),
            },
            RuleAtom::Triple {
                subject: Term::Constant("s1".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Constant("o1".to_string()),
            },
            RuleAtom::Triple {
                subject: Term::Constant("s2".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Constant("o2".to_string()),
            },
        ];

        // Goal with fully bound subject to minimize search space
        let goal = RuleAtom::Triple {
            subject: Term::Constant("s0".to_string()),
            predicate: Term::Constant("q".to_string()),
            object: Term::Constant("o0".to_string()), // Fully bound to avoid exponential search
        };

        let goal_clone = goal.clone();
        let dataset_clone = minimal_dataset.clone();
        let result = self.run_benchmark_with_engine(
            "backward_chain_minimal",
            BenchmarkCategory::BackwardChaining,
            |engine| {
                engine.clear();
                // Re-apply the safe max_depth after clear
                engine.set_backward_chain_max_depth(5);
                engine.add_facts(dataset_clone.clone());
                engine.backward_chain(&goal_clone)
            },
        )?;
        results.push(result);

        Ok(results)
    }

    /// Benchmark RETE matching
    fn bench_rete_matching(&mut self) -> Result<Vec<BenchmarkResult>> {
        let mut results = Vec::new();

        // Clone datasets to avoid borrow checker issues
        let datasets: Vec<(String, Vec<RuleAtom>)> = self
            .datasets
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();

        for (name, dataset) in datasets {
            let result = self.run_benchmark_with_engine(
                &format!("rete_{}", name),
                BenchmarkCategory::ReteMatching,
                |engine| {
                    engine.clear();
                    engine.rete_forward_chain(dataset.clone())
                },
            )?;
            results.push(result);
        }

        Ok(results)
    }

    /// Placeholder benchmarks for other categories
    fn bench_incremental_reasoning(&mut self) -> Result<Vec<BenchmarkResult>> {
        Ok(vec![])
    }

    fn bench_parallel_execution(&mut self) -> Result<Vec<BenchmarkResult>> {
        Ok(vec![])
    }

    fn bench_sparql_integration(&mut self) -> Result<Vec<BenchmarkResult>> {
        Ok(vec![])
    }

    fn bench_shacl_validation(&mut self) -> Result<Vec<BenchmarkResult>> {
        Ok(vec![])
    }

    fn bench_rule_optimization(&mut self) -> Result<Vec<BenchmarkResult>> {
        Ok(vec![])
    }

    fn bench_memory_usage(&mut self) -> Result<Vec<BenchmarkResult>> {
        Ok(vec![])
    }

    fn bench_scalability(&mut self) -> Result<Vec<BenchmarkResult>> {
        Ok(vec![])
    }

    /// Estimate memory usage of the engine
    ///
    /// This provides an approximation of memory consumption based on:
    /// - Number of facts and rules
    /// - Average sizes of terms and atoms
    /// - Internal data structure overhead
    fn estimate_memory_usage(&self) -> usize {
        let facts = self.engine.get_facts();

        // Estimate bytes per fact (conservative)
        // - RuleAtom enum discriminant: 1 byte
        // - Triple with 3 Terms: ~3 * 50 bytes (strings, etc.) = 150 bytes
        // - HashSet overhead: ~8 bytes per entry
        let bytes_per_fact = 160;

        // Estimate bytes per rule
        // - Rule struct: name (String ~24 bytes) + body Vec + head Vec
        // - Average 3 atoms in body + 2 in head = 5 * 160 bytes
        // - Vec overhead: ~24 bytes each
        let bytes_per_rule = 24 + (5 * 160) + (2 * 24);

        let fact_count = facts.len();
        let rule_count = self.engine.get_facts().len(); // Approximation

        // Calculate total memory
        let facts_memory = fact_count * bytes_per_fact;
        let rules_memory = rule_count * bytes_per_rule;

        // Add engine overhead (RETE network, caches, etc.)
        let engine_overhead = 4096; // ~4KB base overhead

        facts_memory + rules_memory + engine_overhead
    }

    /// Run a single benchmark with timing using engine
    fn run_benchmark_with_engine<F, T>(
        &mut self,
        name: &str,
        category: BenchmarkCategory,
        mut f: F,
    ) -> Result<BenchmarkResult>
    where
        F: FnMut(&mut RuleEngine) -> Result<T>,
    {
        debug!("Running benchmark: {}", name);

        // Warmup
        for _ in 0..self.config.warmup {
            let _ = f(&mut self.engine);
        }

        // Actual benchmark
        let mut durations = Vec::new();
        let benchmark_start = Instant::now();

        for _ in 0..self.config.iterations {
            let start = Instant::now();
            let _ = f(&mut self.engine)?;
            let duration = start.elapsed();
            durations.push(duration);

            if benchmark_start.elapsed().as_millis() > self.config.max_duration_ms as u128 {
                debug!("Benchmark {} hit time limit", name);
                break;
            }
        }

        // Calculate statistics
        let total_time: Duration = durations.iter().sum();
        let avg_time = total_time / durations.len() as u32;
        let min_time = *durations
            .iter()
            .min()
            .expect("durations should not be empty");
        let max_time = *durations
            .iter()
            .max()
            .expect("durations should not be empty");

        // Calculate standard deviation
        let mean = avg_time.as_nanos() as f64;
        let variance: f64 = durations
            .iter()
            .map(|d| {
                let diff = d.as_nanos() as f64 - mean;
                diff * diff
            })
            .sum::<f64>()
            / durations.len() as f64;
        let std_dev = Duration::from_nanos(variance.sqrt() as u64);

        // Calculate throughput
        let throughput = if avg_time.as_secs_f64() > 0.0 {
            1.0 / avg_time.as_secs_f64()
        } else {
            f64::INFINITY
        };

        // Estimate memory usage after benchmark execution
        let memory_bytes = self.estimate_memory_usage();

        Ok(BenchmarkResult {
            name: name.to_string(),
            category,
            total_time,
            avg_time,
            min_time,
            max_time,
            std_dev,
            throughput,
            samples: durations.len(),
            memory_bytes,
        })
    }
}

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

    #[test]
    fn test_benchmark_config_default() {
        let config = BenchmarkConfig::default();
        assert_eq!(config.iterations, 100);
        assert_eq!(config.warmup, 10);
        assert!(!config.detailed_profiling);
    }

    #[test]
    fn test_benchmark_config_builder() {
        let config = BenchmarkConfig::default()
            .with_iterations(50)
            .with_warmup(5)
            .with_detailed_profiling(true);

        assert_eq!(config.iterations, 50);
        assert_eq!(config.warmup, 5);
        assert!(config.detailed_profiling);
    }

    #[test]
    fn test_benchmark_categories() {
        let categories = BenchmarkCategory::all();
        assert_eq!(categories.len(), 10);
        assert!(categories.contains(&BenchmarkCategory::ForwardChaining));
    }

    #[test]
    fn test_benchmark_category_names() {
        assert_eq!(
            BenchmarkCategory::ForwardChaining.name(),
            "Forward Chaining"
        );
        assert_eq!(
            BenchmarkCategory::BackwardChaining.name(),
            "Backward Chaining"
        );
    }

    #[test]
    fn test_benchmark_suite_creation() {
        let config = BenchmarkConfig::default().with_iterations(10);
        let suite = BenchmarkSuite::new(config);

        assert!(suite.datasets.contains_key("small"));
        assert!(suite.datasets.contains_key("medium"));
        assert!(suite.datasets.contains_key("large"));
    }

    #[test]
    fn test_benchmark_forward_chaining() -> Result<(), Box<dyn std::error::Error>> {
        let config = BenchmarkConfig::default().with_iterations(5).with_warmup(1);
        let mut suite = BenchmarkSuite::new(config);

        let results = suite.run_category(BenchmarkCategory::ForwardChaining)?;
        assert!(!results.results.is_empty());

        for result in &results.results {
            assert_eq!(result.category, BenchmarkCategory::ForwardChaining);
            assert!(result.throughput > 0.0);
            assert!(result.samples > 0);
        }
        Ok(())
    }

    #[test]
    fn test_benchmark_backward_chaining() -> Result<(), Box<dyn std::error::Error>> {
        let config = BenchmarkConfig::default().with_iterations(1).with_warmup(0);
        let mut suite = BenchmarkSuite::new(config);

        let results = suite.run_category(BenchmarkCategory::BackwardChaining)?;
        assert!(!results.results.is_empty());
        // Should only have one result (small dataset)
        assert_eq!(results.results.len(), 1);
        Ok(())
    }

    #[test]
    fn test_benchmark_rete_matching() -> Result<(), Box<dyn std::error::Error>> {
        let config = BenchmarkConfig::default().with_iterations(5).with_warmup(1);
        let mut suite = BenchmarkSuite::new(config);

        let results = suite.run_category(BenchmarkCategory::ReteMatching)?;
        assert!(!results.results.is_empty());
        Ok(())
    }

    #[test]
    fn test_benchmark_results_report() -> Result<(), Box<dyn std::error::Error>> {
        let config = BenchmarkConfig::default().with_iterations(5);
        let mut suite = BenchmarkSuite::new(config);

        let results = suite.run_category(BenchmarkCategory::ForwardChaining)?;
        let report = results.generate_report();

        assert!(report.contains("Benchmark Report"));
        assert!(report.contains("Forward Chaining"));
        Ok(())
    }

    #[test]
    fn test_benchmark_result_summary() {
        let result = BenchmarkResult {
            name: "test".to_string(),
            category: BenchmarkCategory::ForwardChaining,
            total_time: Duration::from_millis(100),
            avg_time: Duration::from_micros(1000),
            min_time: Duration::from_micros(800),
            max_time: Duration::from_micros(1200),
            std_dev: Duration::from_micros(100),
            throughput: 1000.0,
            samples: 100,
            memory_bytes: 1024,
        };

        let summary = result.summary();
        assert!(summary.contains("test"));
        assert!(summary.contains("1000 ops/sec"));
    }

    #[test]
    fn test_benchmark_fastest_slowest() -> Result<(), Box<dyn std::error::Error>> {
        let results = BenchmarkResults {
            results: vec![
                BenchmarkResult {
                    name: "fast".to_string(),
                    category: BenchmarkCategory::ForwardChaining,
                    total_time: Duration::from_millis(10),
                    avg_time: Duration::from_micros(100),
                    min_time: Duration::from_micros(90),
                    max_time: Duration::from_micros(110),
                    std_dev: Duration::from_micros(5),
                    throughput: 10000.0,
                    samples: 100,
                    memory_bytes: 1024,
                },
                BenchmarkResult {
                    name: "slow".to_string(),
                    category: BenchmarkCategory::ForwardChaining,
                    total_time: Duration::from_millis(100),
                    avg_time: Duration::from_millis(1),
                    min_time: Duration::from_micros(900),
                    max_time: Duration::from_micros(1100),
                    std_dev: Duration::from_micros(50),
                    throughput: 1000.0,
                    samples: 100,
                    memory_bytes: 2048,
                },
            ],
            timestamp: std::time::SystemTime::now(),
        };

        let fastest = results
            .fastest_in_category(BenchmarkCategory::ForwardChaining)
            .ok_or("expected Some value")?;
        assert_eq!(fastest.name, "fast");

        let slowest = results
            .slowest_in_category(BenchmarkCategory::ForwardChaining)
            .ok_or("expected Some value")?;
        assert_eq!(slowest.name, "slow");
        Ok(())
    }

    #[test]
    fn test_run_all_benchmarks() -> Result<(), Box<dyn std::error::Error>> {
        let config = BenchmarkConfig::default().with_iterations(2).with_warmup(1);
        let mut suite = BenchmarkSuite::new(config);

        let results = suite.run_all()?;
        assert!(!results.results.is_empty());

        let (total_time, avg_throughput) = results.overall_stats();
        assert!(total_time.as_millis() > 0);
        assert!(avg_throughput > 0.0);
        Ok(())
    }

    #[test]
    fn test_memory_tracking() -> Result<(), Box<dyn std::error::Error>> {
        let config = BenchmarkConfig::default().with_iterations(1).with_warmup(0);
        let mut suite = BenchmarkSuite::new(config);

        let results = suite.run_category(BenchmarkCategory::ForwardChaining)?;

        // Verify memory tracking is working
        for result in &results.results {
            assert!(
                result.memory_bytes > 0,
                "Memory tracking should report non-zero bytes"
            );
            // Memory should be reasonable (> 1KB, < 100MB for our test datasets)
            assert!(
                result.memory_bytes >= 1024,
                "Memory usage should be at least 1KB"
            );
            assert!(
                result.memory_bytes <= 100 * 1024 * 1024,
                "Memory usage should be < 100MB for tests"
            );
        }
        Ok(())
    }
}