Election age statistics (#4537)

* Add election age information to statistics
This commit is contained in:
RickiNano 2024-04-09 13:13:52 +02:00 committed by GitHub
commit 1fb3e9f535
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 12 deletions

View file

@ -118,6 +118,10 @@ public: // Interface
nano::vote_info get_last_vote (nano::account const & account);
void set_last_vote (nano::account const & account, nano::vote_info vote_info);
nano::election_status get_status () const;
std::chrono::steady_clock::time_point get_election_start () const
{
return election_start;
}
private: // Dependencies
nano::node & node;

View file

@ -2009,10 +2009,21 @@ void nano::json_handler::election_statistics ()
unsigned hinted_count = 0;
unsigned optimistic_count = 0;
unsigned total_count = 0;
std::chrono::steady_clock::duration total_age{};
auto now = std::chrono::steady_clock::now ();
std::chrono::steady_clock::time_point oldest_election_start = now;
for (auto const & election : active_elections)
{
total_count++;
auto election_start = election->get_election_start ();
auto age = now - election_start;
total_age += age;
if (election_start < oldest_election_start)
{
oldest_election_start = election_start;
}
switch (election->behavior ())
{
case election_behavior::normal:
@ -2028,14 +2039,20 @@ void nano::json_handler::election_statistics ()
}
auto utilization_percentage = (static_cast<double> (total_count * 100) / node.config.active_elections_size);
std::stringstream stream;
stream << std::fixed << std::setprecision (2) << utilization_percentage;
auto max_election_age = std::chrono::duration_cast<std::chrono::seconds> (now - oldest_election_start).count ();
double average_election_age = total_count ? std::chrono::duration<double> (total_age).count () / total_count : 0;
std::stringstream stream_utilization, stream_average_age;
stream_utilization << std::fixed << std::setprecision (2) << utilization_percentage;
stream_average_age << std::fixed << std::setprecision (2) << average_election_age;
response_l.put ("normal", normal_count);
response_l.put ("hinted", hinted_count);
response_l.put ("optimistic", optimistic_count);
response_l.put ("total", total_count);
response_l.put ("aec_utilization_percentage", stream.str ());
response_l.put ("aec_utilization_percentage", stream_utilization.str ());
response_l.put ("max_election_age", max_election_age);
response_l.put ("average_election_age", stream_average_age.str ());
response_errors ();
}

View file

@ -6870,14 +6870,38 @@ TEST (rpc, confirmation_info)
TEST (rpc, election_statistics)
{
nano::test::system system;
auto node1 = add_ipc_enabled_node (system);
nano::node_config node_config;
node_config.ipc_config.transport_tcp.enabled = true;
node_config.ipc_config.transport_tcp.port = system.get_available_port ();
nano::node_flags node_flags;
node_flags.disable_request_loop = true;
auto node1 (system.add_node (node_config, node_flags));
auto const rpc_ctx = add_rpc (system, node1);
boost::property_tree::ptree request1;
request1.put ("action", "election_statistics");
auto response1 (wait_response (system, rpc_ctx, request1));
ASSERT_EQ ("0", response1.get<std::string> ("normal"));
ASSERT_EQ ("0", response1.get<std::string> ("hinted"));
ASSERT_EQ ("0", response1.get<std::string> ("optimistic"));
ASSERT_EQ ("0", response1.get<std::string> ("total"));
ASSERT_EQ ("0.00", response1.get<std::string> ("aec_utilization_percentage"));
nano::block_builder builder;
auto send1 = builder
.send ()
.previous (nano::dev::genesis->hash ())
.destination (nano::public_key ())
.balance (nano::dev::constants.genesis_amount - 100)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (nano::dev::genesis->hash ()))
.build ();
node1->process_active (send1);
ASSERT_TIMELY (5s, !node1->active.empty ());
ASSERT_TRUE (nano::test::start_elections (system, *node1, { send1 }));
ASSERT_EQ (1, node1->active.size ());
boost::property_tree::ptree request;
request.put ("action", "election_statistics");
{
auto response (wait_response (system, rpc_ctx, request));
ASSERT_EQ ("1", response.get<std::string> ("normal"));
ASSERT_EQ ("0", response.get<std::string> ("hinted"));
ASSERT_EQ ("0", response.get<std::string> ("optimistic"));
ASSERT_EQ ("1", response.get<std::string> ("total"));
ASSERT_NE ("0.00", response.get<std::string> ("aec_utilization_percentage"));
ASSERT_NO_THROW (response.get<std::string> ("max_election_age"));
ASSERT_NO_THROW (response.get<std::string> ("average_election_age"));
}
}