Speedup socket unit test (#1444)

Was testing a setup that wasn't used in production, it would
unnecessarily thrash small buffers.
This commit is contained in:
Gareth Andrew Lloyd 2023-11-01 17:24:24 +00:00 committed by GitHub
parent af4fdef029
commit 157b36162b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 19 deletions

View File

@ -216,7 +216,7 @@ bool Socket::Write(const uint8_t *data, size_t len, bool have_more) {
return true;
}
bool Socket::Write(const std::string &s, bool have_more) {
bool Socket::Write(std::string_view s, bool have_more) {
return Write(reinterpret_cast<const uint8_t *>(s.data()), s.size(), have_more);
}

View File

@ -1,4 +1,4 @@
// Copyright 2022 Memgraph Ltd.
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -149,7 +149,7 @@ class Socket {
* false if write failed
*/
bool Write(const uint8_t *data, size_t len, bool have_more = false);
bool Write(const std::string &s, bool have_more = false);
bool Write(std::string_view s, bool have_more = false);
/**
* Read data from the socket.

View File

@ -1,4 +1,4 @@
// Copyright 2022 Memgraph Ltd.
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -53,16 +53,13 @@ TEST(Socket, WaitForReadyWrite) {
ASSERT_TRUE(server.Bind({"127.0.0.1", 0}));
ASSERT_TRUE(server.Listen(1024));
std::thread thread([&server] {
std::jthread thread([&server] {
uint8_t buff[10000];
memgraph::io::network::Socket client;
ASSERT_TRUE(client.Connect(server.endpoint()));
client.SetNonBlocking();
// Decrease the TCP read buffer.
int len = 1024;
ASSERT_EQ(setsockopt(client.fd(), SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)), 0);
// Wait for server to fill its buffer and hence would WaitForReadyWrite
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
while (true) {
int ret = client.Read(buff, sizeof(buff));
@ -70,26 +67,26 @@ TEST(Socket, WaitForReadyWrite) {
std::raise(SIGPIPE);
} else if (ret == 0) {
break;
} else if (ret == -1) {
client.WaitForReadyRead(); // reduce CPU load
}
}
});
auto client = server.Accept();
ASSERT_TRUE(client);
auto connection_with_client = server.Accept();
ASSERT_TRUE(connection_with_client);
client->SetNonBlocking();
connection_with_client->SetNonBlocking();
// Decrease the TCP write buffer.
int len = 1024;
ASSERT_EQ(setsockopt(client->fd(), SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)), 0);
ASSERT_EQ(setsockopt(connection_with_client->fd(), SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)), 0);
auto const payload = std::string_view{"test"};
memgraph::utils::Timer timer;
for (int i = 0; i < 1000000; ++i) {
ASSERT_TRUE(client->Write("test"));
for (int i = 0; i < 1'000'000; ++i) {
ASSERT_TRUE(connection_with_client->Write(payload));
}
ASSERT_GT(timer.Elapsed().count(), 1.0);
client->Close();
thread.join();
connection_with_client->Close();
}