quantrs2_circuit/profiler/
benchmarks.rs

1//! Benchmarking engine and regression detection
2//!
3//! This module provides comprehensive benchmarking capabilities including
4//! benchmark suite management, regression detection, baseline management,
5//! and performance trend analysis.
6
7use serde::{Deserialize, Serialize};
8use std::collections::{HashMap, VecDeque};
9use std::time::{Duration, SystemTime};
10
11// Import types from sibling modules
12use super::analyzers::*;
13use super::metrics::*;
14
15pub struct BenchmarkEngine {
16    /// Benchmark suites
17    pub benchmark_suites: HashMap<String, BenchmarkSuite>,
18    /// Benchmark results
19    pub benchmark_results: HashMap<String, BenchmarkResult>,
20    /// Comparison results
21    pub comparison_results: ComparisonResults,
22    /// Benchmark configuration
23    pub config: BenchmarkConfig,
24}
25
26/// Benchmark suite definition
27#[derive(Debug, Clone)]
28pub struct BenchmarkSuite {
29    /// Suite name
30    pub name: String,
31    /// Benchmark tests
32    pub tests: Vec<BenchmarkTest>,
33    /// Suite configuration
34    pub config: BenchmarkSuiteConfig,
35    /// Suite metadata
36    pub metadata: HashMap<String, String>,
37}
38
39/// Individual benchmark test
40#[derive(Debug, Clone)]
41pub struct BenchmarkTest {
42    /// Test name
43    pub name: String,
44    /// Test type
45    pub test_type: BenchmarkTestType,
46    /// Test parameters
47    pub parameters: HashMap<String, f64>,
48    /// Expected performance range
49    pub expected_range: (f64, f64),
50}
51
52/// Types of benchmark tests
53#[derive(Debug, Clone)]
54pub enum BenchmarkTestType {
55    /// Performance test
56    Performance,
57    /// Stress test
58    Stress,
59    /// Load test
60    Load,
61    /// Endurance test
62    Endurance,
63    /// Accuracy test
64    Accuracy,
65}
66
67/// Benchmark suite configuration
68#[derive(Debug, Clone)]
69pub struct BenchmarkSuiteConfig {
70    /// Number of iterations
71    pub iterations: usize,
72    /// Warmup iterations
73    pub warmup_iterations: usize,
74    /// Timeout per test
75    pub test_timeout: Duration,
76    /// Statistical confidence level
77    pub confidence_level: f64,
78}
79
80/// Benchmark execution result
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct BenchmarkResult {
83    /// Result timestamp
84    pub timestamp: SystemTime,
85    /// Suite name
86    pub suite_name: String,
87    /// Test results
88    pub test_results: HashMap<String, TestResult>,
89    /// Overall score
90    pub overall_score: f64,
91    /// Execution duration
92    pub execution_duration: Duration,
93}
94
95/// Individual test result
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct TestResult {
98    /// Test name
99    pub test_name: String,
100    /// Performance score
101    pub score: f64,
102    /// Execution time
103    pub execution_time: Duration,
104    /// Pass/fail status
105    pub passed: bool,
106    /// Error message if failed
107    pub error_message: Option<String>,
108    /// Test metadata
109    pub metadata: HashMap<String, String>,
110}
111
112/// Comparison results between benchmarks
113#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct ComparisonResults {
115    /// Baseline benchmark
116    pub baseline: String,
117    /// Comparison benchmarks
118    pub comparisons: HashMap<String, ComparisonSummary>,
119    /// Statistical significance tests
120    pub significance_tests: HashMap<String, f64>,
121    /// Performance regression analysis
122    pub regression_analysis: RegressionAnalysisResults,
123}
124
125/// Summary of benchmark comparison
126#[derive(Debug, Clone, Serialize, Deserialize)]
127pub struct ComparisonSummary {
128    /// Performance improvement
129    pub improvement: f64,
130    /// Statistical significance
131    pub significance: f64,
132    /// Confidence interval
133    pub confidence_interval: (f64, f64),
134    /// Effect size
135    pub effect_size: f64,
136}
137
138/// Regression analysis results
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct RegressionAnalysisResults {
141    /// Detected regressions
142    pub regressions: Vec<PerformanceRegression>,
143    /// Regression severity
144    pub severity_summary: HashMap<RegressionSeverity, usize>,
145    /// Trend analysis
146    pub trend_analysis: TrendAnalysisResults,
147}
148
149/// Performance regression information
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct PerformanceRegression {
152    /// Test name
153    pub test_name: String,
154    /// Regression magnitude
155    pub magnitude: f64,
156    /// Regression severity
157    pub severity: RegressionSeverity,
158    /// Statistical confidence
159    pub confidence: f64,
160    /// Probable cause
161    pub probable_cause: Option<String>,
162}
163
164/// Regression severity levels
165#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
166pub enum RegressionSeverity {
167    /// Minor regression
168    Minor,
169    /// Moderate regression
170    Moderate,
171    /// Major regression
172    Major,
173    /// Critical regression
174    Critical,
175}
176
177/// Trend analysis results
178#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct TrendAnalysisResults {
180    /// Performance trends
181    pub trends: HashMap<String, TrendDirection>,
182    /// Trend strengths
183    pub trend_strengths: HashMap<String, f64>,
184    /// Forecast confidence
185    pub forecast_confidence: HashMap<String, f64>,
186}
187
188/// Benchmark configuration
189#[derive(Debug, Clone, Serialize, Deserialize)]
190pub struct BenchmarkConfig {
191    /// Default iterations
192    pub default_iterations: usize,
193    /// Default timeout
194    pub default_timeout: Duration,
195    /// Enable statistical analysis
196    pub enable_statistical_analysis: bool,
197    /// Comparison baseline
198    pub comparison_baseline: Option<String>,
199    /// Auto-regression detection
200    pub auto_regression_detection: bool,
201}
202
203/// Regression detector for performance monitoring
204#[derive(Debug, Clone)]
205pub struct RegressionDetector {
206    /// Detection algorithms
207    pub algorithms: HashMap<String, RegressionDetectionAlgorithm>,
208    /// Detected regressions
209    pub detected_regressions: Vec<PerformanceRegression>,
210    /// Detection configuration
211    pub config: RegressionDetectionConfig,
212    /// Baseline management
213    pub baseline_manager: BaselineManager,
214}
215
216/// Regression detection algorithm
217#[derive(Debug, Clone)]
218pub struct RegressionDetectionAlgorithm {
219    /// Algorithm type
220    pub algorithm_type: RegressionAlgorithmType,
221    /// Algorithm parameters
222    pub parameters: HashMap<String, f64>,
223    /// Detection sensitivity
224    pub sensitivity: f64,
225    /// False positive rate
226    pub false_positive_rate: f64,
227}
228
229/// Types of regression detection algorithms
230#[derive(Debug, Clone)]
231pub enum RegressionAlgorithmType {
232    /// Statistical change point detection
233    ChangePointDetection,
234    /// Control chart analysis
235    ControlChart,
236    /// Trend analysis
237    TrendAnalysis,
238    /// Machine learning based
239    MachineLearning,
240    /// Composite algorithm
241    Composite,
242}
243
244/// Regression detection configuration
245#[derive(Debug, Clone, Serialize, Deserialize)]
246pub struct RegressionDetectionConfig {
247    /// Enable continuous monitoring
248    pub enable_continuous_monitoring: bool,
249    /// Detection window size
250    pub detection_window: Duration,
251    /// Minimum regression magnitude
252    pub min_regression_magnitude: f64,
253    /// Confidence threshold
254    pub confidence_threshold: f64,
255}
256
257/// Baseline management for regression detection
258#[derive(Debug, Clone)]
259pub struct BaselineManager {
260    /// Current baselines
261    pub baselines: HashMap<String, PerformanceBaseline>,
262    /// Baseline update policy
263    pub update_policy: BaselineUpdatePolicy,
264    /// Baseline validation
265    pub validation_results: BaselineValidationResults,
266}
267
268/// Performance baseline definition
269#[derive(Debug, Clone, Serialize, Deserialize)]
270pub struct PerformanceBaseline {
271    /// Baseline name
272    pub name: String,
273    /// Baseline values
274    pub values: HashMap<String, f64>,
275    /// Baseline timestamp
276    pub timestamp: SystemTime,
277    /// Baseline confidence
278    pub confidence: f64,
279    /// Baseline validity period
280    pub validity_period: Duration,
281}
282
283/// Baseline update policy
284#[derive(Debug, Clone, Serialize, Deserialize)]
285pub struct BaselineUpdatePolicy {
286    /// Update frequency
287    pub update_frequency: Duration,
288    /// Minimum data points for update
289    pub min_data_points: usize,
290    /// Update threshold
291    pub update_threshold: f64,
292    /// Auto-update enabled
293    pub auto_update: bool,
294}
295
296/// Baseline validation results
297#[derive(Debug, Clone, Serialize, Deserialize)]
298pub struct BaselineValidationResults {
299    /// Validation status
300    pub status: ValidationStatus,
301    /// Validation score
302    pub score: f64,
303    /// Validation timestamp
304    pub timestamp: SystemTime,
305    /// Validation errors
306    pub errors: Vec<String>,
307}
308
309/// Validation status
310#[derive(Debug, Clone, Serialize, Deserialize)]
311pub enum ValidationStatus {
312    /// Valid baseline
313    Valid,
314    /// Invalid baseline
315    Invalid,
316    /// Needs validation
317    NeedsValidation,
318    /// Validation in progress
319    Validating,
320}