api_openai/
enhanced_client_performance.rs

1//! Performance Monitoring and Analysis for Enhanced `OpenAI` Client
2//!
3//! This module provides performance metrics, analysis, and reporting structures
4//! for monitoring and optimizing `EnhancedClient` performance.
5
6use mod_interface::mod_interface;
7
8mod private
9{
10  use serde::{ Serialize, Deserialize };
11
12  /// Connection performance metrics for monitoring
13  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
14  pub struct ConnectionPerformanceReport
15  {
16    /// Overall efficiency metrics
17    pub efficiency_metrics : crate::connection_manager::ConnectionEfficiencyMetrics,
18    /// Per-pool statistics
19    pub pool_stats : Vec< crate::connection_manager::PoolStatistics >,
20    /// Performance analysis
21    pub analysis : PerformanceAnalysis,
22  }
23
24  /// Analysis of connection performance
25  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
26  pub struct PerformanceAnalysis
27  {
28    /// Overall performance grade (A, B, C, D, F)
29    pub grade : String,
30    /// Key performance indicators
31    pub kpis : Vec< String >,
32    /// Recommendations for improvement
33    pub recommendations : Vec< String >,
34    /// Potential issues identified
35    pub issues : Vec< String >,
36  }
37
38  /// Unified performance dashboard combining all components
39  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
40  pub struct UnifiedPerformanceDashboard
41  {
42    /// Overall performance score (0-100)
43    pub overall_performance_score : f64,
44    /// Connection performance metrics
45    pub connection_performance : ConnectionPerformanceReport,
46    /// Cache performance statistics (if available)
47    #[ cfg( feature = "caching" ) ]
48    pub cache_performance : Option< crate::response_cache::CacheStatistics >,
49    /// Placeholder for cache performance when feature is disabled
50    #[ cfg( not( feature = "caching" ) ) ]
51    pub cache_performance : Option< () >,
52    /// Metrics summary (if available)
53    pub metrics_summary : Option< crate::metrics_framework::MetricsSnapshot >,
54    /// Unified recommendations from all components
55    pub recommendations : Vec< String >,
56  }
57
58  /// Analyze connection performance and provide recommendations
59  #[ must_use ]
60  #[ inline ]
61  pub fn analyze_performance(
62    efficiency : &crate::connection_manager::ConnectionEfficiencyMetrics,
63    pools : &[ crate::connection_manager::PoolStatistics ],
64  ) -> PerformanceAnalysis
65  {
66    let mut kpis = Vec::new();
67    let mut recommendations = Vec::new();
68    let mut issues = Vec::new();
69
70    // Analyze efficiency score
71    let grade = match efficiency.efficiency_score
72    {
73      s if s >= 0.9 => "A",
74      s if s >= 0.8 => "B",
75      s if s >= 0.7 => "C",
76      s if s >= 0.6 => "D",
77      _ => "F",
78    };
79
80    kpis.push( format!( "Efficiency Score : {:.1}%", efficiency.efficiency_score * 100.0 ) );
81    kpis.push( format!( "Connection Reuse Ratio : {:.1}", efficiency.connection_reuse_ratio ) );
82    kpis.push( format!( "Average Pool Utilization : {:.1}%", efficiency.average_pool_utilization * 100.0 ) );
83
84    // Analyze connection reuse
85    if efficiency.connection_reuse_ratio < 5.0
86    {
87      issues.push( "Low connection reuse - connections are not being reused efficiently".to_string() );
88      recommendations.push( "Increase connection pool size or check for connection leaks".to_string() );
89    }
90    else if efficiency.connection_reuse_ratio > 100.0
91    {
92      issues.push( "Extremely high connection reuse - may indicate connection starvation".to_string() );
93      recommendations.push( "Increase maximum connections per host".to_string() );
94    }
95
96    // Analyze pool utilization
97    if efficiency.average_pool_utilization < 0.3
98    {
99      recommendations.push( "Pool utilization is low - consider reducing minimum connections".to_string() );
100    }
101    else if efficiency.average_pool_utilization > 0.9
102    {
103      issues.push( "High pool utilization - may cause connection wait times".to_string() );
104      recommendations.push( "Increase maximum connections per host".to_string() );
105    }
106
107    // Analyze individual pools
108    for pool in pools
109    {
110      if pool.current_utilization > 0.95
111      {
112        issues.push( format!( "Pool for {} is overutilized ({:.1}%)", pool.host, pool.current_utilization * 100.0 ) );
113      }
114
115      if pool.total_connections_created > pool.total_requests_served
116      {
117        issues.push( format!( "Pool for {} has poor connection reuse", pool.host ) );
118      }
119    }
120
121    // General recommendations
122    if efficiency.efficiency_score < 0.8
123    {
124      recommendations.push( "Consider tuning connection pool parameters".to_string() );
125      recommendations.push( "Monitor connection health and cleanup intervals".to_string() );
126    }
127
128    PerformanceAnalysis
129    {
130      grade : grade.to_string(),
131      kpis,
132      recommendations,
133      issues,
134    }
135  }
136}
137
138mod_interface!
139{
140  exposed use
141  {
142    ConnectionPerformanceReport,
143    PerformanceAnalysis,
144    UnifiedPerformanceDashboard,
145    analyze_performance,
146  };
147}