The problem: measuring PHP performance
It’s not always easy to tell why your PHP is not running well. Sometimes you need to spend some time hunting through your code examining how well it is performing at various places, eliminating bottlenecks as you go. You probably also want debugging info to be visible and easy to read. Here’s how I’ve been doing it lately, using a new php class called GLPTimer…
What GLPTmer is
This is a class that you can use to measure the performance of your PHP across a site. You include it once then call it wherever you need to take a reading. Each reading is displayed in-page, with the execution time to that step clearly displayed in seconds and microseconds.
How to use it to measure your PHP performance
- Download GLPTimer and extract it.
- Make sure you include it somewhere in your code
- Call it wherever you want to take a reading using:
What kinds of PHP performance issues can GLPTimer identify?
Any PHP that is performing poorly. Typically that will mean:
- Loops with many iterations, especially with inefficient handling of resources
- large and inefficient regular expression operations
- functions that perform many database queries, especially when these functions are called inside loops
GLPTimer can be used to find the slowest parts of your code. Of course it is up to you to figure out how to improve the speed.
Tips: how to use GLPTimer
Site performance can degrade over time. Often loops that run just fine when a site launches will begin to slow down as the number of iterations (i.e.: posts, records, rows) goes up.
If you suspect a loop might be slowing down your code, place the snap() code in step 3 above before and after the loop and examine A. the number of seconds elapsed and B. the memory increase. Any large increase in memory usage or long time delay will mean you can probably make some time savings.
How much time elapsed is too much?
This is a tough one to answer: it really depends. But one approach is to place the snap code at the start of your PHP, and before and after some key points (for example around the WordPress Loop). Then check the time elapsed there vs the time elapsed in your trouble spots. Always try to zoom in: focus on the main functional blocks first, then try to bookend the trouble spots with snap() calls.
2013-July-10 Initial release
Here’s an example of GLPTimer in use on this site. I’ve placed it at the beginning of every iteration of the WordPress loop. Note it looks nice and sits easily within the layout. (click for full-size version). Also don’t forget to check out the video above for an example of doing this in WordPress.
Also: if you’re interested in PHP performance you should check out my book about PHP error reporting. It’s reasonably priced and has some great pointers about improving reporting. It comes with downloadable source code. Check it out here.
Some more resources about php performance that might be useful:
PHP Performance Crash course – good overview at the AppDynamics blog. They recommend WebGrind for profiling; personally I find it too cumbersome, which is why I wrote GLPTimer for basic profiling.
Recommendations from Google – some good practical tips but as always with performance tweaking advice, you have to weigh speed against code readability and good practice.