1010use std:: collections:: HashMap ;
1111use std:: ffi:: { OsStr , OsString } ;
1212use std:: fmt:: { Debug , Formatter } ;
13+ use std:: fs:: File ;
1314use std:: hash:: Hash ;
15+ use std:: io:: { BufWriter , Write } ;
1416use std:: panic:: Location ;
1517use std:: path:: Path ;
18+ use std:: process;
1619use std:: process:: {
1720 Child , ChildStderr , ChildStdout , Command , CommandArgs , CommandEnvs , ExitStatus , Output , Stdio ,
1821} ;
@@ -100,10 +103,22 @@ impl CommandProfiler {
100103 }
101104
102105 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) ;
103118 let stats = self . stats . lock ( ) . unwrap ( ) ;
104119
105120 for ( key, profile) in stats. iter ( ) {
106- println ! ( " \n Command : {:?}", key. program) ;
121+ writeln ! ( writer , "Command : {:?}", key. program) . unwrap ( ) ;
107122
108123 let mut hits = 0 ;
109124 let mut runs = 0 ;
@@ -113,25 +128,37 @@ impl CommandProfiler {
113128 match trace {
114129 ExecutionTrace :: CacheHit { timestamp } => {
115130 hits += 1 ;
116- println ! ( " - Cache hit at: {timestamp :?}" ) ;
131+ writeln ! ( writer , " - Cache hit at: { :?}" , timestamp ) . unwrap ( ) ;
117132 }
118133 ExecutionTrace :: Executed { duration, timestamp } => {
119134 runs += 1 ;
120135 if max_duration. is_none_or ( |d| * duration > d) {
121136 max_duration = Some ( * duration) ;
122137 }
123- println ! ( " - Executed at: {timestamp:?}, duration: {duration:.2?}" ) ;
138+ writeln ! (
139+ writer,
140+ " - Executed at: {:?}, duration: {:.2?}" ,
141+ timestamp, duration
142+ )
143+ . unwrap ( ) ;
124144 }
125145 }
126146 }
127147
128148 let duration_str = match max_duration {
129- Some ( d) => format ! ( "{d :.2?}" ) ,
149+ Some ( d) => format ! ( "{:.2?}" , d ) ,
130150 None => "-" . into ( ) ,
131151 } ;
132152
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 ( ) ;
134158 }
159+
160+ // Print a message to user
161+ println ! ( "Command profiler report saved to {filename}" ) ;
135162 }
136163}
137164
0 commit comments