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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2020 Netwarps Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::metrics::metricmap::MetricMap;
use crate::ProtocolId;
use libp2prs_core::PeerId;
use std::collections::hash_map::IntoIter;
use std::fmt;
use std::ops::Add;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::atomic::{AtomicUsize, Ordering};

pub struct Metric {
    /// The accumulative counter of packets sent.
    pkt_sent: AtomicUsize,
    /// The accumulative counter of packets received.
    pkt_recv: AtomicUsize,
    /// The accumulative counter of bytes sent.
    byte_sent: AtomicUsize,
    /// The accumulative counter of bytes received.
    byte_recv: AtomicUsize,

    /// A hashmap that key is protocol name and value is a counter of bytes received.
    protocol_in: MetricMap<String, usize>,
    /// A hashmap that key is protocol name and value is a counter of bytes sent.
    protocol_out: MetricMap<String, usize>,

    /// A hashmap that key is peer_id and value is a counter of bytes received.
    peer_in: MetricMap<PeerId, usize>,
    /// A hashmap that key is peer_id and value is a counter of bytes sent.
    peer_out: MetricMap<PeerId, usize>,
}

impl fmt::Debug for Metric {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Metric")
            .field("pkt_sent", &self.pkt_sent)
            .field("pkt_recv", &self.pkt_recv)
            .field("byte_sent", &self.byte_sent)
            .field("byte_recv", &self.byte_recv)
            .field("protocol_in", &self.protocol_in)
            .field("protocol_out", &self.protocol_out)
            .field("peer_in", &self.peer_in)
            .field("peer_out", &self.peer_out)
            .finish()
    }
}

impl Default for Metric {
    fn default() -> Self {
        Self::new()
    }
}

impl Metric {
    /// Create a new metric
    pub fn new() -> Metric {
        Metric {
            pkt_sent: AtomicUsize::new(0),
            pkt_recv: AtomicUsize::new(0),
            byte_sent: AtomicUsize::new(0),
            byte_recv: AtomicUsize::new(0),
            protocol_in: MetricMap::new(),
            protocol_out: MetricMap::new(),
            peer_in: MetricMap::new(),
            peer_out: MetricMap::new(),
        }
    }

    #[inline]
    pub(crate) fn log_recv_msg(&self, n: usize) {
        self.pkt_recv.fetch_add(1, Ordering::SeqCst);
        self.byte_recv.fetch_add(n, Ordering::SeqCst);
    }

    #[inline]
    pub(crate) fn log_sent_msg(&self, n: usize) {
        self.pkt_sent.fetch_add(1, Ordering::SeqCst);
        self.byte_sent.fetch_add(n, Ordering::SeqCst);
    }

    #[inline]
    pub(crate) fn log_sent_stream(&self, protocol: &ProtocolId, count: usize, peer_id: &PeerId) {
        self.protocol_out
            .store_or_modify(&protocol.to_string(), count, |_, value| value.add(count));
        self.peer_out.store_or_modify(peer_id, count, |_, value| value.add(count));
    }

    #[inline]
    pub(crate) fn log_recv_stream(&self, protocol: &ProtocolId, count: usize, peer_id: &PeerId) {
        self.protocol_in
            .store_or_modify(&protocol.to_string(), count, |_, value| value.add(count));
        self.peer_in.store_or_modify(peer_id, count, |_, value| value.add(count));
    }

    /// Get count & bytes about received package
    pub fn get_recv_count_and_size(&self) -> (usize, usize) {
        (self.pkt_recv.load(SeqCst), self.byte_recv.load(SeqCst))
    }

    /// Get count & bytes about sent package
    pub fn get_sent_count_and_size(&self) -> (usize, usize) {
        (self.pkt_sent.load(SeqCst), self.byte_sent.load(SeqCst))
    }

    /// Get in&out bytes by protocol_id
    pub fn get_protocol_in_and_out(&self, protocol_id: &str) -> (Option<usize>, Option<usize>) {
        let protocol_in = self.protocol_in.load(&protocol_id.to_string());
        let protocol_out = self.protocol_out.load(&protocol_id.to_string());
        (protocol_in, protocol_out)
    }

    /// Get in&out bytes by peer_id
    pub fn get_peer_in_and_out(&self, peer_id: &PeerId) -> (Option<usize>, Option<usize>) {
        let peer_in = self.peer_in.load(peer_id);
        let peer_out = self.peer_out.load(peer_id);
        (peer_in, peer_out)
    }

    /// Get an iterator that key is peer_id and value is input bytes.
    pub fn get_peers_in_list(&self) -> IntoIter<PeerId, usize> {
        self.peer_in.iterator().unwrap()
    }

    /// Get an iterator that key is peer_id and value is output bytes.
    pub fn get_peers_out_list(&self) -> IntoIter<PeerId, usize> {
        self.peer_out.iterator().unwrap()
    }

    /// Get an iterator that key is protocol and value is input bytes.
    pub fn get_protocols_in_list(&self) -> IntoIter<String, usize> {
        self.protocol_in.iterator().unwrap()
    }

    /// Get an iterator that key is protocol and value is output bytes.
    pub fn get_protocols_out_list(&self) -> IntoIter<String, usize> {
        self.protocol_out.iterator().unwrap()
    }
}

#[cfg(test)]
mod tests {
    use crate::metrics::metric::Metric;
    use crate::ProtocolId;
    use async_std::task;
    use libp2prs_core::PeerId;
    use std::sync::Arc;

    fn generate_metrics() -> Metric {
        Metric::new()
    }

    #[test]
    fn test_sent_package_and_byte() {
        let metric = Arc::new(generate_metrics());

        task::block_on(async {
            let mut t = Vec::new();
            for index in 0..16 {
                let m = metric.clone();
                t.push(task::spawn(async move {
                    m.log_sent_msg(index);
                }));
            }
            for item in t {
                item.await;
            }
        });

        assert_eq!(metric.get_sent_count_and_size(), (16, 120));
    }

    #[test]
    fn test_recv_package_and_byte() {
        let metric = Arc::new(generate_metrics());

        task::block_on(async {
            let mut t = Vec::new();
            for index in 0..16 {
                let m = metric.clone();
                t.push(task::spawn(async move {
                    m.log_recv_msg(index);
                }));
            }
            for item in t {
                item.await;
            }
        });

        assert_eq!(metric.get_recv_count_and_size(), (16, 120));
    }

    #[test]
    fn test_protocol_and_peer() {
        // env_logger::builder().filter_level(LevelFilter::Info).init();
        let metric = Arc::new(generate_metrics());

        let peer_id = PeerId::random();
        let proto_id: &[u8] = b"/test/1.0.0";
        let protocol: ProtocolId = proto_id.into();
        task::block_on(async {
            let mut t = Vec::new();
            for i in 0..16 {
                let m = metric.clone();
                let pid = peer_id.clone();
                let protocol = protocol.clone();
                t.push(task::spawn(async move {
                    m.log_sent_stream(&protocol, i, &pid);
                    m.log_recv_stream(&protocol, i, &pid);
                }));
            }

            for item in t {
                item.await;
            }
        });

        assert_eq!(metric.get_peer_in_and_out(&peer_id), (Some(120), Some(120)));
        assert_eq!(metric.get_protocol_in_and_out(&protocol.to_string()), (Some(120), Some(120)));
    }
}