Benchmarking and Timing PHP Class
Introducing a simple, yet deceivingly useful, micro benchmarking / timer class called Bench.
I’ve found myself on more than a few occasion using this familiar block of code in order to quickly narrow down a bottleneck or test a specific section of code.
1 2 3 4 |
This is a crude but effective way of finding out how long it takes to get from line A to line B. On the down-side, this technique is annoying to implement, only measures time between two points, and makes code look very cluttered — so I started developing Bench.
Features
Bench offers more than just clean looking code or timing between two lines. Its goal is to be more powerful and reusable than simple Microtime Arithmetic while trying avoid stepping on anyone’s toes (*cough* *cough* Xdebug, see below).
Start and Stop
Bench can perform the same operation as the example above but in a much cleaner manner.
1 2 3 | Bench::start(); // [Some Code To Test] echo Bench::stop() . ' Seconds.'; |
Marks
“Marks” are simply points in code that one would like tracked by Bench. They are easy to use, quick to implement, and very useful when tracking multiple parts of a script/request.
1 2 3 4 5 6 7 8 9 | Bench::start(); // [Application Bootstrap] Bench::mark('bootstrap'); // [Database Connection Opened] Bench::mark('database'); // [Data Processing + Manipulation] Bench::mark('processing'); // [HTML Creation] Bench::mark('html'); |
Each call to mark() creates a Mark Array that contains the id, microtime of when it occurred, seconds since start, and seconds since the previously called mark.
- October 26, 2010
- Categories: Development