@@ -373,6 +373,28 @@ using socket_t = int;
373373#include < zstd.h>
374374#endif
375375
376+ // Profiling support
377+ #ifdef CPPHTTPLIB_PROFILE
378+ #include < chrono>
379+ #include < iostream>
380+ #define CPPHTTPLIB_PROFILE_POINT (name ) \
381+ do { \
382+ static auto __profile_start = std::chrono::high_resolution_clock::now (); \
383+ auto __profile_now = std::chrono::high_resolution_clock::now (); \
384+ auto __profile_elapsed = \
385+ std::chrono::duration_cast<std::chrono::microseconds>(__profile_now - \
386+ __profile_start) \
387+ .count (); \
388+ std::cout << " [PROFILE] " << name << " : " << __profile_elapsed << " μs" \
389+ << std::endl; \
390+ __profile_start = __profile_now; \
391+ } while (0 )
392+ #else
393+ #define CPPHTTPLIB_PROFILE_POINT (name ) \
394+ do { \
395+ } while (0 )
396+ #endif
397+
376398/*
377399 * Declaration
378400 */
@@ -4090,6 +4112,7 @@ inline socket_t create_client_socket(
40904112 time_t connection_timeout_usec, time_t read_timeout_sec,
40914113 time_t read_timeout_usec, time_t write_timeout_sec,
40924114 time_t write_timeout_usec, const std::string &intf, Error &error) {
4115+ CPPHTTPLIB_PROFILE_POINT (" create_client_socket start" );
40934116 auto sock = create_socket (
40944117 host, ip, port, address_family, 0 , tcp_nodelay, ipv6_v6only,
40954118 std::move (socket_options),
@@ -4107,16 +4130,20 @@ inline socket_t create_client_socket(
41074130
41084131 set_nonblocking (sock2, true );
41094132
4133+ CPPHTTPLIB_PROFILE_POINT (" before connect()" );
41104134 auto ret =
41114135 ::connect (sock2, ai.ai_addr, static_cast <socklen_t >(ai.ai_addrlen));
4136+ CPPHTTPLIB_PROFILE_POINT (" after connect()" );
41124137
41134138 if (ret < 0 ) {
41144139 if (is_connection_error ()) {
41154140 error = Error::Connection;
41164141 return false ;
41174142 }
4143+ CPPHTTPLIB_PROFILE_POINT (" before wait_until_socket_is_ready" );
41184144 error = wait_until_socket_is_ready (sock2, connection_timeout_sec,
41194145 connection_timeout_usec);
4146+ CPPHTTPLIB_PROFILE_POINT (" after wait_until_socket_is_ready" );
41204147 if (error != Error::Success) {
41214148 if (error == Error::ConnectionTimeout) { quit = true ; }
41224149 return false ;
@@ -8728,6 +8755,7 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) {
87288755}
87298756
87308757inline bool ClientImpl::send_ (Request &req, Response &res, Error &error) {
8758+ CPPHTTPLIB_PROFILE_POINT (" send_ start" );
87318759 {
87328760 std::lock_guard<std::mutex> guard (socket_mutex_);
87338761
@@ -8738,6 +8766,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
87388766
87398767 auto is_alive = false ;
87408768 if (socket_.is_open ()) {
8769+ CPPHTTPLIB_PROFILE_POINT (" check socket is_alive" );
87418770 is_alive = detail::is_socket_alive (socket_.sock );
87428771
87438772#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@@ -8761,10 +8790,12 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
87618790 }
87628791
87638792 if (!is_alive) {
8793+ CPPHTTPLIB_PROFILE_POINT (" before create_and_connect_socket" );
87648794 if (!create_and_connect_socket (socket_, error)) {
87658795 output_error_log (error, &req);
87668796 return false ;
87678797 }
8798+ CPPHTTPLIB_PROFILE_POINT (" after create_and_connect_socket" );
87688799
87698800#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
87708801 // TODO: refactoring
@@ -8857,6 +8888,7 @@ inline Result ClientImpl::send_(Request &&req) {
88578888inline bool ClientImpl::handle_request (Stream &strm, Request &req,
88588889 Response &res, bool close_connection,
88598890 Error &error) {
8891+ CPPHTTPLIB_PROFILE_POINT (" handle_request start" );
88608892 if (req.path .empty ()) {
88618893 error = Error::Connection;
88628894 output_error_log (error, &req);
@@ -8870,11 +8902,15 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req,
88708902 if (!is_ssl () && !proxy_host_.empty () && proxy_port_ != -1 ) {
88718903 auto req2 = req;
88728904 req2.path = " http://" + host_and_port_ + req.path ;
8905+ CPPHTTPLIB_PROFILE_POINT (" before process_request (proxy)" );
88738906 ret = process_request (strm, req2, res, close_connection, error);
8907+ CPPHTTPLIB_PROFILE_POINT (" after process_request (proxy)" );
88748908 req = req2;
88758909 req.path = req_save.path ;
88768910 } else {
8911+ CPPHTTPLIB_PROFILE_POINT (" before process_request" );
88778912 ret = process_request (strm, req, res, close_connection, error);
8913+ CPPHTTPLIB_PROFILE_POINT (" after process_request" );
88788914 }
88798915
88808916 if (!ret) { return false ; }
0 commit comments