mirror of https://github.com/oxen-io/lokinet
Initialize sqlite_orm and start interacting with it
parent
73c9ddff52
commit
8adb6295fc
@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <sqlite_orm/sqlite_orm.h>
|
||||
|
||||
#include <peerstats/types.hpp>
|
||||
|
||||
/// Contains some code to help deal with sqlite_orm in hopes of keeping other headers clean
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
inline auto
|
||||
initStorage(const std::string& file)
|
||||
{
|
||||
using namespace sqlite_orm;
|
||||
return make_storage(
|
||||
file,
|
||||
make_table(
|
||||
"peerstats",
|
||||
make_column("routerId", &PeerStats::routerIdHex, primary_key(), unique()),
|
||||
make_column("numConnectionAttempts", &PeerStats::numConnectionAttempts),
|
||||
make_column("numConnectionSuccesses", &PeerStats::numConnectionSuccesses),
|
||||
make_column("numConnectionRejections", &PeerStats::numConnectionRejections),
|
||||
make_column("numConnectionTimeouts", &PeerStats::numConnectionTimeouts),
|
||||
make_column("numPathBuilds", &PeerStats::numPathBuilds),
|
||||
make_column("numPacketsAttempted", &PeerStats::numPacketsAttempted),
|
||||
make_column("numPacketsSent", &PeerStats::numPacketsSent),
|
||||
make_column("numPacketsDropped", &PeerStats::numPacketsDropped),
|
||||
make_column("numPacketsResent", &PeerStats::numPacketsResent),
|
||||
make_column("numDistinctRCsReceived", &PeerStats::numDistinctRCsReceived),
|
||||
make_column("numLateRCs", &PeerStats::numLateRCs),
|
||||
make_column("peakBandwidthBytesPerSec", &PeerStats::peakBandwidthBytesPerSec),
|
||||
make_column("longestRCReceiveInterval", &PeerStats::longestRCReceiveIntervalMs),
|
||||
make_column("mostExpiredRC", &PeerStats::mostExpiredRCMs)));
|
||||
}
|
||||
|
||||
using PeerDbStorage = decltype(initStorage(""));
|
||||
|
||||
} // namespace llarp
|
@ -0,0 +1,56 @@
|
||||
#include <peerstats/types.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
PeerStats::PeerStats(const RouterID& routerId)
|
||||
{
|
||||
routerIdHex = routerId.ToHex();
|
||||
}
|
||||
|
||||
PeerStats&
|
||||
PeerStats::operator+=(const PeerStats& other)
|
||||
{
|
||||
numConnectionAttempts += other.numConnectionAttempts;
|
||||
numConnectionSuccesses += other.numConnectionSuccesses;
|
||||
numConnectionRejections += other.numConnectionRejections;
|
||||
numConnectionTimeouts += other.numConnectionTimeouts;
|
||||
|
||||
numPathBuilds += other.numPathBuilds;
|
||||
numPacketsAttempted += other.numPacketsAttempted;
|
||||
numPacketsSent += other.numPacketsSent;
|
||||
numPacketsDropped += other.numPacketsDropped;
|
||||
numPacketsResent += other.numPacketsResent;
|
||||
|
||||
numDistinctRCsReceived += other.numDistinctRCsReceived;
|
||||
numLateRCs += other.numLateRCs;
|
||||
|
||||
peakBandwidthBytesPerSec = std::max(peakBandwidthBytesPerSec, other.peakBandwidthBytesPerSec);
|
||||
longestRCReceiveIntervalMs =
|
||||
std::max(longestRCReceiveIntervalMs, other.longestRCReceiveIntervalMs);
|
||||
mostExpiredRCMs = std::max(mostExpiredRCMs, other.mostExpiredRCMs);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool
|
||||
PeerStats::operator==(const PeerStats& other)
|
||||
{
|
||||
return routerIdHex == other.routerIdHex and numConnectionAttempts == other.numConnectionAttempts
|
||||
and numConnectionSuccesses == other.numConnectionSuccesses
|
||||
and numConnectionRejections == other.numConnectionRejections
|
||||
and numConnectionTimeouts == other.numConnectionTimeouts
|
||||
|
||||
and numPathBuilds == other.numPathBuilds
|
||||
and numPacketsAttempted == other.numPacketsAttempted
|
||||
and numPacketsSent == other.numPacketsSent and numPacketsDropped == other.numPacketsDropped
|
||||
and numPacketsResent == other.numPacketsResent
|
||||
|
||||
and numDistinctRCsReceived == other.numDistinctRCsReceived
|
||||
and numLateRCs == other.numLateRCs
|
||||
|
||||
and peakBandwidthBytesPerSec == other.peakBandwidthBytesPerSec
|
||||
and longestRCReceiveIntervalMs == other.longestRCReceiveIntervalMs
|
||||
and mostExpiredRCMs == other.mostExpiredRCMs;
|
||||
}
|
||||
|
||||
}; // namespace llarp
|
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <router_id.hpp>
|
||||
#include <util/time.hpp>
|
||||
|
||||
/// Types stored in our peerstats database are declared here
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
// Struct containing stats we know about a peer
|
||||
struct PeerStats
|
||||
{
|
||||
std::string routerIdHex;
|
||||
|
||||
int32_t numConnectionAttempts = 0;
|
||||
int32_t numConnectionSuccesses = 0;
|
||||
int32_t numConnectionRejections = 0;
|
||||
int32_t numConnectionTimeouts = 0;
|
||||
|
||||
int32_t numPathBuilds = 0;
|
||||
int64_t numPacketsAttempted = 0;
|
||||
int64_t numPacketsSent = 0;
|
||||
int64_t numPacketsDropped = 0;
|
||||
int64_t numPacketsResent = 0;
|
||||
|
||||
int32_t numDistinctRCsReceived = 0;
|
||||
int32_t numLateRCs = 0;
|
||||
|
||||
double peakBandwidthBytesPerSec = 0;
|
||||
int64_t longestRCReceiveIntervalMs = 0;
|
||||
int64_t mostExpiredRCMs = 0;
|
||||
|
||||
PeerStats(const RouterID& routerId);
|
||||
|
||||
PeerStats&
|
||||
operator+=(const PeerStats& other);
|
||||
bool
|
||||
operator==(const PeerStats& other);
|
||||
};
|
||||
|
||||
} // namespace llarp
|
@ -1,46 +0,0 @@
|
||||
#include <numeric>
|
||||
#include <peerstats/peer_db.hpp>
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
TEST_CASE("Test PeerStats operator+=", "[PeerStats]")
|
||||
{
|
||||
// TODO: test all members
|
||||
llarp::PeerStats stats;
|
||||
stats.numConnectionAttempts = 1;
|
||||
stats.peakBandwidthBytesPerSec = 12;
|
||||
|
||||
llarp::PeerStats delta;
|
||||
delta.numConnectionAttempts = 2;
|
||||
delta.peakBandwidthBytesPerSec = 4;
|
||||
|
||||
stats += delta;
|
||||
|
||||
CHECK(stats.numConnectionAttempts == 3);
|
||||
CHECK(stats.peakBandwidthBytesPerSec == 12); // should take max(), not add
|
||||
}
|
||||
|
||||
TEST_CASE("Test PeerDb PeerStats memory storage", "[PeerDb]")
|
||||
{
|
||||
const llarp::PeerStats empty = {};
|
||||
const llarp::RouterID id = {};
|
||||
|
||||
llarp::PeerDb db;
|
||||
CHECK(db.getCurrentPeerStats(id) == empty);
|
||||
|
||||
llarp::PeerStats delta;
|
||||
delta.numConnectionAttempts = 4;
|
||||
delta.peakBandwidthBytesPerSec = 5;
|
||||
db.accumulatePeerStats(id, delta);
|
||||
CHECK(db.getCurrentPeerStats(id) == delta);
|
||||
|
||||
delta = {};
|
||||
delta.numConnectionAttempts = 5;
|
||||
delta.peakBandwidthBytesPerSec = 6;
|
||||
db.accumulatePeerStats(id, delta);
|
||||
|
||||
llarp::PeerStats expected;
|
||||
expected.numConnectionAttempts = 9;
|
||||
expected.peakBandwidthBytesPerSec = 6;
|
||||
CHECK(db.getCurrentPeerStats(id) == expected);
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
#include <numeric>
|
||||
#include <peerstats/peer_db.hpp>
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
TEST_CASE("Test PeerDb PeerStats memory storage", "[PeerDb]")
|
||||
{
|
||||
const llarp::RouterID id = {};
|
||||
const llarp::PeerStats empty(id);
|
||||
|
||||
llarp::PeerDb db;
|
||||
CHECK(db.getCurrentPeerStats(id).has_value() == false);
|
||||
|
||||
llarp::PeerStats delta(id);
|
||||
delta.numConnectionAttempts = 4;
|
||||
delta.peakBandwidthBytesPerSec = 5;
|
||||
db.accumulatePeerStats(id, delta);
|
||||
CHECK(db.getCurrentPeerStats(id).value() == delta);
|
||||
|
||||
delta = llarp::PeerStats(id);
|
||||
delta.numConnectionAttempts = 5;
|
||||
delta.peakBandwidthBytesPerSec = 6;
|
||||
db.accumulatePeerStats(id, delta);
|
||||
|
||||
llarp::PeerStats expected(id);
|
||||
expected.numConnectionAttempts = 9;
|
||||
expected.peakBandwidthBytesPerSec = 6;
|
||||
CHECK(db.getCurrentPeerStats(id).value() == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("Test PeerDb flush before load", "[PeerDb]")
|
||||
{
|
||||
llarp::PeerDb db;
|
||||
CHECK_THROWS_WITH(db.flushDatabase(), "Cannot flush database before it has been loaded");
|
||||
}
|
||||
|
||||
TEST_CASE("Test PeerDb load twice", "[PeerDb]")
|
||||
{
|
||||
llarp::PeerDb db;
|
||||
CHECK_NOTHROW(db.loadDatabase(std::nullopt));
|
||||
CHECK_THROWS_WITH(db.loadDatabase(std::nullopt), "Reloading database not supported");
|
||||
}
|
||||
|
||||
TEST_CASE("Test PeerDb nukes stats on load", "[PeerDb]")
|
||||
{
|
||||
const llarp::RouterID id = {};
|
||||
|
||||
llarp::PeerDb db;
|
||||
|
||||
llarp::PeerStats stats(id);
|
||||
stats.numConnectionAttempts = 1;
|
||||
|
||||
db.accumulatePeerStats(id, stats);
|
||||
CHECK(db.getCurrentPeerStats(id).value() == stats);
|
||||
|
||||
db.loadDatabase(std::nullopt);
|
||||
|
||||
CHECK(db.getCurrentPeerStats(id).has_value() == false);
|
||||
}
|
||||
|
||||
/*
|
||||
TEST_CASE("Test file-backed database", "[PeerDb]")
|
||||
{
|
||||
llarp::PeerDb db;
|
||||
db.loadDatabase(std::nullopt);
|
||||
|
||||
const llarp::RouterID id = {};
|
||||
llarp::PeerStats stats(id);
|
||||
stats.numConnectionAttempts = 42;
|
||||
|
||||
db.accumulatePeerStats(id, stats);
|
||||
|
||||
db.flushDatabase();
|
||||
}
|
||||
*/
|
@ -0,0 +1,23 @@
|
||||
#include <numeric>
|
||||
#include <peerstats/types.hpp>
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
TEST_CASE("Test PeerStats operator+=", "[PeerStats]")
|
||||
{
|
||||
llarp::RouterID id = {};
|
||||
|
||||
// TODO: test all members
|
||||
llarp::PeerStats stats(id);
|
||||
stats.numConnectionAttempts = 1;
|
||||
stats.peakBandwidthBytesPerSec = 12;
|
||||
|
||||
llarp::PeerStats delta(id);
|
||||
delta.numConnectionAttempts = 2;
|
||||
delta.peakBandwidthBytesPerSec = 4;
|
||||
|
||||
stats += delta;
|
||||
|
||||
CHECK(stats.numConnectionAttempts == 3);
|
||||
CHECK(stats.peakBandwidthBytesPerSec == 12); // should take max(), not add
|
||||
}
|
Loading…
Reference in New Issue