@@ -132,23 +132,60 @@ pub struct SelfProfileKey {
132
132
pub scenario : database:: Scenario ,
133
133
}
134
134
135
+ #[ derive( Default ) ]
136
+ pub struct SelfProfileCacheStats {
137
+ hits : u64 ,
138
+ misses : u64 ,
139
+ }
140
+
141
+ impl SelfProfileCacheStats {
142
+ pub fn get_hits ( & self ) -> u64 {
143
+ self . hits
144
+ }
145
+ pub fn get_misses ( & self ) -> u64 {
146
+ self . misses
147
+ }
148
+
149
+ fn hit ( & mut self ) {
150
+ self . hits += 1 ;
151
+ }
152
+ fn miss ( & mut self ) {
153
+ self . misses += 1 ;
154
+ }
155
+ }
156
+
135
157
/// Stores a cache of N most recently used self profiles.
136
158
/// The profiles are downloaded from S3 and analysed on each request to the detailed compare result
137
159
/// page, but the post-processed results aren't very large in memory (~50 KiB), so it makes sense
138
160
/// to cache them.
139
161
pub struct SelfProfileCache {
140
162
profiles : LruCache < SelfProfileKey , SelfProfileWithAnalysis > ,
163
+ stats : SelfProfileCacheStats ,
141
164
}
142
165
143
166
impl SelfProfileCache {
144
167
pub fn new ( cache_size : usize ) -> Self {
145
168
Self {
146
169
profiles : LruCache :: new ( NonZeroUsize :: new ( cache_size) . unwrap ( ) ) ,
170
+ stats : Default :: default ( ) ,
147
171
}
148
172
}
149
173
174
+ pub fn get_stats ( & self ) -> & SelfProfileCacheStats {
175
+ & self . stats
176
+ }
177
+
150
178
pub fn get ( & mut self , key : & SelfProfileKey ) -> Option < SelfProfileWithAnalysis > {
151
- self . profiles . get ( key) . cloned ( )
179
+ match self . profiles . get ( key) {
180
+ Some ( value) => {
181
+ self . stats . hit ( ) ;
182
+ Some ( value. clone ( ) )
183
+ }
184
+ None => {
185
+ self . stats . miss ( ) ;
186
+ None
187
+ }
188
+ }
152
189
}
153
190
154
191
pub fn insert ( & mut self , key : SelfProfileKey , profile : SelfProfileWithAnalysis ) {
0 commit comments