1

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?

2
  • i would suggest using microtime(); .. since u know y.. Commented 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? Commented Nov 16, 2012 at 6:37

4 Answers 4

9

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

1

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

Comments

1

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

1 Comment

That would work but it would only be accurate to the second. I'd worry if the hour was necessary when recording the duration. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.