From 8bb0776b84fe1fffc194aee0ab0b4359ed99c2cf Mon Sep 17 00:00:00 2001 From: tejas Date: Sat, 23 Aug 2025 18:57:03 +0530 Subject: [PATCH 1/4] Initialize start time for server By initializing start_time_ for server, I hope to measure the time taken to process a request at the end maybe in the set_logger callback and print it. I only see current usage in client with server retaining the inital min value --- httplib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/httplib.h b/httplib.h index 25e1b2f1ab..e1ace8aeae 100644 --- a/httplib.h +++ b/httplib.h @@ -8265,6 +8265,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr, if (!line_reader.getline()) { return false; } Request req; + req.start_time_ = std::chrono::steady_clock::now(); Response res; res.version = "HTTP/1.1"; From 8e0c8b1d74be7e8c6db7c9d7e2308c14918d51ed Mon Sep 17 00:00:00 2001 From: tejas Date: Sun, 24 Aug 2025 20:47:04 +0530 Subject: [PATCH 2/4] Add test to verify start time is initialized --- test/test.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/test.cc b/test/test.cc index 0e4fd42d58..9c02e4a0ad 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3200,6 +3200,10 @@ class ServerTest : public ::testing::Test { [&](const Request & /*req*/, Response &res) { res.set_content("abcdefg", "text/plain"); }) + .Get("/custom", + [&](const Request &req, Response &res) { + if (custom_fn) { custom_fn(req, res); } + }) .Get("/with-range-customized-response", [&](const Request & /*req*/, Response &res) { res.status = StatusCode::BadRequest_400; @@ -3594,6 +3598,7 @@ class ServerTest : public ::testing::Test { #endif thread t_; std::vector request_threads_; + std::function custom_fn; }; TEST_F(ServerTest, GetMethod200) { @@ -5800,6 +5805,13 @@ TEST_F(ServerTest, TooManyRedirect) { EXPECT_EQ(Error::ExceedRedirectCount, res.error()); } +TEST_F(ServerTest, StartTime) { + custom_fn = [](const Request &req, Response &res) { + EXPECT_NE(req.start_time_, std::chrono::steady_clock::time_point::min()); + }; + auto res = cli_.Get("/custom"); +} + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) { Headers headers; From bea8007033ca63e422f2cb0061c9ac2d2446918c Mon Sep 17 00:00:00 2001 From: tejas Date: Mon, 25 Aug 2025 20:47:24 +0530 Subject: [PATCH 3/4] Address review comments --- test/test.cc | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/test.cc b/test/test.cc index 9c02e4a0ad..ac5a3031db 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3200,9 +3200,10 @@ class ServerTest : public ::testing::Test { [&](const Request & /*req*/, Response &res) { res.set_content("abcdefg", "text/plain"); }) - .Get("/custom", + .Get("/test-start-time", [&](const Request &req, Response &res) { - if (custom_fn) { custom_fn(req, res); } + EXPECT_NE(req.start_time_, + std::chrono::steady_clock::time_point::min()); }) .Get("/with-range-customized-response", [&](const Request & /*req*/, Response &res) { @@ -3598,7 +3599,6 @@ class ServerTest : public ::testing::Test { #endif thread t_; std::vector request_threads_; - std::function custom_fn; }; TEST_F(ServerTest, GetMethod200) { @@ -5806,10 +5806,7 @@ TEST_F(ServerTest, TooManyRedirect) { } TEST_F(ServerTest, StartTime) { - custom_fn = [](const Request &req, Response &res) { - EXPECT_NE(req.start_time_, std::chrono::steady_clock::time_point::min()); - }; - auto res = cli_.Get("/custom"); + auto res = cli_.Get("/test-start-time"); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT From cb8e3b86a909e56bb5e49388586e4263fcd07859 Mon Sep 17 00:00:00 2001 From: tejas Date: Tue, 26 Aug 2025 21:12:28 +0530 Subject: [PATCH 4/4] run clang format --- test/test.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/test.cc b/test/test.cc index ac5a3031db..4788ada383 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5805,9 +5805,7 @@ TEST_F(ServerTest, TooManyRedirect) { EXPECT_EQ(Error::ExceedRedirectCount, res.error()); } -TEST_F(ServerTest, StartTime) { - auto res = cli_.Get("/test-start-time"); -} +TEST_F(ServerTest, StartTime) { auto res = cli_.Get("/test-start-time"); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) {