Easy PHP Speed Gains

A few months ago I had the good fortune of attending the innaugral PHP Australia conference. Not surprisingly, Rasmus Lerdorf’s talk was awesome, especially the information about the speed improvements that are coming in PHP 7. However, Rasmus also mentioned the significant speed improvements that were made in the last couple of versions of PHP as well (5.5 and 5.6). Continuing the performance theme, Monty Widenius, creator of MySQL and, more recently, MariaDB, pointed out the performance benefits available in MariaDB.

Intrigued, I started wondering how much extra performance I could squeeze out of my $5 per month Debian VPS at Digital Ocean simply by upgrading my apps from PHP 5.4 to 5.6 and switching from MySQL 5.5 to MariaDB 10.0.

I was also planning on migrating these (very simple) apps from Laravel 4 to Laravel 5, so I set about measuring the baseline performance under Laravel 4 and then took more measurements as I applied the various upgrades.

The results were fairly predictable, although the upgrade from PHP 5.4 to 5.6 resulted in much more of a boost then I was anticipating.

My testing methodology was fairly straightforward. I installed Siege on another VPS at Digital Ocean that was only two network hops away so as to minimise network latency. The Apache config on the target VPS was straight out of the box - no changes made at all to thread pool sizes, etc. For each test I ran Siege with 20 concurrent users making 20 consecutive requests each to the home page, with zero delay between requests. Siege provides lots of interesting output, but the main ones I was interested in were response time and transaction rate.

The two apps I tested were this blog and one of my side projects, randomyoutube.net. This blog could probably be implemented as static HTML, but I haven’t implemented that change yet, so it still does a couple of database queries each time the home page is rendered. The other site, randomyoutube.net, is pretty similar, although the database queries are slightly different. Both sites also log site visits by writing a row to an access log table, so there is one database write on each page view as well.

First up was the migration from Laravel 4 to 5. Remember, I only had PHP 5.4 at this stage and clearly Laravel 5 was doing a bit more work on boot up, so performance actually went down 6% for randomyoutube.net and 10% for this blog.

That was no problem though, because the upgrade to PHP 5.6 more than made up for that slight loss - both apps got a performance boost of around 4x with this upgrade! Average response times dropped to about one quarter of what they were previously and, correspondingly, the transaction rate pretty much quadrupled for both applications. That is a pretty amazing result considering the relatively small amount of effort required to do the upgrade.

The only code change I had to make in order to upgrade to PHP 5.6 was to replace some preg_replace() calls that were using the callback operator with preg_replace_callback(). Due the Debian distro I was on, switching to PHP 5.6 also required using the dotdeb packages, so the upgrade wasn’t quite as simple as issuing two or three apt-get commands, but still, it was a fairly straightforward process and the resulting speed improvements, IMO, more than justified the cost.

The final step was the switch to MariaDB. I had been told by some guys I met at the conference how easy it was to install MariaDB - that it truly was a “drop-in replacement for MySQL”. I honestly didn’t believe them until I went through the process myself and discovered that yes, it really is that simple. I backed up my databases, issued a few apt-get commands, restored my databases, re-created some users and voila - everything was up and running again! I think the apps were off-line for maybe one hour, tops.

Although the performance improvement gained by the MariaDB upgrade was only around 5-6%, that’s a fairly solid result given the extremely small amount of database processing that happens with each page view. I am very much looking forward to converting some of my larger scale applications to MariaDB, as I am anticipating some much more substantial gains with those.

So in conclusion, if you haven’t upgrade to PHP 5.6 yet, and you have the option to do so, DO IT! And if you haven’t switched to MariaDB yet, and you have the option to do so, DO IT! :-)

Yet Another Programming Blog

Where James Gordon rambles about PHP and web development in general.

Find me on Twitter Find me on Stack Exchange Find me on Github Subscribe