Loading time of php page in apache server

前端 未结 4 1205
长情又很酷
长情又很酷 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

    0 讨论(0)
  • 2021-01-07 09:13

    you can try the log format of apache where you can log the time taken (%D) in microseconds in your access logs.

    http://httpd.apache.org/docs/current/mod/mod_log_config.html#formats

    I have never tried it though..

    0 讨论(0)
  • 2021-01-07 09:27

    You can print the current time stamp at the starting of your code, then print it at the end and subtract end time from start time to see the total time to load and run through the data

    0 讨论(0)
  • You may use this to create log file yourself:

    in top of header file:

    $process_start = date('H:i:s');
    

    in footer file

    $process_end = date('H:i:s');
    echo "<br/> process_start {$process_start}<br/> process_end {$process_end}"
    

    Not best way but may work

    0 讨论(0)
提交回复
热议问题