@@ -54,11 +54,11 @@ pub struct Metrics {
54
54
}
55
55
56
56
#[ derive( Debug , Clone ) ]
57
- pub struct HeavyMetrics {
57
+ pub struct PinnedMetrics {
58
58
// It is *intentional* that this is only a vector
59
59
// We don't need a potentially expensive hashing algorithm here
60
60
// The checksums are sourced from a hashmap already, ensuring uniqueness of the checksums
61
- pub hits_per_pinned_contract : Vec < ( Checksum , u32 ) > ,
61
+ pub hits_per_contract : Vec < ( Checksum , u32 ) > ,
62
62
}
63
63
64
64
#[ derive( Clone , Debug ) ]
@@ -190,17 +190,15 @@ where
190
190
self . inner . lock ( ) . unwrap ( ) . stats
191
191
}
192
192
193
- pub fn heavy_metrics ( & self ) -> HeavyMetrics {
193
+ pub fn pinned_metrics ( & self ) -> PinnedMetrics {
194
194
let cache = self . inner . lock ( ) . unwrap ( ) ;
195
- let hits_per_pinned_contract = cache
195
+ let hits_per_contract = cache
196
196
. pinned_memory_cache
197
197
. iter ( )
198
198
. map ( |( checksum, module) | ( * checksum, module. hits ) )
199
199
. collect ( ) ;
200
200
201
- HeavyMetrics {
202
- hits_per_pinned_contract,
203
- }
201
+ PinnedMetrics { hits_per_contract }
204
202
}
205
203
206
204
pub fn metrics ( & self ) -> Metrics {
@@ -1431,22 +1429,41 @@ mod tests {
1431
1429
}
1432
1430
1433
1431
#[ test]
1434
- fn heavy_metrics_works ( ) {
1432
+ fn pinned_metrics_works ( ) {
1435
1433
let cache = unsafe { Cache :: new ( make_testing_options ( ) ) . unwrap ( ) } ;
1436
1434
let checksum = cache. save_wasm ( CONTRACT ) . unwrap ( ) ;
1437
1435
1438
1436
cache. pin ( & checksum) . unwrap ( ) ;
1439
1437
1440
- let heavy_metrics = cache. heavy_metrics ( ) ;
1441
- assert_eq ! ( heavy_metrics . hits_per_pinned_contract , [ ( checksum, 0 ) ] ) ;
1438
+ let pinned_metrics = cache. pinned_metrics ( ) ;
1439
+ assert_eq ! ( pinned_metrics . hits_per_contract , [ ( checksum, 0 ) ] ) ;
1442
1440
1443
1441
let backend = mock_backend ( & [ ] ) ;
1444
1442
let _ = cache
1445
1443
. get_instance ( & checksum, backend, TESTING_OPTIONS )
1446
1444
. unwrap ( ) ;
1447
1445
1448
- let heavy_metrics = cache. heavy_metrics ( ) ;
1449
- assert_eq ! ( heavy_metrics. hits_per_pinned_contract, [ ( checksum, 1 ) ] ) ;
1446
+ let pinned_metrics = cache. pinned_metrics ( ) ;
1447
+ assert_eq ! ( pinned_metrics. hits_per_contract, [ ( checksum, 1 ) ] ) ;
1448
+
1449
+ let empty_checksum = cache. save_wasm ( EMPTY_CONTRACT ) . unwrap ( ) ;
1450
+ cache. pin ( & empty_checksum) . unwrap ( ) ;
1451
+
1452
+ let pinned_metrics = cache. pinned_metrics ( ) ;
1453
+ assert_eq ! ( pinned_metrics. hits_per_contract. len( ) , 2 ) ;
1454
+
1455
+ let get_module_hits = |checksum| {
1456
+ pinned_metrics
1457
+ . hits_per_contract
1458
+ . iter ( )
1459
+ . find ( |( iter_checksum, _module) | * iter_checksum == checksum)
1460
+ . map ( |( _checksum, module) | module)
1461
+ . copied ( )
1462
+ . unwrap ( )
1463
+ } ;
1464
+
1465
+ assert_eq ! ( get_module_hits( checksum) , 1 ) ;
1466
+ assert_eq ! ( get_module_hits( empty_checksum) , 0 ) ;
1450
1467
}
1451
1468
1452
1469
#[ test]
0 commit comments