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 server?
-
i would suggest using microtime(); .. since u know y..Dinesh– Dinesh2012-11-16 06:29:28 +00:00Commented Nov 16, 2012 at 6:29
-
No in that case i have to use micro time in every php page created by user.I want to know can i check it from any log file of apache?Sahasrangshu Guha– Sahasrangshu Guha2012-11-16 06:37:34 +00:00Commented Nov 16, 2012 at 6:37
4 Answers
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
Comments
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..
Comments
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