Skip to content

Commit 2d6b4c8

Browse files
committed
Fix benchmark test issue on Windows
1 parent 318a3fe commit 2d6b4c8

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

test/test.cc

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,46 @@ static void read_file(const std::string &path, std::string &out) {
7878
fs.read(&out[0], static_cast<std::streamsize>(size));
7979
}
8080

81+
void performance_test(const char *host) {
82+
auto port = 1234;
83+
84+
Server svr;
85+
86+
svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) {
87+
res.set_content("Benchmark Response", "text/plain");
88+
});
89+
90+
auto listen_thread = std::thread([&]() { svr.listen(host, port); });
91+
auto se = detail::scope_exit([&] {
92+
svr.stop();
93+
listen_thread.join();
94+
ASSERT_FALSE(svr.is_running());
95+
});
96+
97+
svr.wait_until_ready();
98+
99+
Client cli(host, port);
100+
101+
auto start = std::chrono::high_resolution_clock::now();
102+
103+
auto res = cli.Get("/benchmark");
104+
ASSERT_TRUE(res);
105+
EXPECT_EQ(StatusCode::OK_200, res->status);
106+
107+
auto end = std::chrono::high_resolution_clock::now();
108+
109+
auto elapsed =
110+
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
111+
.count();
112+
113+
EXPECT_LE(elapsed, 5) << "Performance is too slow: " << elapsed
114+
<< "ms (Issue #1777)";
115+
}
116+
117+
TEST(_BenchmarkTest, localhost) { performance_test("localhost"); }
118+
119+
TEST(_BenchmarkTest, v6) { performance_test("::1"); }
120+
81121
class UnixSocketTest : public ::testing::Test {
82122
protected:
83123
void TearDown() override { std::remove(pathname_.c_str()); }
@@ -3634,46 +3674,6 @@ TEST_F(ServerTest, GetMethod200) {
36343674
EXPECT_EQ("Hello World!", res->body);
36353675
}
36363676

3637-
void performance_test(const char *host) {
3638-
auto port = 1234;
3639-
3640-
Server svr;
3641-
3642-
svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) {
3643-
res.set_content("Benchmark Response", "text/plain");
3644-
});
3645-
3646-
auto listen_thread = std::thread([&]() { svr.listen(host, port); });
3647-
auto se = detail::scope_exit([&] {
3648-
svr.stop();
3649-
listen_thread.join();
3650-
ASSERT_FALSE(svr.is_running());
3651-
});
3652-
3653-
svr.wait_until_ready();
3654-
3655-
Client cli(host, port);
3656-
3657-
auto start = std::chrono::high_resolution_clock::now();
3658-
3659-
auto res = cli.Get("/benchmark");
3660-
ASSERT_TRUE(res);
3661-
EXPECT_EQ(StatusCode::OK_200, res->status);
3662-
3663-
auto end = std::chrono::high_resolution_clock::now();
3664-
3665-
auto elapsed =
3666-
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
3667-
.count();
3668-
3669-
EXPECT_LE(elapsed, 5) << "Performance is too slow: " << elapsed
3670-
<< "ms (Issue #1777)";
3671-
}
3672-
3673-
TEST(BenchmarkTest, localhost) { performance_test("localhost"); }
3674-
3675-
TEST(BenchmarkTest, v6) { performance_test("::1"); }
3676-
36773677
TEST_F(ServerTest, GetEmptyFile) {
36783678
auto res = cli_.Get("/empty_file");
36793679
ASSERT_TRUE(res);

0 commit comments

Comments
 (0)