@@ -78,6 +78,65 @@ 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+ // Warmup test to avoid framework initialization overhead affecting benchmark
118+ TEST (_0_WarmupTest, system_initialization) {
119+ Server svr;
120+ svr.Get (" /warmup" , [](const Request &, Response &res) {
121+ res.set_content (" OK" , " text/plain" );
122+ });
123+ auto listen_thread = std::thread ([&]() { svr.listen (" localhost" , 9999 ); });
124+ auto se = detail::scope_exit ([&] {
125+ svr.stop ();
126+ listen_thread.join ();
127+ });
128+ svr.wait_until_ready ();
129+
130+ Client cli (" localhost" , 9999 );
131+ auto res = cli.Get (" /warmup" );
132+ ASSERT_TRUE (res);
133+ EXPECT_EQ (StatusCode::OK_200, res->status );
134+ }
135+
136+ TEST (_BenchmarkTest, localhost) { performance_test (" localhost" ); }
137+
138+ TEST (_BenchmarkTest, v6) { performance_test (" ::1" ); }
139+
81140class UnixSocketTest : public ::testing::Test {
82141protected:
83142 void TearDown () override { std::remove (pathname_.c_str ()); }
@@ -3634,46 +3693,6 @@ TEST_F(ServerTest, GetMethod200) {
36343693 EXPECT_EQ (" Hello World!" , res->body );
36353694}
36363695
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-
36773696TEST_F (ServerTest, GetEmptyFile) {
36783697 auto res = cli_.Get (" /empty_file" );
36793698 ASSERT_TRUE (res);
0 commit comments