Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows req/res very slow compared to Linux #1777

Open
hconcessa opened this issue Feb 12, 2024 · 4 comments
Open

Windows req/res very slow compared to Linux #1777

hconcessa opened this issue Feb 12, 2024 · 4 comments
Labels

Comments

@hconcessa
Copy link

I'm using gtest to do some performance testing. On Linux I have less than 1 ms, but the same code on Windows has ~30ms.

I think it's a significant difference, but i don't undestand why

Client

// Create client
httplib::Client cli(ADDR, PORT);
ASSERT_TRUE(cli.is_valid());

httplib::Result res;

// Measure the get req/res time
auto start_time = std::chrono::high_resolution_clock::now();
res = cli.get(ROUTE_GET_TEST_TEXT);
auto end_time = std::chrono::high_resolution_clock::now();

auto elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();

// Verify response
ASSERT_TRUE(res);
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(httplib::StatusCode::OK_200, res->status);
EXPECT_EQ(httplib::Error::Success, res.error());
EXPECT_EQ(TXT_DEFAULT_RESPONSE, res->body);

// Verify elapsed time		
ASSERT_LE(elapsed_time, TIME_GET_US);

Server handle:

_svr.Get(ROUTE_GET_TEST_TEXT, [&](const httplib::Request& req, httplib::Response& res) {
	res.set_content(TXT_DEFAULT_RESPONSE, "text/plain");
	});

Common:

#define ADDR "127.0.0.1"
#define PORT 8181

#define TXT_DEFAULT_RESPONSE  "Default Response to txt request"

@rpmocchet
Copy link

I´ve made some unit tests based on your code and had similar results:

On Linux:
Linux

On Windows:
Windows

@yhirose
Copy link
Owner

yhirose commented Feb 26, 2024

@hconcessa @rpmocchet sorry for the late reply. I'll take a look at the behavior on my Windows machine when I have time. Thanks!

@q2a3z
Copy link

q2a3z commented Jun 19, 2024

Hey Guys.
I did some time investigation on the client side in Win64 PC.
I found socket_t create_socket will took 1ms to process.

So I suspected WSASocketW took so much time, and got it.
But even I forced it to ues the std.socket to create a socket. It also cost around 1ms.
Is any possiable when Win create a socket will take more time than Linux or BSD?
I don't have Linux or BSD eniviroment right now.
Can you test this patten for me? Thank you.

for (auto rp = result; rp; rp = rp->ai_next) {
    // Create a socket
#ifdef _WIN32
    auto sock =
        WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0,
                   WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED);
    /*...*/
    if (sock == INVALID_SOCKET) {
      sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    }
#else
    auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
#endif
    if (sock == INVALID_SOCKET) { continue; }

Repository owner deleted a comment from J-dev740 Sep 4, 2024
@yhirose yhirose added the windows Windows specific topic label Sep 28, 2024
@RCECoder
Copy link

RCECoder commented Nov 17, 2024

There is a significant delay when connecting to localhost. It tries first to connect to 0.0.0.0 returned from getaddrinfo which always fails. This needs some kind of filtering on connect and skip connecting to INADDR_ANY.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants