Blog » PHP performance: Switching to HHVM

PHP performance: Switching to HHVM

Getting good performance out of your web applications can be difficult, it often involves adding potentially complex caches to your application that can create additional problems, notably around cache invalidation, or pile-up from multiple requests hitting a cold cache at once. Things can be improved with the use of an HTTP cache server such as Varnish or Squid. These proxy servers sit in front of your site and cache content given rules such as HTTP expires headers. While these caches can be effective in most cases where your site's pages are static, they're not as effective if your application has content that regularly changes and simply cannot be cached.

A few years ago, a project from Facebook called HHVM came along whose goal was to provide a high performance drop-in replacement to PHP using techniques such as JIT compilation to increase performance. It has seen a number of improvements over the last few years and is being used in more and more production applications, such as Wikimedia (at the time of this writing) migrating to HHVM and seeing performance improvements and reductions in server load.

Tools like xhprof can help you to determine what's causing slowness in your PHP applications. Looking at a typical SilverStripe web request, it appears that a lot of the performance bottlenecks come from the Config class where a lot of PHP processing occurs, so this seemed like an area that could be improved on. Given that, I decided to benchmark PHP against HHVM on a SilverStripe site to see how much of a performance improvement we can achieve.

For this test, I used the latest SilverStripe 3.1 install, and created a website navigation using the CMS four levels deep with a lot of pages on each level, which may be a good test as the navigation has to be rendered dynamically on every request without caching. Tests were performed on an Ubuntu 14.04 machine with 2 cores and 4 GB RAM. HHVM 3.5.1, and PHP 5.6.6 versions were used. PHP has Zend Opcache enabled, and the same website code and MariaDB 5.5.41 database used. Tests were run using siege with a timed benchmark of 30 seconds simulating 100 concurrent users. The test was run 3 times to produce an average.

  PHP 5.6.6 (mod-php) PHP 5.6.6 (FPM) HHVM 3.5.1 Improvement
Average request time (seconds) 11.75 12.52 5.64 2.4x
Completed requests (total) 218 205 471 2.3x

In another test, we create 10000 ArrayData objects and render them into a template:

  PHP 5.6.6 (mod-php) PHP 5.6.6 (FPM) HHVM 3.5.1 Improvement
Average request time (seconds) 7.74 8.51 1.41 6x
Completed requests (total) 314 291 2130 7.3x

The benefits of HHVM seem to be immediate apparent: requests are faster in general, and there's a reduction in CPU and memory usage on the server. This can translate into reduced costs when running on cloud platforms like Amazon EC2. Keep in mind though that these are just simple tests, and your mileage may vary depending on your particular SilverStripe project.

PHP 7 is on the horizon, with some promising performance improvements, but it's unclear whether it will be as fast as HHVM since it's in early stages. At least right now you have the possibility of dropping in HHVM for better performance.