@@ -3,6 +3,8 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
3
3
use std:: sync:: Arc ;
4
4
use std:: thread;
5
5
use std:: time:: { Duration , Instant } ;
6
+ #[ cfg( feature = "stats" ) ]
7
+ use sysinfo:: { Pid , System } ;
6
8
7
9
const SLIDING_WINDOW_SIZE : u64 = 2 ; // In seconds
8
10
const BATCH_SIZE : u64 = 1000 ;
27
29
} )
28
30
. expect ( "Error setting Ctrl-C handler" ) ;
29
31
let num_threads = num_cpus:: get ( ) ;
30
- println ! ( "Number of threads: {}" , num_threads) ;
32
+ println ! ( "Number of threads: {}\n " , num_threads) ;
31
33
let mut handles = Vec :: with_capacity ( num_threads) ;
32
34
let func_arc = Arc :: new ( func) ;
33
35
let mut worker_stats_vec: Vec < WorkerStats > = Vec :: new ( ) ;
42
44
let mut start_time = Instant :: now ( ) ;
43
45
let mut end_time = start_time;
44
46
let mut total_count_old: u64 = 0 ;
47
+
48
+ #[ cfg( feature = "stats" ) ]
49
+ let pid = Pid :: from ( std:: process:: id ( ) as usize ) ;
50
+ #[ cfg( feature = "stats" ) ]
51
+ let mut system = System :: new_all ( ) ;
52
+
45
53
loop {
46
54
let elapsed = end_time. duration_since ( start_time) . as_secs ( ) ;
47
55
if elapsed >= SLIDING_WINDOW_SIZE {
56
64
"Throughput: {} iterations/sec" ,
57
65
throughput. to_formatted_string( & Locale :: en)
58
66
) ;
67
+
68
+ #[ cfg( feature = "stats" ) ]
69
+ {
70
+ system. refresh_all ( ) ;
71
+ if let Some ( process) = system. process ( pid) {
72
+ println ! (
73
+ "Memory usage: {:.2} MB" ,
74
+ process. memory( ) as f64 / ( 1024.0 * 1024.0 )
75
+ ) ;
76
+ println ! ( "CPU usage: {}%" , process. cpu_usage( ) / num_threads as f32 ) ;
77
+ println ! (
78
+ "Virtual memory usage: {:.2} MB" ,
79
+ process. virtual_memory( ) as f64 / ( 1024.0 * 1024.0 )
80
+ ) ;
81
+ } else {
82
+ println ! ( "Process not found" ) ;
83
+ }
84
+ }
85
+
86
+ println ! ( "\n " ) ;
59
87
start_time = Instant :: now ( ) ;
60
88
}
61
89
0 commit comments