How can I get closer to the proxy connection time for a cURL request using the values ​​of CURLINFO _ * _ TIME?

Below is a small data set from which I am trying to answer two questions:

  • How long did the proxy take to connect to the API server?
  • How long did it take to return the API request?

The main code is as follows:

$c = curl_init(); // assume all options set correctly $time = microtime(true); $response = curl_exec($c); $curl_info = curl_getinfo($c); // Returns each `*_TIME` field $response_time = microtime(true)-$time; // Returns total PHP execution time 

From the foregoing, I will build this:

 id response_time NAMELOOKUP_TIME CONNECT_TIME APPCONNECT_TIME PRETRANSFER_TIME STARTTRANSFER_TIME REDIRECT_TIME TOTAL_TIME 1 0.250691 0.000191 0.025070 NULL 0.181040 0.250239 0.000000 0.250306 2 0.958577 0.000129 0.022764 NULL 0.136846 0.664099 0.000000 0.957881 3 0.578614 0.000053 0.021111 NULL 0.127998 0.440123 0.000000 0.577812 

How much time was spent on the proxy server or api request for each of the above?


The cURL documentation is useful, but I'm not sure how to answer my questions above with the appropriate section from the docs:

 TOTAL_TIME Total time of previous transfer. NAMELOOKUP_TIME Time from start until name resolving completed. CONNECT_TIME Time from start until remote host or proxy completed. APPCONNECT_TIME Time from start until SSL/SSH handshake completed. PRETRANSFER_TIME Time from start until just before the transfer begins. STARTTRANSFER_TIME Time from start until just when the first byte is received. REDIRECT_TIME Time taken for all redirect steps before the final transfer. 

The included chart is useful to see how these moments add up:

 | |--NAMELOOKUP |--|--CONNECT |--|--|--APPCONNECT |--|--|--|--PRETRANSFER |--|--|--|--|--STARTTRANSFER |--|--|--|--|--|--TOTAL |--|--|--|--|--|--REDIRECT 

But I'm still not sure what to specify the connection time of the proxy server. Here is a chart with my comments:

 | |--NAMELOOKUP // DNS, clearly not proxy. Also insignificant values. |--|--CONNECT // Does this count toward Proxy Time? |--|--|--APPCONNECT // Not set (likely due to non-https transaction) |--|--|--|--PRETRANSFER // Does this count toward Proxy Time? |--|--|--|--|--STARTTRANSFER // Stop proxy time? So Proxy Time = STARTTRANSFER? |--|--|--|--|--|--TOTAL // Would TOTAL-STARTRANSFER = API Request Time? |--|--|--|--|--|--REDIRECT // Always 0 (???) 

Here is a graph of how HTTP proxies work. Where do these CURLINFO _ * _ TIME elements correspond to this graph?

HTTP Proxies

+7
curl proxy libcurl
source share
1 answer

I don’t think there is any way to accurately calculate what you are looking for.

cURL connects to the proxy, sends a request and waits for a response. Everything the proxy does in time (it has DNS resolution, connects to the host, sends (proxies) the request, waits for a response, reads the response and proxies it back - this is a black box for cURL.

There is no way to find out how long any of these steps have been taken individually solely from the HTTP / SOCKS proxy.

The only thing you can know for sure is the sum of all these actions ( CURLINFO_TOTAL_TIME - CURLINFO_STARTTRANSFER_TIME ). I suppose it would be possible that there might be a slight delay from the moment the proxy completes the request, when cURL gets the first byte back (maybe caching?).

Another possibility that I'm not entirely sure of (it may depend on the proxy configuration and response headers sent by the API) is when the proxy server really sends the data back. You may need to download the entire HTTP response, or it may start sending headers and data as they read. This can lead to some potentially significant differences in your calculations.

Ultimately, I believe that the main reason is that you do not have data about the connection to the proxy server, there is no way to find out or determine exactly how long the proxy connection to the API has been accepted, and how long it took to get the response. Otherwise, you are right that TOTAL - STARTTRANSFER is your best approximation for response time if you use a proxy.

Not knowing exactly what you are trying to do or why, it might be best to rent some VPS or cloud instances at different geographical locations to run some PHP instances using cURL to connect directly to the API so that you have access to all metrics which you are looking for.

+2
source share

All Articles