From 887bc2bc2c6845e30dc708848efd47981bce89e8 Mon Sep 17 00:00:00 2001 From: cryptocode <34946442+cryptocode@users.noreply.github.com> Date: Thu, 8 Nov 2018 20:38:06 +0100 Subject: [PATCH] Bootstrap traffic stats (#1330) --- rai/node/bootstrap.cpp | 2 ++ rai/node/stats.cpp | 3 +++ rai/node/stats.hpp | 1 + rai/qt/qt.cpp | 8 +++++--- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/rai/node/bootstrap.cpp b/rai/node/bootstrap.cpp index c834987b..b6d6316c 100644 --- a/rai/node/bootstrap.cpp +++ b/rai/node/bootstrap.cpp @@ -37,6 +37,7 @@ void rai::socket::async_read (std::shared_ptr> buffer_a, si auto this_l (shared_from_this ()); start (); boost::asio::async_read (socket_m, boost::asio::buffer (buffer_a->data (), size_a), [this_l, callback_a](boost::system::error_code const & ec, size_t size_a) { + this_l->node->stats.add (rai::stat::type::traffic_bootstrap, rai::stat::dir::in, size_a); this_l->stop (); callback_a (ec, size_a); }); @@ -47,6 +48,7 @@ void rai::socket::async_write (std::shared_ptr> buffer_a, s auto this_l (shared_from_this ()); start (); boost::asio::async_write (socket_m, boost::asio::buffer (buffer_a->data (), buffer_a->size ()), [this_l, callback_a, buffer_a](boost::system::error_code const & ec, size_t size_a) { + this_l->node->stats.add (rai::stat::type::traffic_bootstrap, rai::stat::dir::out, size_a); this_l->stop (); callback_a (ec, size_a); }); diff --git a/rai/node/stats.cpp b/rai/node/stats.cpp index ec35d010..5cdc4bb5 100644 --- a/rai/node/stats.cpp +++ b/rai/node/stats.cpp @@ -342,6 +342,9 @@ std::string rai::stat::type_to_string (uint32_t key) case rai::stat::type::traffic: res = "traffic"; break; + case rai::stat::type::traffic_bootstrap: + res = "traffic_bootstrap"; + break; case rai::stat::type::vote: res = "vote"; break; diff --git a/rai/node/stats.hpp b/rai/node/stats.hpp index aea372b1..7d99f8da 100644 --- a/rai/node/stats.hpp +++ b/rai/node/stats.hpp @@ -176,6 +176,7 @@ public: enum class type : uint8_t { traffic, + traffic_bootstrap, error, message, block, diff --git a/rai/qt/qt.cpp b/rai/qt/qt.cpp index f326ea77..53bc2d9c 100644 --- a/rai/qt/qt.cpp +++ b/rai/qt/qt.cpp @@ -809,16 +809,18 @@ void rai_qt::stats_viewer::refresh_stats () detail = "total"; } - if (type == "traffic") + if (type == "traffic" || type == "traffic_bootstrap") { const std::vector units = { " bytes", " KB", " MB", " GB", " TB", " PB" }; double bytes = std::stod (value); - auto index = std::min (units.size () - 1, static_cast (std::floor (std::log2 (bytes) / 10))); + auto index = bytes == 0 ? 0 : std::min (units.size () - 1, static_cast (std::floor (std::log2 (bytes) / 10))); std::string unit = units[index]; bytes /= std::pow (1024, index); + // Only show decimals from MB and up + int precision = index < 2 ? 0 : 2; std::stringstream numstream; - numstream << std::fixed << std::setprecision (2) << bytes; + numstream << std::fixed << std::setprecision (precision) << bytes; value = numstream.str () + unit; }