@@ -78,6 +78,47 @@ 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+ auto res = cli.Get (" /benchmark" );
103+ auto end = std::chrono::high_resolution_clock::now ();
104+
105+ auto elapsed =
106+ std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
107+ .count ();
108+
109+ // Assertions after timing measurement to avoid overhead
110+ ASSERT_TRUE (res);
111+ EXPECT_EQ (StatusCode::OK_200, res->status );
112+
113+ // Localhost HTTP GET should be fast even in CI environments
114+ EXPECT_LE (elapsed, 5 ) << " Performance is too slow: " << elapsed
115+ << " ms (Issue #1777)" ;
116+ }
117+
118+ TEST (BenchmarkTest, localhost) { performance_test (" localhost" ); }
119+
120+ TEST (BenchmarkTest, v6) { performance_test (" ::1" ); }
121+
81122class UnixSocketTest : public ::testing::Test {
82123protected:
83124 void TearDown () override { std::remove (pathname_.c_str ()); }
@@ -3634,46 +3675,6 @@ TEST_F(ServerTest, GetMethod200) {
36343675 EXPECT_EQ (" Hello World!" , res->body );
36353676}
36363677
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-
36773678TEST_F (ServerTest, GetEmptyFile) {
36783679 auto res = cli_.Get (" /empty_file" );
36793680 ASSERT_TRUE (res);
0 commit comments