api_openai/
enhanced_client_performance.rs1use mod_interface::mod_interface;
7
8mod private
9{
10 use serde::{ Serialize, Deserialize };
11
12 #[ derive( Debug, Clone, Serialize, Deserialize ) ]
14 pub struct ConnectionPerformanceReport
15 {
16 pub efficiency_metrics : crate::connection_manager::ConnectionEfficiencyMetrics,
18 pub pool_stats : Vec< crate::connection_manager::PoolStatistics >,
20 pub analysis : PerformanceAnalysis,
22 }
23
24 #[ derive( Debug, Clone, Serialize, Deserialize ) ]
26 pub struct PerformanceAnalysis
27 {
28 pub grade : String,
30 pub kpis : Vec< String >,
32 pub recommendations : Vec< String >,
34 pub issues : Vec< String >,
36 }
37
38 #[ derive( Debug, Clone, Serialize, Deserialize ) ]
40 pub struct UnifiedPerformanceDashboard
41 {
42 pub overall_performance_score : f64,
44 pub connection_performance : ConnectionPerformanceReport,
46 #[ cfg( feature = "caching" ) ]
48 pub cache_performance : Option< crate::response_cache::CacheStatistics >,
49 #[ cfg( not( feature = "caching" ) ) ]
51 pub cache_performance : Option< () >,
52 pub metrics_summary : Option< crate::metrics_framework::MetricsSnapshot >,
54 pub recommendations : Vec< String >,
56 }
57
58 #[ 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 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 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 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 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 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}