Raptor
Raptor

Reputation: 54258

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
)

Upvotes: 9

Views: 5108

Answers (2)

Somnath Muluk
Somnath Muluk

Reputation: 57786

  • Why does the Time to First Byte (TTFB) time equal to Total Time?

It depends on total data. If your total data transferred is almost zero then it will be equal.

  • Is the above timing cumulative? If yes, the transfer time is equal to 0ms?

Yes, it is cumulative. If data is big then it would have increased.

  • What does Pretransfer time mean?

Pretransfer time means time in seconds from start until just before file transfer begins

For more info on Curl GetInfo

It also depends on your connection speed.

You should also check this values.

  1. CURLINFO_SIZE_UPLOAD - Total number of bytes uploaded
  2. CURLINFO_SIZE_DOWNLOAD - Total number of bytes downloaded
  3. CURLINFO_SPEED_DOWNLOAD - Average download speed
  4. CURLINFO_SPEED_UPLOAD - Average upload speed

So you will understand your speed and data size.

Update:

With your updated output, we can clearly understand there is almost no data is uploaded and downloaded. So timings are making more sense now.

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

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

Upvotes: 2

Related Questions