Main Content

A story about benchmarking PHP

Archive - Originally posted on "The Horse's Mouth" - 2007-12-23 13:31:07 - Graham Ellis

I've noticed recently that the response speed of this website hasn't been as brisk as I would have liked, and thought that the cause was a steady growth in the amount of code we run behind the scenes on each page - elements I particularly felt were culprits were identifying visitors to a country by their IP addresses, and the background booking system which I hope will be live within the next few months. Plans for these next few days included a couple of hours to look at each of these areas of concern and see if I could save a few cycles here and there, add in some caches, etc.

But then I have noticed that the occasional page that wasn't going through these areas was also running a little slower, and I started to investigate. The host we're using is a shared server and I have shell login ... and a few uses of the uptime command convinced me that all was not as it should be; there seemed always to be more work that the c.p.u. could handle, and a similar test run on an identical machine on which I look after another site did not show similar overloading. So perhaps something to have a word with my ISP about?

This is one of those cautionary Christmas tales!. One last check before I wrote an "oy - what's going on" type email to the ISP ... I added in a series of 30 calls to PHP's microtime function splattered throughout a sample page, saved them in an array and dumped them out at the end of the page (I don't expect anyone but me saw them - I can't imagine that may people will be looking for the printable form of the Tk course description this morning! And what did I find? That the elements I has suspected were only taking a few milliseconds; the real culprit was a MySQL database enquiry that searched 3 million records on a non-indexed field!. Solution - short term - clean up that table that, frankly, has a lot of old crud in it. Then add an index!!

And a 2 reminder lessons - firstly not to make assumptions, and secondly of just how useful a relatively crude tool like microtime can be!

You can click on that tk course link above if you like - it'll reveal out stats at the bottom. But you shouldn't see a jump of 5 seconds between stages 15 and 16 like I did this morning!