1
0
mirror of synced 2025-09-05 13:43:59 +03:00

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
This commit is contained in:
tejas
2025-08-27 01:04:13 +05:30
committed by GitHub
parent 3fae5f1473
commit b8e21eac89
2 changed files with 8 additions and 0 deletions

View File

@@ -8264,6 +8264,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
if (!line_reader.getline()) { return false; } if (!line_reader.getline()) { return false; }
Request req; Request req;
req.start_time_ = std::chrono::steady_clock::now();
Response res; Response res;
res.version = "HTTP/1.1"; res.version = "HTTP/1.1";

View File

@@ -3200,6 +3200,11 @@ protected:
[&](const Request & /*req*/, Response &res) { [&](const Request & /*req*/, Response &res) {
res.set_content("abcdefg", "text/plain"); 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", .Get("/with-range-customized-response",
[&](const Request & /*req*/, Response &res) { [&](const Request & /*req*/, Response &res) {
res.status = StatusCode::BadRequest_400; res.status = StatusCode::BadRequest_400;
@@ -5800,6 +5805,8 @@ TEST_F(ServerTest, TooManyRedirect) {
EXPECT_EQ(Error::ExceedRedirectCount, res.error()); EXPECT_EQ(Error::ExceedRedirectCount, res.error());
} }
TEST_F(ServerTest, StartTime) { auto res = cli_.Get("/test-start-time"); }
#ifdef CPPHTTPLIB_ZLIB_SUPPORT #ifdef CPPHTTPLIB_ZLIB_SUPPORT
TEST_F(ServerTest, Gzip) { TEST_F(ServerTest, Gzip) {
Headers headers; Headers headers;