PHP:测量 TTFB(第一个字节的时间)

Mic*_*ter 3 php performance measurement

由于 TTFB 可能因每个请求而异,因此我想对其进行统计并获得平均值。有谁知道我如何通过 PHP 来衡量这个?网站 bytecheck.com 能够分析这些数据:以下是 example.com 的示例:http ://www.bytecheck.com/results?resource=example.com

有没有人建议我如何实现这样的目标?

提前致谢。

spi*_*sch 6

你可以用 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'];
Run Code Online (Sandbox Code Playgroud)

结果

TTFB of https://www.example.com/ is: 0.67417
Run Code Online (Sandbox Code Playgroud)