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
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
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..
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
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