由于TTFB可能因每个请求而异,我想进行统计并获得其平均值。有人知道如何通过PHP来测量吗?
网站bytecheck.com能够分析这些数据:
以下是使用example.com的示例:http://www.bytecheck.com/results?resource=example.com
请问是否有建议可以实现类似此功能?
提前感谢。
提前感谢。
$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
这里有一个很好的Curl包装器: https://packagist.org/packages/curl/curl
如果您已经安装了Composer,只需执行“composer require curl/curl”即可使用它。
祝好。