PHP:测量 TTFB(第一个字节的时间)
PHP: measure TTFB (Time To First Byte)
由于TTFB会因每个请求而异,我想做一个统计并得到一个平均值。有谁知道我如何通过 PHP 来衡量这个?
网站 bytecheck.com 能够分析这些数据:
这是 example.com 的示例:http://www.bytecheck.com/results?resource=example.com
有人建议我如何实现这样的目标吗?
提前致谢。
你可以用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'];
结果
TTFB of https://www.example.com/ is: 0.67417
这里有一个很好的 Curl 包装器:
https://packagist.org/packages/curl/curl
如果您已经安装了 Composer,只需 'composer require curl/curl' 并使用它。
此致。
由于TTFB会因每个请求而异,我想做一个统计并得到一个平均值。有谁知道我如何通过 PHP 来衡量这个? 网站 bytecheck.com 能够分析这些数据: 这是 example.com 的示例:http://www.bytecheck.com/results?resource=example.com
有人建议我如何实现这样的目标吗?
提前致谢。
你可以用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'];
结果
TTFB of https://www.example.com/ is: 0.67417
这里有一个很好的 Curl 包装器: https://packagist.org/packages/curl/curl
如果您已经安装了 Composer,只需 'composer require curl/curl' 并使用它。
此致。