Skip to content

Commit b8e21ea

Browse files
authored
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
1 parent 3fae5f1 commit b8e21ea

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

httplib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8264,6 +8264,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
82648264
if (!line_reader.getline()) { return false; }
82658265

82668266
Request req;
8267+
req.start_time_ = std::chrono::steady_clock::now();
82678268

82688269
Response res;
82698270
res.version = "HTTP/1.1";

test/test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,6 +3200,11 @@ class ServerTest : public ::testing::Test {
32003200
[&](const Request & /*req*/, Response &res) {
32013201
res.set_content("abcdefg", "text/plain");
32023202
})
3203+
.Get("/test-start-time",
3204+
[&](const Request &req, Response &res) {
3205+
EXPECT_NE(req.start_time_,
3206+
std::chrono::steady_clock::time_point::min());
3207+
})
32033208
.Get("/with-range-customized-response",
32043209
[&](const Request & /*req*/, Response &res) {
32053210
res.status = StatusCode::BadRequest_400;
@@ -5800,6 +5805,8 @@ TEST_F(ServerTest, TooManyRedirect) {
58005805
EXPECT_EQ(Error::ExceedRedirectCount, res.error());
58015806
}
58025807

5808+
TEST_F(ServerTest, StartTime) { auto res = cli_.Get("/test-start-time"); }
5809+
58035810
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
58045811
TEST_F(ServerTest, Gzip) {
58055812
Headers headers;

0 commit comments

Comments
 (0)