Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timings of cURL request by getting details from curl_getinfo()

I wrote a little script to diagnose the connection time of my website, and here is the result:

Lookup time:                0.6454ms
Connect time:               1.1611ms
Pretransfer time:           1.1615ms
Redirect time:              0ms
Time to 1st Byte time:      43.397ms
Total time:                 43.445ms

The code used is as follow:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch) !== false) {
    $info = curl_getinfo($ch);
    echo 'Lookup time: ' . "\t\t" . ($info['namelookup_time'] * 1000) . 'ms' . PHP_EOL;
    echo 'Connect time: ' . "\t\t" . ($info['connect_time'] * 1000) . 'ms' . PHP_EOL;
    echo 'Pretransfer time: ' . "\t" . ($info['pretransfer_time']) . 'ms' . PHP_EOL;
    echo 'Redirect time: ' . "\t\t" . ($info['redirect_time'] * 1000) . 'ms' . PHP_EOL;
    echo 'Time to 1st Byte time: ' . "\t" . ($info['starttransfer_time'] * 1000) . 'ms' . PHP_EOL;
    echo 'Total time: ' . "\t\t" . ($info['total_time'] * 1000) . 'ms' . PHP_EOL;
} else {
    echo 'Error: ' . curl_error($ch);
}
curl_close($ch);

I don't quite understand the result above.

  1. Why does the Time to First Byte (TTFB) time equal to Total Time?
  2. Is the above timing cumulative? If yes, the transfer time is equal to 0ms?
  3. What does Pretransfer time mean?

Output of curl_getinfo($ch):

Array
(
    [url] => http://www.example.com/apply.php
    [content_type] => text/html
    [http_code] => 302
    [header_size] => 412
    [request_size] => 88
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.043445
    [namelookup_time] => 0.006454
    [connect_time] => 0.011611
    [pretransfer_time] => 0.011615
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 0.043397
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [redirect_url] => http://www.example.com/index.php
)
like image 652
Raptor Avatar asked Aug 04 '16 07:08

Raptor


People also ask

What is Curl_getinfo?

curl_getinfo — Get information regarding a specific transfer.

How do you use the GET method in cURL?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.

How do I get the information from a curl session?

#include <curl/curl.h> CURLcode curl_easy_getinfo (CURL *curl, CURLINFO info, ... ); Request internal information from the curl session with this function. The third argument MUST be a pointer to a long, a pointer to a char *, a pointer to a struct curl_slist * or a pointer to a double (as this documentation describes further down).

Is there a time breakdown for curl requests?

While the time breakdown from cURL can be useful, it doesn’t breakdown all of the various times for a request. For instance WebPageTest will report times for: SSL延迟有多大? – City Hunter […] Timing Analysis section.

What does curl_GetInfo() return?

curl_getinfo ( CurlHandle $handle, ?int $option = null ): mixed Gets information about the last transfer. A cURL handle returned by curl_init ().

What is curlinfo_response_code in curl?

curl_getinfo ( CurlHandle $handle, ?int $option = null ): mixed Gets information about the last transfer. A cURL handle returned by curl_init (). CURLINFO_HTTP_CODE - The last response code. As of cURL 7.10.8, this is a legacy alias of CURLINFO_RESPONSE_CODE


1 Answers

Don't round time for any thorough analysis of a request. 0ms and 0.5ms are ages apart for computers; read the raw numbers to avoid confusion.

1) If an HTTP request returns a single resource (as opposed to a complete web page), TTFB and Total Time are both the same things, unless the amount of data returned by the server was considerably larger.

2) Yes it is cumulative. Increase the amount of data returned by the URL and you will see that the transfer time will also increase from 0ms

3) Time spent waiting for the server to respond after it accepted the request

like image 139
Hanky Panky Avatar answered Oct 09 '22 09:10

Hanky Panky