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
use crate::{
clear::Clear,
metric::Metric,
time_source::{Instant, StdInstant},
};
use aspect::{Advice, Enter, OnResult};
use serde::{Serialize, Serializer};
use std::ops::Deref;
mod atomic_tps;
mod tx_per_sec;
pub use atomic_tps::AtomicTxPerSec;
pub use tx_per_sec::TxPerSec;
#[derive(Clone)]
pub struct Throughput<T: Instant = StdInstant, P: RecordThroughput = AtomicTxPerSec<T>>(
pub P,
std::marker::PhantomData<T>,
);
pub trait RecordThroughput: Default {
fn on_result(&self);
}
impl<P: RecordThroughput, T: Instant> Default for Throughput<T, P> {
fn default() -> Self {
Throughput(P::default(), std::marker::PhantomData)
}
}
impl<P: RecordThroughput + Serialize + Clear, T: Instant, R> Metric<R> for Throughput<T, P> {}
impl<P: RecordThroughput, T: Instant> Enter for Throughput<T, P> {
type E = ();
fn enter(&self) {}
}
impl<P: RecordThroughput + Clear, T: Instant> Clear for Throughput<T, P> {
fn clear(&self) {
self.0.clear();
}
}
impl<P: RecordThroughput + Serialize, T: Instant, R> OnResult<R> for Throughput<T, P> {
fn leave_scope(&self, _enter: ()) -> Advice {
self.0.on_result();
Advice::Return
}
}
impl<P: RecordThroughput + Serialize, T: Instant> Serialize for Throughput<T, P> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Serialize::serialize(&self.0, serializer)
}
}
use std::{fmt, fmt::Debug};
impl<P: RecordThroughput + Debug, T: Instant> Debug for Throughput<T, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", &self.0)
}
}
impl<P: RecordThroughput, T: Instant> Deref for Throughput<T, P> {
type Target = P;
fn deref(&self) -> &Self::Target {
&self.0
}
}