PHP: measure TTFB (Time To First Byte)

221
August 15, 2017, at 6:38 PM

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.

Answer 1

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
Answer 2

There is a good Curl wrapper here: https://packagist.org/packages/curl/curl

If you already have installed Composer, just do 'composer require curl/curl' and use it.

Regards.

Rent Charter Buses Company
READ ALSO
How to secure an authentication in swift

How to secure an authentication in swift

I'm quite new to programming apps for iOSI've had a deeper look into creating a login for an app

280
What wordpress plugins should I use to build this service marketplace?

What wordpress plugins should I use to build this service marketplace?

I am trying to build a service-based marketplace for online tutoringThis includes 'tutors' selling their service in units of hours to 'students'

176
How to include X-Auth-Token from api in php

How to include X-Auth-Token from api in php

I want to load some json data from an API with php into my pageHowever i dont know how to include the authorization

297