@@ -373,6 +373,27 @@ using socket_t = int;
373373#include < zstd.h>
374374#endif
375375
376+ // Profiling support
377+ #ifdef CPPHTTPLIB_PROFILE
378+ #include < iostream>
379+ #define CPPHTTPLIB_PROFILE_POINT (name ) \
380+ do { \
381+ static auto __profile_start = std::chrono::high_resolution_clock::now (); \
382+ auto __profile_now = std::chrono::high_resolution_clock::now (); \
383+ auto __profile_elapsed = \
384+ std::chrono::duration_cast<std::chrono::microseconds>(__profile_now - \
385+ __profile_start) \
386+ .count (); \
387+ std::cout << " [PROFILE] " << name << " : " << __profile_elapsed << " μs" \
388+ << std::endl; \
389+ __profile_start = __profile_now; \
390+ } while (0 )
391+ #else
392+ #define CPPHTTPLIB_PROFILE_POINT (name ) \
393+ do { \
394+ } while (0 )
395+ #endif
396+
376397/*
377398 * Declaration
378399 */
@@ -4090,6 +4111,7 @@ inline socket_t create_client_socket(
40904111 time_t connection_timeout_usec, time_t read_timeout_sec,
40914112 time_t read_timeout_usec, time_t write_timeout_sec,
40924113 time_t write_timeout_usec, const std::string &intf, Error &error) {
4114+ CPPHTTPLIB_PROFILE_POINT (" create_client_socket start" );
40934115 auto sock = create_socket (
40944116 host, ip, port, address_family, 0 , tcp_nodelay, ipv6_v6only,
40954117 std::move (socket_options),
@@ -4107,16 +4129,20 @@ inline socket_t create_client_socket(
41074129
41084130 set_nonblocking (sock2, true );
41094131
4132+ CPPHTTPLIB_PROFILE_POINT (" before connect()" );
41104133 auto ret =
41114134 ::connect (sock2, ai.ai_addr, static_cast <socklen_t >(ai.ai_addrlen));
4135+ CPPHTTPLIB_PROFILE_POINT (" after connect()" );
41124136
41134137 if (ret < 0 ) {
41144138 if (is_connection_error ()) {
41154139 error = Error::Connection;
41164140 return false ;
41174141 }
4142+ CPPHTTPLIB_PROFILE_POINT (" before wait_until_socket_is_ready" );
41184143 error = wait_until_socket_is_ready (sock2, connection_timeout_sec,
41194144 connection_timeout_usec);
4145+ CPPHTTPLIB_PROFILE_POINT (" after wait_until_socket_is_ready" );
41204146 if (error != Error::Success) {
41214147 if (error == Error::ConnectionTimeout) { quit = true ; }
41224148 return false ;
@@ -8728,6 +8754,7 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) {
87288754}
87298755
87308756inline bool ClientImpl::send_ (Request &req, Response &res, Error &error) {
8757+ CPPHTTPLIB_PROFILE_POINT (" send_ start" );
87318758 {
87328759 std::lock_guard<std::mutex> guard (socket_mutex_);
87338760
@@ -8738,6 +8765,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
87388765
87398766 auto is_alive = false ;
87408767 if (socket_.is_open ()) {
8768+ CPPHTTPLIB_PROFILE_POINT (" check socket is_alive" );
87418769 is_alive = detail::is_socket_alive (socket_.sock );
87428770
87438771#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@@ -8761,10 +8789,12 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
87618789 }
87628790
87638791 if (!is_alive) {
8792+ CPPHTTPLIB_PROFILE_POINT (" before create_and_connect_socket" );
87648793 if (!create_and_connect_socket (socket_, error)) {
87658794 output_error_log (error, &req);
87668795 return false ;
87678796 }
8797+ CPPHTTPLIB_PROFILE_POINT (" after create_and_connect_socket" );
87688798
87698799#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
87708800 // TODO: refactoring
@@ -8857,6 +8887,7 @@ inline Result ClientImpl::send_(Request &&req) {
88578887inline bool ClientImpl::handle_request (Stream &strm, Request &req,
88588888 Response &res, bool close_connection,
88598889 Error &error) {
8890+ CPPHTTPLIB_PROFILE_POINT (" handle_request start" );
88608891 if (req.path .empty ()) {
88618892 error = Error::Connection;
88628893 output_error_log (error, &req);
@@ -8870,11 +8901,15 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req,
88708901 if (!is_ssl () && !proxy_host_.empty () && proxy_port_ != -1 ) {
88718902 auto req2 = req;
88728903 req2.path = " http://" + host_and_port_ + req.path ;
8904+ CPPHTTPLIB_PROFILE_POINT (" before process_request (proxy)" );
88738905 ret = process_request (strm, req2, res, close_connection, error);
8906+ CPPHTTPLIB_PROFILE_POINT (" after process_request (proxy)" );
88748907 req = req2;
88758908 req.path = req_save.path ;
88768909 } else {
8910+ CPPHTTPLIB_PROFILE_POINT (" before process_request" );
88778911 ret = process_request (strm, req, res, close_connection, error);
8912+ CPPHTTPLIB_PROFILE_POINT (" after process_request" );
88788913 }
88798914
88808915 if (!ret) { return false ; }
0 commit comments