10
10
use std:: collections:: HashMap ;
11
11
use std:: ffi:: { OsStr , OsString } ;
12
12
use std:: fmt:: { Debug , Formatter } ;
13
+ use std:: fs:: File ;
13
14
use std:: hash:: Hash ;
15
+ use std:: io:: { BufWriter , Write } ;
14
16
use std:: panic:: Location ;
15
17
use std:: path:: Path ;
18
+ use std:: process;
16
19
use std:: process:: {
17
20
Child , ChildStderr , ChildStdout , Command , CommandArgs , CommandEnvs , ExitStatus , Output , Stdio ,
18
21
} ;
@@ -100,10 +103,22 @@ impl CommandProfiler {
100
103
}
101
104
102
105
pub fn report_summary ( & self ) {
106
+ let pid = process:: id ( ) ;
107
+ let filename = format ! ( "bootstrap-profile-{pid}.txt" ) ;
108
+
109
+ let file = match File :: create ( & filename) {
110
+ Ok ( f) => f,
111
+ Err ( e) => {
112
+ eprintln ! ( "Failed to create profiler output file: {e}" ) ;
113
+ return ;
114
+ }
115
+ } ;
116
+
117
+ let mut writer = BufWriter :: new ( file) ;
103
118
let stats = self . stats . lock ( ) . unwrap ( ) ;
104
119
105
120
for ( key, profile) in stats. iter ( ) {
106
- println ! ( " \n Command : {:?}", key. program) ;
121
+ writeln ! ( writer , "Command : {:?}", key. program) . unwrap ( ) ;
107
122
108
123
let mut hits = 0 ;
109
124
let mut runs = 0 ;
@@ -113,25 +128,37 @@ impl CommandProfiler {
113
128
match trace {
114
129
ExecutionTrace :: CacheHit { timestamp } => {
115
130
hits += 1 ;
116
- println ! ( " - Cache hit at: {timestamp :?}" ) ;
131
+ writeln ! ( writer , " - Cache hit at: { :?}" , timestamp ) . unwrap ( ) ;
117
132
}
118
133
ExecutionTrace :: Executed { duration, timestamp } => {
119
134
runs += 1 ;
120
135
if max_duration. is_none_or ( |d| * duration > d) {
121
136
max_duration = Some ( * duration) ;
122
137
}
123
- println ! ( " - Executed at: {timestamp:?}, duration: {duration:.2?}" ) ;
138
+ writeln ! (
139
+ writer,
140
+ " - Executed at: {:?}, duration: {:.2?}" ,
141
+ timestamp, duration
142
+ )
143
+ . unwrap ( ) ;
124
144
}
125
145
}
126
146
}
127
147
128
148
let duration_str = match max_duration {
129
- Some ( d) => format ! ( "{d :.2?}" ) ,
149
+ Some ( d) => format ! ( "{:.2?}" , d ) ,
130
150
None => "-" . into ( ) ,
131
151
} ;
132
152
133
- println ! ( "Summary: {runs} run(s), {hits} hit(s), max_duration={duration_str}" ) ;
153
+ writeln ! (
154
+ writer,
155
+ "Summary: {runs} run(s), {hits} hit(s), max_duration={duration_str}\n "
156
+ )
157
+ . unwrap ( ) ;
134
158
}
159
+
160
+ // Print a message to user
161
+ println ! ( "Command profiler report saved to {filename}" ) ;
135
162
}
136
163
}
137
164
0 commit comments