Skip to content

Commit

Permalink
Updated bytes counter, libp2p is dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ErakhtinB committed Sep 23, 2024
1 parent 5c8495a commit 0f73046
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ hunter_config(

hunter_config(
libp2p
URL https://github.com/ErakhtinB/cpp-libp2p/archive/e10b86d6ffe726f39ca6e9a1dfc5826e9249d7e7.tar.gz
SHA1 84e1a72a13ab83baff8afe4c2f327a412563763b
URL https://github.com/ErakhtinB/cpp-libp2p/archive/8a9e6bb5e4beb1300526e19eeeecbc14b300b2dc.tar.gz
SHA1 7d64b598d3fe0ad80e192c93e24c7cb3e0ba6478
)

hunter_config(
Expand Down
33 changes: 19 additions & 14 deletions core/telemetry/impl/service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace rapidjson {
#include <libp2p/basic/scheduler/asio_scheduler_backend.hpp>
#include <libp2p/basic/scheduler/scheduler_impl.hpp>
#include <libp2p/multi/multiaddress.hpp>
#include <libp2p/transport/tcp/tcp_connection.hpp>
#include <libp2p/transport/tcp/bytes_counter.hpp>

#include <network/helpers/stream_read_buffer.hpp>
#include "common/uri.hpp"
Expand Down Expand Up @@ -455,7 +455,7 @@ namespace kagome::telemetry {
rapidjson::Value payload(rapidjson::kObjectType);

rapidjson::Value peers_count;
peers_count.SetInt(peer_manager_->activePeersNumber());
peers_count.SetUint(peer_manager_->activePeersNumber());

auto bandwidth = getBandwidth();
rapidjson::Value upBandwidth, downBandwidth;
Expand Down Expand Up @@ -488,18 +488,20 @@ namespace kagome::telemetry {
}

TelemetryServiceImpl::Bandwidth TelemetryServiceImpl::getBandwidth() {
if (not previous_bandwidth_calculated_.has_value()) {
previous_bandwidth_calculated_ = std::chrono::high_resolution_clock::now();
if (not previous_bandwidth_calculated_) {
previous_bandwidth_calculated_ =
std::chrono::high_resolution_clock::now();
}

auto calculateBandwidth = [](uint64_t &previousBytes,
uint64_t totalBytes,
auto &bandwidth,
const std::chrono::seconds &timeElapsed) {
if (timeElapsed.count() > 0) {
bandwidth = (totalBytes - previousBytes) / timeElapsed.count();
const auto bytesDiff = totalBytes - previousBytes;
if (const auto secondsElapsed = timeElapsed.count(); secondsElapsed > 0) {
bandwidth = bytesDiff / secondsElapsed;
} else {
bandwidth = 0;
bandwidth = bytesDiff;
}
previousBytes = totalBytes;
};
Expand All @@ -509,13 +511,16 @@ namespace kagome::telemetry {
currentTime - *previous_bandwidth_calculated_);

Bandwidth bandwidth;
const auto totalBytesRead = libp2p::transport::TcpConnection::getBytesRead();
calculateBandwidth(
previous_bytes_read_, totalBytesRead, bandwidth.down, timeElapsed);

const auto totalBytesWritten = libp2p::transport::TcpConnection::getBytesWritten();
calculateBandwidth(
previous_bytes_written_, totalBytesWritten, bandwidth.up, timeElapsed);
const auto &bytesCounter = libp2p::transport::ByteCounter::getInstance();
calculateBandwidth(previous_bytes_read_,
bytesCounter.getBytesRead(),
bandwidth.down,
timeElapsed);

calculateBandwidth(previous_bytes_written_,
bytesCounter.getBytesWritten(),
bandwidth.up,
timeElapsed);

previous_bandwidth_calculated_ = currentTime;

Expand Down

0 comments on commit 0f73046

Please sign in to comment.