Max election age precision (#4549)

* Improved syntax

* Use two decimals for max_election_age

* Re-write the unit test to tidy it up

* Use milliseconds instead of floating point seconds

---------

Co-authored-by: Dimitrios Siganos <dimitris@siganos.org>
This commit is contained in:
RickiNano 2024-04-11 10:43:27 +02:00 committed by GitHub
commit 52af411d9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 28 deletions

View file

@ -2019,10 +2019,7 @@ void nano::json_handler::election_statistics ()
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;
}
oldest_election_start = std::min (oldest_election_start, election->get_election_start ());
switch (election->behavior ())
{
@ -2039,12 +2036,11 @@ void nano::json_handler::election_statistics ()
}
auto utilization_percentage = (static_cast<double> (total_count * 100) / node.config.active_elections_size);
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;
auto max_election_age = std::chrono::duration_cast<std::chrono::milliseconds> (now - oldest_election_start).count ();
auto average_election_age = total_count ? std::chrono::duration_cast<std::chrono::milliseconds> (total_age).count () / total_count : 0;
std::stringstream stream_utilization, stream_average_age;
std::stringstream stream_utilization;
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);
@ -2052,7 +2048,7 @@ void nano::json_handler::election_statistics ()
response_l.put ("total", total_count);
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_l.put ("average_election_age", average_election_age);
response_errors ();
}

View file

@ -6870,14 +6870,10 @@ TEST (rpc, confirmation_info)
TEST (rpc, election_statistics)
{
nano::test::system 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 node1 = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node1);
// process block and wait for election to start, the election will not be completed because there are no voters on the network
nano::block_builder builder;
auto send1 = builder
.send ()
@ -6888,20 +6884,22 @@ TEST (rpc, election_statistics)
.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 ());
ASSERT_TIMELY_EQ (5s, node1->active.size (), 1);
// delay to ensure returned age is not rounded down to zero
system.delay_ms (20ms);
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"));
}
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_GT (response.get<int> ("max_election_age"), 0);
ASSERT_GT (response.get<int> ("average_election_age"), 0);
ASSERT_LT (response.get<int> ("max_election_age"), 5000);
ASSERT_LT (response.get<int> ("average_election_age"), 5000);
}