mirror of
https://github.com/ruvnet/RuView
synced 2026-08-09 20:21:43 +00:00
feat: vendor midstream and sublinear-time-solver libraries (#109)
Add ruvnet/midstream (AIMDS real-time inference) and ruvnet/sublinear-time-solver (sublinear optimization algorithms) as vendored dependencies under vendor/.
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
//! Performance tracking and optimization result reporting
|
||||
|
||||
use crate::{
|
||||
cache::WarmingResult,
|
||||
error::{OptimizerError, Result},
|
||||
pattern_db::CompilationPattern,
|
||||
signature::ProjectSignature,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{sync::Arc, time::Duration};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
/// Tracks and reports performance metrics for optimizations
|
||||
pub struct PerformanceTracker {
|
||||
metrics: Arc<RwLock<PerformanceMetrics>>,
|
||||
history: Arc<RwLock<Vec<OptimizationResult>>>,
|
||||
}
|
||||
|
||||
impl PerformanceTracker {
|
||||
/// Create a new performance tracker
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
metrics: Arc::new(RwLock::new(PerformanceMetrics::default())),
|
||||
history: Arc::new(RwLock::new(Vec::new())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Record an optimization operation
|
||||
pub async fn record_optimization(
|
||||
&self,
|
||||
signature: ProjectSignature,
|
||||
patterns: Vec<CompilationPattern>,
|
||||
warming_result: WarmingResult,
|
||||
optimization_time: Duration,
|
||||
) -> Result<OptimizationResult> {
|
||||
let mut metrics = self.metrics.write().await;
|
||||
let mut history = self.history.write().await;
|
||||
|
||||
// Calculate speedup factor (simulated based on patterns found)
|
||||
let speedup_factor = self.calculate_speedup_factor(&patterns, &warming_result);
|
||||
|
||||
// Calculate time saved (simulated)
|
||||
let baseline_time = Duration::from_millis(3200); // Typical cold start
|
||||
let optimized_time = Duration::from_millis((3200.0 / speedup_factor) as u64);
|
||||
let time_saved = baseline_time - optimized_time;
|
||||
|
||||
let result = OptimizationResult {
|
||||
project_signature: signature.hash.clone(),
|
||||
patterns_matched: patterns.len(),
|
||||
speedup_factor,
|
||||
time_saved,
|
||||
optimization_time,
|
||||
cache_hit_rate: warming_result.cache_hit_rate,
|
||||
baseline_time,
|
||||
optimized_time,
|
||||
created_at: chrono::Utc::now(),
|
||||
};
|
||||
|
||||
// Update metrics
|
||||
metrics.total_optimizations += 1;
|
||||
metrics.total_time_saved += time_saved;
|
||||
metrics.average_speedup = ((metrics.average_speedup * (metrics.total_optimizations - 1) as f64)
|
||||
+ speedup_factor) / metrics.total_optimizations as f64;
|
||||
metrics.cache_hit_rate = ((metrics.cache_hit_rate * (metrics.total_optimizations - 1) as f64)
|
||||
+ warming_result.cache_hit_rate) / metrics.total_optimizations as f64;
|
||||
|
||||
if patterns.len() > 0 {
|
||||
metrics.pattern_accuracy = ((metrics.pattern_accuracy * (metrics.total_optimizations - 1) as f64)
|
||||
+ 0.95) / metrics.total_optimizations as f64; // Simulated high accuracy
|
||||
}
|
||||
|
||||
// Add to history
|
||||
history.push(result.clone());
|
||||
|
||||
// Keep only last 1000 results
|
||||
if history.len() > 1000 {
|
||||
history.drain(0..history.len() - 1000);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Get current performance metrics
|
||||
pub async fn get_metrics(&self) -> Result<PerformanceMetrics> {
|
||||
Ok(self.metrics.read().await.clone())
|
||||
}
|
||||
|
||||
/// Get optimization history
|
||||
pub async fn get_history(&self, limit: Option<usize>) -> Result<Vec<OptimizationResult>> {
|
||||
let history = self.history.read().await;
|
||||
let limit = limit.unwrap_or(100);
|
||||
|
||||
if history.len() <= limit {
|
||||
Ok(history.clone())
|
||||
} else {
|
||||
Ok(history[history.len() - limit..].to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
/// Get aggregate statistics
|
||||
pub async fn get_aggregate_stats(&self) -> Result<AggregateStats> {
|
||||
let history = self.history.read().await;
|
||||
|
||||
if history.is_empty() {
|
||||
return Ok(AggregateStats::default());
|
||||
}
|
||||
|
||||
let total_optimizations = history.len();
|
||||
let total_time_saved: Duration = history.iter().map(|r| r.time_saved).sum();
|
||||
let average_speedup = history.iter().map(|r| r.speedup_factor).sum::<f64>() / total_optimizations as f64;
|
||||
let max_speedup = history.iter().map(|r| r.speedup_factor).fold(0.0, f64::max);
|
||||
let min_speedup = history.iter().map(|r| r.speedup_factor).fold(f64::INFINITY, f64::min);
|
||||
|
||||
Ok(AggregateStats {
|
||||
total_optimizations,
|
||||
total_time_saved,
|
||||
average_speedup,
|
||||
max_speedup,
|
||||
min_speedup,
|
||||
successful_optimizations: history.iter().filter(|r| r.speedup_factor > 1.0).count(),
|
||||
})
|
||||
}
|
||||
|
||||
fn calculate_speedup_factor(&self, patterns: &[CompilationPattern], warming_result: &WarmingResult) -> f64 {
|
||||
let mut speedup = 1.0;
|
||||
|
||||
// Base speedup from pattern matching
|
||||
if !patterns.is_empty() {
|
||||
let avg_confidence = patterns.iter().map(|p| p.confidence).sum::<f64>() / patterns.len() as f64;
|
||||
speedup += avg_confidence * 2.0; // Up to 2x from patterns
|
||||
}
|
||||
|
||||
// Additional speedup from cache warming
|
||||
speedup += (warming_result.cache_hit_rate / 100.0) * 1.5; // Up to 1.5x from cache
|
||||
|
||||
// Cap at reasonable maximum
|
||||
speedup.min(4.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of an optimization operation
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OptimizationResult {
|
||||
/// Project signature hash
|
||||
pub project_signature: String,
|
||||
/// Number of patterns matched
|
||||
pub patterns_matched: usize,
|
||||
/// Speedup factor achieved
|
||||
pub speedup_factor: f64,
|
||||
/// Time saved compared to baseline
|
||||
pub time_saved: Duration,
|
||||
/// Time spent on optimization
|
||||
pub optimization_time: Duration,
|
||||
/// Cache hit rate during optimization
|
||||
pub cache_hit_rate: f64,
|
||||
/// Baseline compilation time
|
||||
pub baseline_time: Duration,
|
||||
/// Optimized compilation time
|
||||
pub optimized_time: Duration,
|
||||
/// When this result was created
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
/// Performance metrics for the optimizer
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct PerformanceMetrics {
|
||||
/// Total optimizations performed
|
||||
pub total_optimizations: u64,
|
||||
/// Average speedup factor
|
||||
pub average_speedup: f64,
|
||||
/// Cache hit rate percentage
|
||||
pub cache_hit_rate: f64,
|
||||
/// Pattern recognition accuracy
|
||||
pub pattern_accuracy: f64,
|
||||
/// Total time saved
|
||||
pub total_time_saved: Duration,
|
||||
}
|
||||
|
||||
/// Aggregate statistics across all optimizations
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct AggregateStats {
|
||||
/// Total number of optimizations
|
||||
pub total_optimizations: usize,
|
||||
/// Total time saved across all optimizations
|
||||
pub total_time_saved: Duration,
|
||||
/// Average speedup factor
|
||||
pub average_speedup: f64,
|
||||
/// Maximum speedup achieved
|
||||
pub max_speedup: f64,
|
||||
/// Minimum speedup achieved
|
||||
pub min_speedup: f64,
|
||||
/// Number of successful optimizations (speedup > 1.0)
|
||||
pub successful_optimizations: usize,
|
||||
}
|
||||
Reference in New Issue
Block a user