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
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.).
The most simple too is Apache Bench (called ab
), which is provided with Apache :
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.
$ time curl http://www.example.com/
Note that it times the whole request, including network latency. But that may be want you want?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With