Project Titanicarus: Part 7.2 – Supercharging PHP with HHVM

HHVM-LogoThe good people at Facebook have developed a fastcgi replacement for PHP-FPM called HHVM. HHVM is a just in time bytecode compiler that they say increases performance of web applications by as much as 10x. Anything that makes promises like this is immediately going to get my attention so I decided to try it out.

But first, lets do some benchmarking before I make any changes. Bear in mind that the hardware I’m using is quite modest and relatively loaded, so the baseline isn’t overly impressive.

The test server is running PHP 5.3.10-1ubuntu3.11 with Suhosin-Patch.

Pre-Upgrade Performance

www.mobilemafia.com.au (magento)

siege -c20 -t1m www.mobilemafia.com.au

Lifting the server siege…      done.
Transactions:         104 hits
Availability:      100.00 %
Elapsed time:       59.90 secs
Data transferred:        0.94 MB
Response time:       10.12 secs
Transaction rate:        1.74 trans/sec
Throughput:        0.02 MB/sec
Concurrency:       17.57
Successful transactions:         104
Failed transactions:           0
Longest transaction:       16.25
Shortest transaction:        7.04

www.hooton.org (wordpress)

siege -c20 -t1m www.hooton.org

Lifting the server siege…      done.
Transactions:         198 hits
Availability:      100.00 %
Elapsed time:       59.47 secs
Data transferred:        3.70 MB
Response time:        5.21 secs
Transaction rate:        3.33 trans/sec
Throughput:        0.06 MB/sec
Concurrency:       17.35
Successful transactions:         198
Failed transactions:           0
Longest transaction:        9.33
Shortest transaction:        1.03

Installing HHVM

I’m following the instructions on the HHVM Github site here.

add-apt-repository ppa:mapnik/boost
wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add -
echo deb http://dl.hhvm.com/ubuntu precise main | sudo tee /etc/apt/sources.list.d/hhvm.list
apt-get update
apt-get install hhvm

After you’ve finished installing it, you are presented with config and activation instructions:

********************************************************************
* HHVM is installed. Here are some more things you might want to do:
*
* Configure your webserver to use HHVM:
* $ sudo /usr/share/hhvm/install_fastcgi.sh
* $ sudo /etc/init.d/nginx restart
* $ sudo /etc/init.d/apache restart
* $ sudo /etc/init.d/hhvm restart
*
* Run command line scripts with HHVM:
* $ hhvm whatever.php
*
* Use HHVM for /usr/bin/php even if you have php-cli installed:
* $ sudo /usr/bin/update-alternatives –install /usr/bin/php php /usr/bin/hhvm 60
********************************************************************

I ran the install_fastcgi.sh script and changed the configs that pointed to the old fastcgi php-fpm config, replacing them with the recommended hhvm config that is stored in /etc/nginx/hhvm.conf

location ~ \.(hh|php)$ {
    fastcgi_keep_conn on;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;

}

I also ran “/usr/bin/update-alternatives –install /usr/bin/php php /usr/bin/hhvm 60” as suggested by the install script.

After starting up the hhvm daemon and restarting nginx, it is time to make sure the web server is working as expected.

Post Upgrade Test Results

Performance Pre-HHVM Performance Post-HHVM
www.mobilemafia.com.au (magento)Transactions:         104 hits
Availability:      100.00 %
Elapsed time:       59.90 secs
Data transferred:        0.94 MB
Response time:       10.12 secs
Transaction rate:        1.74 trans/sec
Throughput:        0.02 MB/sec
Concurrency:       17.57
Successful transactions:         104
Failed transactions:           0
Longest transaction:       16.25
Shortest transaction:        7.04
www.mobilemafia.com.au (magento)Transactions:         176 hits
Availability:      100.00 %
Elapsed time:       59.87 secs
Data transferred:        1.59 MB
Response time:        5.91 secs
Transaction rate:        2.94 trans/sec
Throughput:        0.03 MB/sec
Concurrency:       17.38
Successful transactions:         176
Failed transactions:           0
Longest transaction:       10.04
Shortest transaction:        4.02
www.hooton.org (wordpress)Transactions:         198 hits
Availability:      100.00 %
Elapsed time:       59.47 secs
Data transferred:        3.70 MB
Response time:        5.21 secs
Transaction rate:        3.33 trans/sec
Throughput:        0.06 MB/sec
Concurrency:       17.35
Successful transactions:         198
Failed transactions:           0
Longest transaction:        9.33
Shortest transaction:        1.03
www.hooton.org (wordpress)Transactions:         435 hits
Availability:      100.00 %
Elapsed time:       59.94 secs
Data transferred:        8.25 MB
Response time:        2.22 secs
Transaction rate:        7.26 trans/sec
Throughput:        0.14 MB/sec
Concurrency:       16.08
Successful transactions:         435
Failed transactions:           0
Longest transaction:        5.73
Shortest transaction:        0.74

Not bad! Pretty much doubles the speed of both apps which I’ve already spent a fair bit of effort optimising.

Conclusion

Install HHVM. It works.

Update:

Since originally posting this, I’ve made some additional performance hacks that have helped speed things up even more.

  • I played with using unix sockets, but found that it was actually slower. FAIL
  • Added “Eval.JitWarmupRequests = 3” to server.ini to reduce the number of runs a php script requires before its compiled. WIN!
  • Added “ResourceLimit.SocketDefaultTimeout=30” to server.ini to make slow scripts keep running. This may wind up being set higher later on. MEH
  • Added “Http.SlowQueryThreshold=30000” to server.ini for the same reasons as above. MEH

Richard also asked what the difference in WordPress speed is if I turn on a caching plugin, here’s the answer:

Performance Pre-HHVM Performance Post-HHVM Performance Post-HHVM with caching
www.hooton.org (wordpress)

Transactions:         198 hits
Availability:      100.00 %
Elapsed time:       59.47 secs
Data transferred:        3.70 MB
Response time:        5.21 secs
Transaction rate:        3.33 trans/sec
Throughput:        0.06 MB/sec
Concurrency:       17.35
Successful transactions:         198
Failed transactions:           0
Longest transaction:        9.33
Shortest transaction:        1.03

www.hooton.org (wordpress)

Transactions:         435 hits
Availability:      100.00 %
Elapsed time:       59.94 secs
Data transferred:        8.25 MB
Response time:        2.22 secs
Transaction rate:        7.26 trans/sec
Throughput:        0.14 MB/sec
Concurrency:       16.08
Successful transactions:         435
Failed transactions:           0
Longest transaction:        5.73
Shortest transaction:        0.74

www.hooton.org (wordpress)

Transactions:        1496 hits
Availability:      100.00 %
Elapsed time:       59.66 secs
Data transferred:       57.33 MB
Response time:        0.30 secs
Transaction rate:       25.08 trans/sec
Throughput:        0.96 MB/sec
Concurrency:        7.55
Successful transactions:        1496
Failed transactions:           0
Longest transaction:        2.20
Shortest transaction:        0.19

Last updated by at .