Raptor Raptor - 4 months ago 21
PHP Question

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
)

Answer

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