[ntcore] Minimize latency on localhost connections (#7997)

This commit is contained in:
Peter Johnson
2025-06-02 16:43:18 -07:00
committed by GitHub
parent 2af8c59858
commit f99692f287
9 changed files with 97 additions and 14 deletions

View File

@@ -30,6 +30,7 @@ void bench();
void bench2();
void stress();
void stress2();
void latency();
int main(int argc, char* argv[]) {
if (argc == 2 && std::string_view{argv[1]} == "bench") {
@@ -48,6 +49,10 @@ int main(int argc, char* argv[]) {
stress2();
return EXIT_SUCCESS;
}
if (argc == 2 && std::string_view{argv[1]} == "latency") {
latency();
return EXIT_SUCCESS;
}
auto myValue = nt::GetEntry(nt::GetDefaultInstance(), "MyValue");
@@ -325,3 +330,71 @@ void stress2() {
std::this_thread::sleep_for(10s);
fmt::print("isDone: {}", isDone.load());
}
void latency() {
// set up instances
auto client1 = nt::CreateInstance();
auto client2 = nt::CreateInstance();
auto server = nt::CreateInstance();
// connect client and server
nt::StartServer(server, "latency.json", "127.0.0.1", 10000);
nt::StartClient(client1, "client1");
nt::SetServer(client1, "127.0.0.1", 10000);
nt::StartClient(client2, "client2");
nt::SetServer(client2, "127.0.0.1", 10000);
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
// create publishers and subscribers
auto pub =
nt::Publish(nt::GetTopic(client1, "highrate"), NT_DOUBLE, "double");
nt::SubscribeMultiple(server, {{std::string_view{}}});
auto sub =
nt::Subscribe(nt::GetTopic(server, "highrate"), NT_DOUBLE, "double");
auto sub2 =
nt::Subscribe(nt::GetTopic(client2, "highrate"), NT_DOUBLE, "double");
std::this_thread::sleep_for(1s);
nt::SetDouble(pub, 0);
#if 0
// warm up
for (int i = 1; i <= 10000; ++i) {
nt::SetDouble(pub, i * 0.01);
if (i % 2000 == 0) {
std::this_thread::sleep_for(0.02s);
}
}
#endif
std::vector<int64_t> times;
times.reserve(1001);
// benchmark client to server
for (int i = 1; i <= 1000; ++i) {
int64_t sendTime = nt::Now();
nt::SetDouble(pub, i, sendTime);
nt::Flush(client1);
while (nt::GetDouble(sub, 0) != i) {
wpi::WaitForObject(sub);
}
times.emplace_back(nt::Now() - sendTime);
}
PrintTimes(times);
// benchmark client to client
times.resize(0);
for (int i = 2001; i <= 3000; ++i) {
int64_t sendTime = nt::Now();
nt::SetDouble(pub, i, sendTime);
nt::Flush(client1);
while (nt::GetDouble(sub2, 0) != i) {
wpi::WaitForObject(sub2);
}
times.emplace_back(nt::Now() - sendTime);
}
PrintTimes(times);
}