1- use std:: sync:: Arc ;
21use anyhow:: Result ;
2+ use std:: sync:: Arc ;
33use std:: time:: { Duration , Instant } ;
44use sysinfo:: System ;
55use tokio:: sync:: Mutex ;
@@ -24,7 +24,11 @@ impl PerformanceMonitor {
2424 }
2525
2626 /// Create a new performance monitor with custom thresholds
27- pub fn with_thresholds ( timeout : Duration , memory_threshold_mb : usize , cpu_threshold_percent : f64 ) -> Self {
27+ pub fn with_thresholds (
28+ timeout : Duration ,
29+ memory_threshold_mb : usize ,
30+ cpu_threshold_percent : f64 ,
31+ ) -> Self {
2832 let mut system = System :: new_all ( ) ;
2933 system. refresh_all ( ) ;
3034
@@ -59,13 +63,22 @@ impl PerformanceMonitor {
5963
6064 // Check thresholds
6165 if duration > self . timeout_duration {
62- warn ! ( "Operation {} exceeded timeout threshold: {:?} > {:?}" , operation_name, duration, self . timeout_duration) ;
66+ warn ! (
67+ "Operation {} exceeded timeout threshold: {:?} > {:?}" ,
68+ operation_name, duration, self . timeout_duration
69+ ) ;
6370 }
6471 if metrics. memory_usage_mb > self . memory_threshold_mb as f64 {
65- warn ! ( "Operation {} exceeded memory threshold: {:.1}MB > {}MB" , operation_name, metrics. memory_usage_mb, self . memory_threshold_mb) ;
72+ warn ! (
73+ "Operation {} exceeded memory threshold: {:.1}MB > {}MB" ,
74+ operation_name, metrics. memory_usage_mb, self . memory_threshold_mb
75+ ) ;
6676 }
6777 if metrics. cpu_usage > self . cpu_threshold_percent {
68- warn ! ( "Operation {} exceeded CPU threshold: {:.1}% > {:.1}%" , operation_name, metrics. cpu_usage, self . cpu_threshold_percent) ;
78+ warn ! (
79+ "Operation {} exceeded CPU threshold: {:.1}% > {:.1}%" ,
80+ operation_name, metrics. cpu_usage, self . cpu_threshold_percent
81+ ) ;
6982 }
7083
7184 Ok ( ( ) )
@@ -100,7 +113,10 @@ impl PerformanceMonitor {
100113
101114 let elapsed = start_time. elapsed ( ) ;
102115 if elapsed > timeout_duration {
103- error ! ( "Monitoring timeout exceeded: {:?} > {:?}" , elapsed, timeout_duration) ;
116+ error ! (
117+ "Monitoring timeout exceeded: {:?} > {:?}" ,
118+ elapsed, timeout_duration
119+ ) ;
104120 break ;
105121 }
106122
@@ -162,11 +178,20 @@ impl<T> MonitoredOperation<T> {
162178 self . monitor . start_operation ( & self . operation_name ) ;
163179
164180 // Start monitoring
165- self . monitor . start_async_monitoring ( Duration :: from_secs ( 10 ) ) . await ;
181+ self . monitor
182+ . start_async_monitoring ( Duration :: from_secs ( 10 ) )
183+ . await ;
166184
167185 // Execute with timeout
168- let result = time:: timeout ( self . monitor . timeout_duration , operation ( ) ) . await
169- . map_err ( |_| anyhow:: anyhow!( "Operation {} timed out after {:?}" , self . operation_name, self . monitor. timeout_duration) ) ?
186+ let result = time:: timeout ( self . monitor . timeout_duration , operation ( ) )
187+ . await
188+ . map_err ( |_| {
189+ anyhow:: anyhow!(
190+ "Operation {} timed out after {:?}" ,
191+ self . operation_name,
192+ self . monitor. timeout_duration
193+ )
194+ } ) ?
170195 . map_err ( |e| anyhow:: anyhow!( "Operation {} failed: {}" , self . operation_name, e) ) ?;
171196
172197 self . monitor . end_operation ( & self . operation_name ) . await ?;
@@ -199,10 +224,12 @@ mod tests {
199224 async fn test_monitored_operation ( ) {
200225 let mut monitored = MonitoredOperation :: < String > :: new ( "test_async_op" ) ;
201226
202- let result = monitored. execute ( || async {
203- sleep ( Duration :: from_millis ( 50 ) ) . await ;
204- Ok ( "success" . to_string ( ) )
205- } ) . await ;
227+ let result = monitored
228+ . execute ( || async {
229+ sleep ( Duration :: from_millis ( 50 ) ) . await ;
230+ Ok ( "success" . to_string ( ) )
231+ } )
232+ . await ;
206233
207234 assert ! ( result. is_ok( ) ) ;
208235 assert_eq ! ( result. unwrap( ) , "success" ) ;
0 commit comments