From b8e21eac89a93234791b96e472de65354bf47cce Mon Sep 17 00:00:00 2001 From: tejas Date: Wed, 27 Aug 2025 01:04:13 +0530 Subject: [PATCH] Initialize start time for server (#2220) * 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 * Add test to verify start time is initialized * Address review comments * run clang format --- httplib.h | 1 + test/test.cc | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/httplib.h b/httplib.h index a8dd905..703c171 100644 --- a/httplib.h +++ b/httplib.h @@ -8264,6 +8264,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"; diff --git a/test/test.cc b/test/test.cc index 0e4fd42..4788ada 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3200,6 +3200,11 @@ protected: [&](const Request & /*req*/, Response &res) { res.set_content("abcdefg", "text/plain"); }) + .Get("/test-start-time", + [&](const Request &req, Response &res) { + EXPECT_NE(req.start_time_, + std::chrono::steady_clock::time_point::min()); + }) .Get("/with-range-customized-response", [&](const Request & /*req*/, Response &res) { res.status = StatusCode::BadRequest_400; @@ -5800,6 +5805,8 @@ TEST_F(ServerTest, TooManyRedirect) { EXPECT_EQ(Error::ExceedRedirectCount, res.error()); } +TEST_F(ServerTest, StartTime) { auto res = cli_.Get("/test-start-time"); } + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) { Headers headers;