oasis_core_runtime/storage/mkvs/sync/
stats.rs

1use std::any::Any;
2
3use anyhow::Result;
4
5use super::{GetPrefixesRequest, GetRequest, IterateRequest, ProofResponse, ReadSync};
6
7/// A proxy read syncer which keeps track of call statistics.
8pub struct StatsCollector {
9    /// Count of `sync_get` calls made to the underlying read syncer.
10    pub sync_get_count: usize,
11    /// Count of `sync_get_prefixes` calls made to the underlying read syncer.
12    pub sync_get_prefixes_count: usize,
13    /// Count of `sync_iterate` calls made to the underlying read syncer.
14    pub sync_iterate_count: usize,
15
16    rs: Box<dyn ReadSync>,
17}
18
19impl StatsCollector {
20    /// Construct a new instance, proxying to the given backing read syncer.
21    pub fn new(rs: Box<dyn ReadSync>) -> StatsCollector {
22        StatsCollector {
23            sync_get_count: 0,
24            sync_get_prefixes_count: 0,
25            sync_iterate_count: 0,
26            rs,
27        }
28    }
29}
30
31impl ReadSync for StatsCollector {
32    fn as_any(&self) -> &dyn Any {
33        self
34    }
35
36    fn sync_get(&mut self, request: GetRequest) -> Result<ProofResponse> {
37        self.sync_get_count += 1;
38        self.rs.sync_get(request)
39    }
40
41    fn sync_get_prefixes(&mut self, request: GetPrefixesRequest) -> Result<ProofResponse> {
42        self.sync_get_prefixes_count += 1;
43        self.rs.sync_get_prefixes(request)
44    }
45
46    fn sync_iterate(&mut self, request: IterateRequest) -> Result<ProofResponse> {
47        self.sync_iterate_count += 1;
48        self.rs.sync_iterate(request)
49    }
50}