stats naive

This commit is contained in:
NikVolf 2016-12-21 15:02:50 +03:00
parent c060612a65
commit fbdcdaefbb
3 changed files with 82 additions and 0 deletions

View File

@ -6,6 +6,7 @@ mod connection;
mod connection_counter;
mod connections;
mod peer_context;
mod stats;
pub use self::accept_connection::{AcceptConnection, accept_connection};
pub use self::channel::Channel;
@ -15,3 +16,4 @@ pub use self::connection::Connection;
pub use self::connection_counter::ConnectionCounter;
pub use self::connections::Connections;
pub use self::peer_context::PeerContext;
pub use self::stats::PeerStats;

View File

@ -4,12 +4,14 @@ use message::{Payload, Message};
use p2p::Context;
use util::{PeerInfo, ConfigurableSynchronizer, ResponseQueue, Synchronizer, Responses};
use futures::{lazy, finished};
use net::PeerStats;
pub struct PeerContext {
context: Arc<Context>,
info: PeerInfo,
synchronizer: Mutex<ConfigurableSynchronizer>,
response_queue: Mutex<ResponseQueue>,
stats: Mutex<PeerStats>,
}
impl PeerContext {
@ -19,6 +21,7 @@ impl PeerContext {
info: info,
synchronizer: Mutex::new(ConfigurableSynchronizer::new(synchronous)),
response_queue: Mutex::default(),
stats: Mutex::default(),
}
}
@ -123,4 +126,8 @@ impl PeerContext {
pub fn global(&self) -> &Arc<Context> {
&self.context
}
pub fn stats(&self) -> &Mutex<PeerStats> {
&self.stats
}
}

73
p2p/src/net/stats.rs Normal file
View File

@ -0,0 +1,73 @@
#[derive(Default, Clone)]
pub struct MessageStats {
pub addr: u64,
pub getdata: u64,
pub getheaders: u64,
pub headers: u64,
pub reject: u64,
pub tx: u64,
pub inv: u64,
pub ping: u64,
pub pong: u64,
pub verack: u64,
pub version: u64,
}
#[derive(Default, Clone)]
pub struct PeerStats {
pub last_send: u32,
pub last_recv: u32,
pub total_send: u64,
pub total_recv: u64,
pub avg_ping: f64,
pub min_ping: f64,
pub synced_blocks: u32,
pub synced_headers: u32,
pub counter_send: MessageStats,
pub counter_recv: MessageStats,
pub bytes_send: MessageStats,
pub bytes_recv: MessageStats,
}
macro_rules! impl_send {
($msg: ident, $method_name: ident) => {
pub fn $method_name(&mut self, bytes: usize) {
self.last_send = ::time::get_time().sec as u32;
self.total_send += bytes as u64;
self.bytes_send.$msg += bytes as u64;
self.counter_send.$msg += bytes as u64;
}
}
}
macro_rules! impl_recv {
($msg: ident, $method_name: ident) => {
pub fn $method_name(&mut self, bytes: usize) {
self.last_recv = ::time::get_time().sec as u32;
self.total_recv += bytes as u64;
self.bytes_recv.$msg += bytes as u64;
self.counter_recv.$msg += bytes as u64;
}
}
}
macro_rules! impl_both {
($msg: ident, $method_send: ident, $method_recv: ident) => {
impl_send!($msg, $method_send);
impl_recv!($msg, $method_recv);
}
}
impl PeerStats {
impl_both!(addr, send_addr, recv_addr);
impl_both!(getdata, send_getdata, recv_getdata);
impl_both!(getheaders, send_getheaders, recv_getheaders);
impl_both!(headers, send_headers, recv_headers);
impl_both!(reject, send_reject, recv_reject);
impl_both!(tx, send_tx, recv_tx);
impl_both!(inv, send_inv, recv_inv);
impl_both!(ping, send_ping, recv_ping);
impl_both!(pong, send_pong, recv_pong);
impl_both!(verack, send_verack, recv_verack);
impl_both!(version, send_version, recv_version);
}