【问题标题】:PHP: measure TTFB (Time To First Byte)PHP:测量 TTFB(到第一个字节的时间)
【发布时间】:2018-01-23 06:18:54
【问题描述】:

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

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

提前致谢。

【问题讨论】:

    标签: php performance measurement


    【解决方案1】:

    你可以用 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
    

    【讨论】:

    • 非常感谢。我一直在寻找这个,但我的谷歌查询都没有让我找到这个。
    【解决方案2】:

    这里有一个很好的 Curl 包装器: https://packagist.org/packages/curl/curl

    如果您已经安装了 Composer,只需执行 'composer require curl/curl' 并使用它。

    问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-16
      • 2015-05-29
      • 2015-04-08
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      相关资源
      最近更新 更多