forked from entros-protocol/executor-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus_metrics.rs
More file actions
131 lines (113 loc) · 3.73 KB
/
status_metrics.rs
File metadata and controls
131 lines (113 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::sync::atomic::{AtomicU64, Ordering};
pub struct StatusMetrics {
total_verifications_relayed: AtomicU64,
total_attestations_issued: AtomicU64,
start_time: u64,
cached_balance: AtomicU64,
balance_fetched_at: AtomicU64,
}
impl StatusMetrics {
pub fn new() -> Self {
Self {
total_verifications_relayed: AtomicU64::new(0),
total_attestations_issued: AtomicU64::new(0),
start_time: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
cached_balance: AtomicU64::new(0),
balance_fetched_at: AtomicU64::new(0),
}
}
// increase total_verifications_relayed by 1
pub fn increment_verifications(&self) {
self.total_verifications_relayed
.fetch_add(1, Ordering::Relaxed);
}
// increase total_attestations_issued by 1
pub fn increment_attestations(&self) {
self.total_attestations_issued
.fetch_add(1, Ordering::Relaxed);
}
// getters for the metrics
pub fn verifications_relayed(&self) -> u64 {
self.total_verifications_relayed.load(Ordering::Relaxed)
}
pub fn attestations_issued(&self) -> u64 {
self.total_attestations_issued.load(Ordering::Relaxed)
}
pub fn start_time(&self) -> u64 {
self.start_time
}
pub fn cached_balance(&self) -> u64 {
self.cached_balance.load(Ordering::Relaxed)
}
pub fn balance_fetched_at(&self) -> u64 {
self.balance_fetched_at.load(Ordering::Relaxed)
}
pub fn update_cached_balance(&self, balance: u64, fetched_at: u64) {
self.cached_balance.store(balance, Ordering::Relaxed);
self.balance_fetched_at.store(fetched_at, Ordering::Relaxed);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counters_start_at_zero() {
let m = StatusMetrics::new();
assert_eq!(m.verifications_relayed(), 0);
assert_eq!(m.attestations_issued(), 0);
}
#[test]
fn increment_verifications_counts_correctly() {
let m = StatusMetrics::new();
m.increment_verifications();
m.increment_verifications();
assert_eq!(m.verifications_relayed(), 2);
assert_eq!(m.attestations_issued(), 0);
}
#[test]
fn increment_attestations_counts_correctly() {
let m = StatusMetrics::new();
m.increment_attestations();
assert_eq!(m.attestations_issued(), 1);
assert_eq!(m.verifications_relayed(), 0);
}
#[test]
fn counters_are_independent() {
let m = StatusMetrics::new();
m.increment_verifications();
m.increment_verifications();
m.increment_attestations();
assert_eq!(m.verifications_relayed(), 2);
assert_eq!(m.attestations_issued(), 1);
}
#[test]
fn start_time_is_recent() {
let before = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let m = StatusMetrics::new();
let after = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
assert!(m.start_time() >= before);
assert!(m.start_time() <= after);
}
#[test]
fn balance_cache_starts_empty() {
let m = StatusMetrics::new();
assert_eq!(m.cached_balance(), 0);
assert_eq!(m.balance_fetched_at(), 0);
}
#[test]
fn balance_cache_updates_together() {
let m = StatusMetrics::new();
m.update_cached_balance(123, 456);
assert_eq!(m.cached_balance(), 123);
assert_eq!(m.balance_fetched_at(), 456);
}
}