Add bencoding serialization to PeerStats

pull/1318/head
Stephen Shelton 4 years ago
parent baac6bf3bd
commit 9deee9e542
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -1,7 +1,29 @@
#include <peerstats/types.hpp>
#include <util/str.hpp>
#include <stdexcept>
namespace llarp
{
constexpr auto RouterIdKey = "routerId";
constexpr auto NumConnectionAttemptsKey = "numConnectionAttempts";
constexpr auto NumConnectionSuccessesKey = "numConnectionSuccesses";
constexpr auto NumConnectionRejectionsKey = "numConnectionRejections";
constexpr auto NumConnectionTimeoutsKey = "numConnectionTimeouts";
constexpr auto NumPathBuildsKey = "numPathBuilds";
constexpr auto NumPacketsAttemptedKey = "numPacketsAttempted";
constexpr auto NumPacketsSentKey = "numPacketsSent";
constexpr auto NumPacketsDroppedKey = "numPacketsDropped";
constexpr auto NumPacketsResentKey = "numPacketsResent";
constexpr auto NumDistinctRCsReceivedKey = "numDistinctRCsReceived";
constexpr auto NumLateRCsKey = "numLateRCs";
constexpr auto PeakBandwidthBytesPerSecKey = "peakBandwidthBytesPerSec";
constexpr auto LongestRCReceiveIntervalKey = "longestRCReceiveInterval";
constexpr auto LeastRCRemainingLifetimeKey = "leastRCRemainingLifetime";
constexpr auto LastRCUpdatedKey = "lastRCUpdated";
PeerStats::PeerStats() = default;
PeerStats::PeerStats(const RouterID& routerId_) : routerId(routerId_)
@ -59,23 +81,61 @@ namespace llarp
PeerStats::toJson() const
{
return {
{"routerId", routerId.ToString()},
// {"numConnectionAttempts", numConnectionAttempts},
// {"numConnectionSuccesses", numConnectionSuccesses},
// {"numConnectionRejections", numConnectionRejections},
// {"numConnectionTimeouts", numConnectionTimeouts},
// {"numPathBuilds", numPathBuilds},
// {"numPacketsAttempted", numPacketsAttempted},
// {"numPacketsSent", numPacketsSent},
// {"numPacketsDropped", numPacketsDropped},
// {"numPacketsResent", numPacketsResent},
{"numDistinctRCsReceived", numDistinctRCsReceived},
{"numLateRCs", numLateRCs},
// {"peakBandwidthBytesPerSec", peakBandwidthBytesPerSec},
{"longestRCReceiveInterval", longestRCReceiveInterval.count()},
{"leastRCRemainingLifetime", leastRCRemainingLifetime.count()},
{"lastRCUpdated", lastRCUpdated.count()},
{RouterIdKey, routerId.ToString()},
{NumConnectionAttemptsKey, numConnectionAttempts},
{NumConnectionSuccessesKey, numConnectionSuccesses},
{NumConnectionRejectionsKey, numConnectionRejections},
{NumConnectionTimeoutsKey, numConnectionTimeouts},
{NumPathBuildsKey, numPathBuilds},
{NumPacketsAttemptedKey, numPacketsAttempted},
{NumPacketsSentKey, numPacketsSent},
{NumPacketsDroppedKey, numPacketsDropped},
{NumPacketsResentKey, numPacketsResent},
{NumDistinctRCsReceivedKey, numDistinctRCsReceived},
{NumLateRCsKey, numLateRCs},
{PeakBandwidthBytesPerSecKey, peakBandwidthBytesPerSec},
{LongestRCReceiveIntervalKey, longestRCReceiveInterval.count()},
{LeastRCRemainingLifetimeKey, leastRCRemainingLifetime.count()},
{LastRCUpdatedKey, lastRCUpdated.count()},
};
}
void
PeerStats::BEncode(llarp_buffer_t* buf)
{
if (not buf)
throw std::runtime_error("PeerStats: Can't use null buf");
auto encodeUint64Entry = [&](std::string_view key, uint64_t value) {
if (not bencode_write_uint64_entry(buf, key.data(), key.size(), value))
throw std::runtime_error(stringify("PeerStats: Could not encode ", key));
};
if (not bencode_start_dict(buf))
throw std::runtime_error("PeerStats: Could not create bencode dict");
// TODO: we don't have bencode support for dict entries other than uint64...?
// encodeUint64Entry(RouterIdKey, routerId);
encodeUint64Entry(NumConnectionAttemptsKey, numConnectionAttempts);
encodeUint64Entry(NumConnectionSuccessesKey, numConnectionSuccesses);
encodeUint64Entry(NumConnectionRejectionsKey, numConnectionRejections);
encodeUint64Entry(NumConnectionTimeoutsKey, numConnectionTimeouts);
encodeUint64Entry(NumPathBuildsKey, numPathBuilds);
encodeUint64Entry(NumPacketsAttemptedKey, numPacketsAttempted);
encodeUint64Entry(NumPacketsSentKey, numPacketsSent);
encodeUint64Entry(NumPacketsDroppedKey, numPacketsDropped);
encodeUint64Entry(NumPacketsResentKey, numPacketsResent);
encodeUint64Entry(NumDistinctRCsReceivedKey, numDistinctRCsReceived);
encodeUint64Entry(NumLateRCsKey, numLateRCs);
encodeUint64Entry(PeakBandwidthBytesPerSecKey, (uint64_t)peakBandwidthBytesPerSec);
encodeUint64Entry(LongestRCReceiveIntervalKey, longestRCReceiveInterval.count());
encodeUint64Entry(LeastRCRemainingLifetimeKey, leastRCRemainingLifetime.count());
encodeUint64Entry(LastRCUpdatedKey, lastRCUpdated.count());
if (not bencode_end(buf))
throw std::runtime_error("PeerStats: Could not end bencode dict");
}
}; // namespace llarp

@ -48,6 +48,9 @@ namespace llarp
util::StatusObject
toJson() const;
void
BEncode(llarp_buffer_t* buf);
};
} // namespace llarp

@ -1,5 +1,6 @@
#include <numeric>
#include <peerstats/types.hpp>
#include <test_util.hpp>
#include <catch2/catch.hpp>
@ -21,3 +22,56 @@ TEST_CASE("Test PeerStats operator+=", "[PeerStats]")
CHECK(stats.numConnectionAttempts == 3);
CHECK(stats.peakBandwidthBytesPerSec == 12); // should take max(), not add
}
TEST_CASE("Test PeerStats BEncode", "[PeerStats]")
{
llarp::RouterID id = llarp::test::makeBuf<llarp::RouterID>(0x01);
llarp::PeerStats stats(id);
stats.numConnectionAttempts = 1;
stats.numConnectionSuccesses = 2;
stats.numConnectionRejections = 3;
stats.numConnectionTimeouts = 4;
stats.numPathBuilds = 5;
stats.numPacketsAttempted = 6;
stats.numPacketsSent = 7;
stats.numPacketsDropped = 8;
stats.numPacketsResent = 9;
stats.numDistinctRCsReceived = 10;
stats.numLateRCs = 11;
stats.peakBandwidthBytesPerSec = 12.1; // should truncate to 12
stats.longestRCReceiveInterval = 13ms;
stats.leastRCRemainingLifetime = 14ms;
stats.lastRCUpdated = 15ms;
constexpr int bufSize = 4096;
byte_t* raw = new byte_t[bufSize];
llarp_buffer_t buf(raw, bufSize);
CHECK_NOTHROW(stats.BEncode(&buf));
std::string asString = (const char*)raw;
constexpr std::string_view expected =
"d"
"21:numConnectionAttempts" "i1e"
"22:numConnectionSuccesses" "i2e"
"23:numConnectionRejections" "i3e"
"21:numConnectionTimeouts" "i4e"
"13:numPathBuilds" "i5e"
"19:numPacketsAttempted" "i6e"
"14:numPacketsSent" "i7e"
"17:numPacketsDropped" "i8e"
"16:numPacketsResent" "i9e"
"22:numDistinctRCsReceived" "i10e"
"10:numLateRCs" "i11e"
"24:peakBandwidthBytesPerSec" "i12e"
"24:longestRCReceiveInterval" "i13e"
"24:leastRCRemainingLifetime" "i14e"
"13:lastRCUpdated" "i15e"
"e";
CHECK(asString == expected);
delete [] raw;
}

Loading…
Cancel
Save