PHP performance testing with GLPTimer

July 10, 2013 — 4 Comments

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

  1. Download GLPTimer and extract it.
  2. Make sure you include it somewhere in your code
  3. Call it wherever you want to take a reading using: GLPTimer::getInstance()->snap();

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.


Download GLPTimer here.

Version History

2013-July-10 Initial release


GLPTimer called 5 times on a test page, just as an example of how it looks outside normal page flow.
php performance info in GLPTimer

GLPTimer measuring php performance 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.

php error reporting

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.

Other resources

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.

4 responses to PHP performance testing with GLPTimer

  1. Thanks! I’ll give this class a try.

Trackbacks and Pingbacks:

  1. PHP rand vs mt_rand and openssl_random_pseudo_bytes | Go Learn PHP - July 17, 2013

    [...] the code below… you need the GLPTimer class to do the performance [...]

  2. URL Filtering: make text url-safe - Go Learn PHP - August 23, 2013

    [...] measure the actual speed difference, try my GLPTimer php performance [...]

  3. Wordpress performance: 17 things to do right now - Go Learn PHP - September 5, 2013

    [...] your loops first for expensive code. Optimization-wise, loops are low-hanging fruit. Use GLPTimer, cache grind, or similar to find bottlenecks. Focus on the WordPress loop and any other big loops. [...]

Leave a Reply to Eli Cancel reply


Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>