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
//! Performance anomaly detection algorithms.
//!
//! This module implements various anomaly detection algorithms
//! to identify unusual performance patterns and outliers.
use std::collections::HashMap;
use std::time::SystemTime;
use serde::{Deserialize, Serialize};
use super::regression_detector::RegressionDetectionConfig;
use super::baseline_manager::BaselineData;
use super::performance_measurement::PerformanceMeasurement;
/// Detects performance anomalies using various algorithms
pub struct AnomalyDetector {
/// Configuration for anomaly detection
config: RegressionDetectionConfig,
}
/// Performance anomaly detection result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceAnomaly {
/// Test identifier
pub test_id: String,
/// Implementation identifier
pub implementation: String,
/// Anomaly type
pub anomaly_type: AnomalyType,
/// Anomaly score (higher = more anomalous)
pub anomaly_score: f64,
/// Measurement that triggered the anomaly
pub anomalous_measurement: PerformanceMeasurement,
/// Detection method used
pub detection_method: AnomalyDetectionMethod,
/// Timestamp when detected
pub detected_at: SystemTime,
}
/// Types of performance anomalies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnomalyType {
/// Unusually high performance outlier
OutlierHigh, // Unusually high performance
/// Unusually low performance outlier
OutlierLow, // Unusually low performance
/// Sudden sustained improvement
ShiftUp, // Sudden sustained improvement
/// Sudden sustained degradation
ShiftDown, // Sudden sustained degradation
/// Unusual performance variance
Volatility, // Unusual variance
/// Systematic performance bias
Systematic, // Systematic bias
}
/// Anomaly detection methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnomalyDetectionMethod {
/// Statistical outlier detection method
StatisticalOutlier,
/// Isolation Forest anomaly detection
IsolationForest,
/// DBSCAN clustering-based detection
DBSCAN,
/// Local Outlier Factor method
LocalOutlierFactor,
/// One-class SVM method
OneSVM,
/// Change point detection algorithm
ChangePointDetection,
}
impl AnomalyDetector {
/// Creates a new anomaly detector with the specified configuration.
pub fn new(config: RegressionDetectionConfig) -> Self {
Self { config }
}
/// Detects performance anomalies across all baseline data sets.
pub fn detect_anomalies(&self, baselines: &HashMap<String, BaselineData>) -> Vec<PerformanceAnomaly> {
let mut anomalies = Vec::new();
for baseline in baselines.values() {
anomalies.extend(self.detect_statistical_outliers(baseline));
}
anomalies
}
fn detect_statistical_outliers(&self, baseline: &BaselineData) -> Vec<PerformanceAnomaly> {
let mut anomalies = Vec::new();
if baseline.measurements.len() < 10 {
return anomalies;
}
let mean = baseline.statistics.mean;
let std_dev = baseline.statistics.std_dev;
let threshold = self.config.anomaly_sensitivity;
for measurement in &baseline.measurements {
let z_score = (measurement.value - mean).abs() / std_dev;
if z_score > threshold {
let anomaly_type = if measurement.value > mean {
AnomalyType::OutlierHigh
} else {
AnomalyType::OutlierLow
};
anomalies.push(PerformanceAnomaly {
test_id: baseline.test_id.clone(),
implementation: baseline.implementation_id.clone(),
anomaly_type,
anomaly_score: z_score,
anomalous_measurement: measurement.clone(),
detection_method: AnomalyDetectionMethod::StatisticalOutlier,
detected_at: SystemTime::now(),
});
}
}
anomalies
}
}