views:

376

answers:

7

How could I measure the time taken to load a page (with various different PHP statements)?

Somewhat like the stats available here - http://talks.php.net/show/drupal08/24

+2  A: 

The most simple too is Apache Bench (called ab), which is provided with Apache :

  • It's a command-line tool
  • That can send many requests, in parallel, to and URL
  • And reports timings, errors, ...

Seems to fit the kind of very simple reporting that's presented on your slide.
(It does actually report more than that)


If your needs ar ea bit more complex, Siege can be a good alternative.

An interesting thing with Siege is that it can take a list of URLs from a file, instead of working with just one.


An interesting thing with those tools is that you are not measuring only the time taken to execute a specific portion of code (like you would if using microtime directly in your PHP code), but you're getting the whole time that was required to serve the page.

Also, it can benchmark more than just PHP code, as it's working on the HTTP request, and not the code itself.

Pascal MARTIN
+1  A: 

You can use microtime() at the start of processing and at end of output, then calculate the difference and convert it to seconds if wanted.

This will only measure the execution time for PHP side, not the whole page load time at it seems to be in your link, but it will able you to compare various methods performance, for instance.

Benoit
...but you need to record this somewhere for it to be of any value.
symcbean
sure, the question was about measuring page load time to compare code performance.In a pure debugging process, there is no need to record it, just display the difference of two microtimes in your page footer. For a global performance measuring I would suggest using external tool such as apache bench too.
Benoit
A: 
  1. method one: use xdebug.
  2. method two: Put these statements all around your your scripts

    $TIMER['label']=microtime(1);
    /* some code */
    $TIMER['sql1_before']=microtime(1);
    a/* some code */
    $TIMER['sql1_after']=microtime(1);
    /* some code */

and then output it, with code like this:

  echo "<table border=1><tr><td>name</td><td>so far</td><td>delta</td><td>per cent</td></tr>";
  reset($TIMER);
  $start=$prev=current($TIMER);
  $total=end($TIMER)-$start;
  foreach($TIMER as $name => $value) {
    $sofar=round($value-$start,3);
    $delta=round($value-$prev,3);
    $percent=round($delta/$total*100);
    echo "<tr><td>$name</td><td>$sofar</td><td>$delta</td><td>$percent</td></tr>";
    $prev=$value;
  }
    echo "</table>";

Thus, you'll get tetailed report on how your code goes. this action called profiling and take most important place in the optimization process.

Col. Shrapnel
+2  A: 
  $ time curl http://www.example.com/

Note that it times the whole request, including network latency. But that may be want you want?

Martin Wickman
+1  A: 

There are many ways to do this. I've personally been a fan of using microtime in the following way:

// Start of code
$time = microtime(true); // Gets microseconds

// Rest of code

// End of code
echo "Time Elapsed: ".(microtime(true) - $time)."s";

That will give you microsecond accuracy.

If you are writing command-line scripts (like Facebook puzzles), you can use just time.

time php dancebattle.php ~/input.dat
Win

real    0m0.152s
user    0m0.142s
sys     0m0.012s

I forgot about the method of monitoring page load times (from a browser). You can use the NET tab from Firebug (for Firefox) to do just that. It will let you watch the various file loads (AJAX, JS, CSS, Images, etc.).

St. John Johnson
A: 

The Output in that presentation seem to be copied from Siege (http://www.joedog.org/index/siege-home).

Another quite useful tool for "real world" performance testing your whole application stack is Firebug (http://getfirebug.com/) and YSlow (http://developer.yahoo.com/yslow/)

MGriesbach
A: 

You say you want to measure "Page load times" which is completely different from

  1. the time it takes to generate the page (as measured by an internal timer in your PHP code)

  2. and offload it from the server (which is measured by ab)

A page load time should include the time taken to parse the HTML and to make subsequent requests to the server to fetch all related content (javascript files, css files, images etc).

Measuring this is actually quite difficult. To do it properly, you need to push all the logic client side - drop a timestamped javascript cookie when the user clicks on a link or submits a form, then in the subsequent page, using the onload method (which fires after everything has loaded) compared this time with the current time. You then need a method of reporting this metric back to the server - you can use an Ajax request, or store the time in another cookie to be presented in the subsequent request.

Note that the files required by each client will depend on the current state of the browser side cache.

If you can isolate click streams from your logs, then you can get a good approximation by checking the interval between a request for a text/html content type and the last consecutive request for content type other than text/html. But your stats will get skewed if users interact usnig more than one browser window simultaeneously.

symcbean