9.2. Benchmarking and Profiling
There are many techniques and utilities for estimating the speed of a program, and they should be used before, during, and after embarking on any mission to speed up your site, so that you don't waste time making changes without any appreciable difference or, worse, slowing your site down. Broadly speaking, they can be divided into two main categories: benchmarking is what you do when you conduct experiments to determine the best approach for something before implementing it for real, and profiling covers experiments you conduct on the real thing to see just how well it actually performs. Clearly, there is quite a lot of overlap between the two; many of the techniques and the tools are the same (because the tools don't necessarily care whether the code you're using them on is natural or synthetic). Tools exist to aid you in these at both OS and PHP levels.
9.2.1. PEAR Benchmark
You've already seen examples of benchmarking in the previous section. They were constructed ad-hoc, but the basic ideas they have in common are common enough to warrant being abstracted into a separate class:
Start a timer (noting the current time).
Do something.
Stop the timer (again noting the current time).
Compare the stopping time with the starting time to determine the elapsed duration.
Repeat steps 1-4 a lot, accumulating the durations.
Have a look at the results.
An obvious enhancement to this would be to label the timers, so that several could be started and stopped ...
Get Professional LAMP: Linux®, Apache, MySQL®, and PHP5 Web Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.