Loading time of php page in apache server

前端 未结 4 1248
长情又很酷
长情又很酷 2021-01-07 08:38

Is it possible to know the time taken by Apache server to load a PHP page? Is there any log file generated inside Apache server for every page which is running under Apache

4条回答
  •  感动是毒
    2021-01-07 09:02

    I'm going to assume you wish to know how long it took for PHP to generate the page.

    When the script starts, since 5.1, there's an entry in $_SERVER you can use, called REQUEST_TIME. It contains the time when the request was started. At the end of your script you can calculate it like so:

    $time_taken = time() - $_SERVER['REQUEST_TIME'];
    

    Obviously, this is not very accurate and therefore the newer REQUEST_TIME_FLOAT was introduced in PHP 5.4:

    $time_taken = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
    

    For < 5.4 you could use this snippet at the beginning of your script for backwards compatibility:

    if (!isset($_SERVER['REQUEST_TIME_FLOAT'])) {
        $_SERVER['REQUEST_TIME_FLOAT'] = microtime(true);
    }
    

    Update

    To let Apache do this for you, you can add the %T and/or %D log format option, e.g.

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" **%T/%D**" combined
    

    The %T option lets you log the number of seconds taken and %D logs the microseconds (since Apache 2).

    See also: How long does it take to serve a request

提交回复
热议问题