Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: measure TTFB (Time To First Byte)

Since the TTFB can vary by every request, I want to make a statistic and get an average value for it. Does anyone know how I can measure this via PHP? The website bytecheck.com is able to analyze these data: Here is an example with example.com: http://www.bytecheck.com/results?resource=example.com

Does anyone a suggestions how I could achieve something like this?

Thanks in advance.

like image 618
Michael Walter Avatar asked Mar 14 '26 00:03

Michael Walter


1 Answers

You can solve this with curl:

$url = 'https://www.example.com/';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo "TTFB of ".$url." is: ".$info['starttransfer_time'];

Result

TTFB of https://www.example.com/ is: 0.67417
like image 54
spinsch Avatar answered Mar 15 '26 14:03

spinsch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!