@@ -75,7 +75,7 @@ pub struct CommandCacheKey {
75
75
76
76
#[ derive( Default , Clone ) ]
77
77
pub struct CommandProfile {
78
- pub exec_traces : Vec < ExecutionTrace > ,
78
+ pub traces : Vec < ExecutionTrace > ,
79
79
}
80
80
81
81
#[ derive( Default ) ]
@@ -84,10 +84,57 @@ pub struct CommandProfiler {
84
84
}
85
85
86
86
impl CommandProfiler {
87
- pub fn record ( & self , key : CommandCacheKey , exec_trace : ExecutionTrace ) {
87
+ pub fn record_execution ( & self , key : CommandCacheKey , start_time : Instant ) {
88
88
let mut stats = self . stats . lock ( ) . unwrap ( ) ;
89
89
let entry = stats. entry ( key) . or_default ( ) ;
90
- entry. exec_traces . push ( exec_trace) ;
90
+ entry. traces . push ( ExecutionTrace :: Executed {
91
+ timestamp : start_time,
92
+ duration : start_time. elapsed ( ) ,
93
+ } ) ;
94
+ }
95
+
96
+ pub fn record_cache_hit ( & self , key : CommandCacheKey ) {
97
+ let mut stats = self . stats . lock ( ) . unwrap ( ) ;
98
+ let entry = stats. entry ( key) . or_default ( ) ;
99
+ entry. traces . push ( ExecutionTrace :: CacheHit { timestamp : Instant :: now ( ) } ) ;
100
+ }
101
+
102
+ pub fn report_summary ( & self ) {
103
+ let stats = self . stats . lock ( ) . unwrap ( ) ;
104
+
105
+ for ( key, profile) in stats. iter ( ) {
106
+ println ! ( "\n Command: {:?}" , key. program) ;
107
+
108
+ let mut hits = 0 ;
109
+ let mut runs = 0 ;
110
+ let mut max_duration: Option < Duration > = None ;
111
+
112
+ for trace in & profile. traces {
113
+ match trace {
114
+ ExecutionTrace :: CacheHit { timestamp } => {
115
+ hits += 1 ;
116
+ println ! ( " - Cache hit at: {:?}" , timestamp) ;
117
+ }
118
+ ExecutionTrace :: Executed { duration, timestamp } => {
119
+ runs += 1 ;
120
+ if max_duration. map_or ( true , |d| * duration > d) {
121
+ max_duration = Some ( * duration) ;
122
+ }
123
+ println ! ( " - Executed at: {:?}, duration: {:.2?}" , timestamp, duration) ;
124
+ }
125
+ }
126
+ }
127
+
128
+ let duration_str = match max_duration {
129
+ Some ( d) => format ! ( "{:.2?}" , d) ,
130
+ None => "-" . into ( ) ,
131
+ } ;
132
+
133
+ println ! (
134
+ " => Summary: {} run(s), {} hit(s), max_duration={}" ,
135
+ runs, hits, duration_str
136
+ ) ;
137
+ }
91
138
}
92
139
}
93
140
@@ -572,7 +619,7 @@ impl ExecutionContext {
572
619
if let Some ( cached_output) = self . command_cache . get ( & cache_key) {
573
620
command. mark_as_executed ( ) ;
574
621
self . verbose ( || println ! ( "Cache hit: {command:?}" ) ) ;
575
- self . profiler . record ( cache_key, ExecutionTrace :: CacheHit { timestamp : Instant :: now ( ) } ) ;
622
+ self . profiler . record_cache_hit ( cache_key) ;
576
623
return DeferredCommand { state : CommandState :: Cached ( cached_output) } ;
577
624
}
578
625
@@ -720,13 +767,7 @@ impl<'a> DeferredCommand<'a> {
720
767
&& command. should_cache
721
768
{
722
769
exec_ctx. command_cache . insert ( cache_key. clone ( ) , output. clone ( ) ) ;
723
- exec_ctx. profiler . record (
724
- cache_key. clone ( ) ,
725
- ExecutionTrace :: Executed {
726
- timestamp : start_time,
727
- duration : start_time. elapsed ( ) ,
728
- } ,
729
- ) ;
770
+ exec_ctx. profiler . record_execution ( cache_key. clone ( ) , start_time) ;
730
771
}
731
772
732
773
output
0 commit comments