Optimizing Magento Performance — Using HHVM

Exclusive offer: get 50% off this eBook here
Magento Site Performance Optimization

Magento Site Performance Optimization — Save 50%

Leverage the power of Magento to speed up your website with this book and ebook

£8.99    £4.50
by Mathieu Nayrolles | May 2014 | e-Commerce Open Source Web Development

This article is written by Mathieu Nayrolles, the author of Magento Performance Site Optimization. HipHop Virtual Machine (HHVM) is the cornerstone of the PHP processing stack of Facebook and is currently able to increase the number of requests handle by a server by 9. It is open source and you can download it on GitHub.

(For more resources related to this topic, see here.)

HipHop Virtual Machine

As we can write a whole book (or two) about HHVM, we will just give the key ideas here. HHVM is a virtual machine that will translate any called PHP file into a HHVM byte code in the same spirit as the Java or .NET virtual machine. HHVM transforms your PHP code into a lower level language that is much faster to execute. Of course, the transformation time (compiling) does cost a lot of resources, therefore, HHVM is shipped with a cache mechanism similar to APC. This way, the compiled PHP files are stored and reused when the original file is requested. With HHVM, you keep the PHP flexibility and ease in writing, but you now have a performance like that of C++. Hear the words of the HHVM team at Facebook:

"HHVM (aka the HipHop Virtual Machine) is a new open-source virtual machine designed for executing programs written in PHP. HHVM uses a just-in-time compilation approach to achieve superior performance while maintaining the flexibility that PHP developers are accustomed to. To date, HHVM (and its predecessor HPHPc) has realized over a 9x increase in web request throughput and over a 5x reduction in memory consumption for Facebook compared with the Zend PHP 5.2 engine + APC.

HHVM can be run as a standalone webserver (in other words, without the Apache webserver and the "modphp" extension). HHVM can also be used together with a FastCGI-based webserver, and work is in progress to make HHVM work smoothly with Apache."

If you think this is too good to be true, you're right! Indeed, HHVM have a major drawback. HHVM was and still is focused on the needs of Facebook. Therefore, you might have a bad time trying to use your custom made PHP applications inside it. Nevertheless, this opportunity to speed up large PHP applications has been seen by talented developers who improve it, day after day, in order to support more and more framework. As our interest is in Magento, I will introduce you Daniel Sloof who is a developer from Netherland. More interestingly, Daniel has done (and still does) an amazing work at adapting HHVM for Magento.

Here are the commands to install Daniel Sloof's version of HHVM for Magento:

  1. $ sudo apt-get install git

  2. $ git clone https://github.com/danslo/hhvm.git

  3. $ sudo chmod +x configure_ubuntu_12.04.sh

  4. $ sudo ./configure_ubuntu_12.04.sh

  5. $ sudo CMAKE_PREFIX_PATH=`pwd`/.. make

If you thought that the first step was long, you will be astonished by the time required to actually build HHVM. Nevertheless, the wait is definitely worth it. The following screenshot shows how your terminal will look for the next hour or so:

Create a file named hhvm.hdf under /etc/hhvm and write the following code inside:

Server { Port = 80 SourceRoot = /var/www/_MAGENTO_HOME_ } Eval { Jit = true } Log { Level = Error UseLogFile = true File = /var/log/hhvm/error.log Access { * { File = /var/log/hhvm/access.log Format = %h %l %u %t \"%r\" %>s %b } } } VirtualHost { * { Pattern = .* RewriteRules { dirindex { pattern = ^/(.*)/$ to = $1/index.php qsa = true } } } } StaticFile { FilesMatch { * { pattern = .*\.(dll|exe) headers { * = Content-Disposition: attachment } } } Extensions { css = text/css gif = image/gif html = text/html jpe = image/jpeg jpeg = image/jpeg jpg = image/jpeg png = image/png tif = image/tiff tiff = image/tiff txt = text/plain } }

Now, run the following command:

$ sudo ./hhvm –mode daemon –config /etc/hhvm.hdf

The hhvm executable is under hhvm/hphp/hhvm. Is all of this worth it? Here's the response:

ab -n 100 -c 5 http://192.168.0.105192.168.0.105/index.php/furniture
/livingroom.html
Server Software: Server Hostname: 192.168.0.105192.168.0.105 Server Port: 80 Document Path: /index.php/furniture/living-room.html Document Length: 35552 bytes Concurrency Level: 5 Time taken for tests: 4.970 seconds Requests per second: 20.12 [#/sec] (mean) Time per request: 248.498 [ms] (mean) Time per request: 49.700 [ms] (mean, across all concurrent requests) Transfer rate: 707.26 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 2 12.1 0 89 Processing: 107 243 55.9 243 428 Waiting: 107 242 55.9 242 427 Total: 110 245 56.7 243 428

We literally reach a whole new world here. Indeed, our Magento instance is six times faster than after all our previous optimizations and about 20 times faster than the default Magento served by Apache. The following graph shows the performances:

Our Magento instance is now flying at lightening speed, but what are the drawbacks? Is it still as stable as before? All the optimization we did so far, are they still effective? Can we go even further? In what follows, we present a non-exhaustive list of answers:

  • Fancy extensions and modules may (and will) trigger HHVM incompatibilities.

  • Magento is a relatively old piece of software and combining it with a cutting edge technology such as HHVM can have some unpredictable (and undesirable) effects.

  • HHVM is so complex that fixing a Magento-related bug requires a lot of skill and dedication.

  • HHVM takes care of PHP, not of cache mechanisms or the accelerator we installed before. Therefore, APC, memcached, and Varnish are still running and helping to improve our performances.

  • If you become addicted to performances, HHVM is now supporting Fast-CGI through Nginx and Apache. You can find out more about that at: http://www.hhvm.com/blog/1817/fastercgi-with-hhvm.

Summary

In this article, we successfully used the HipHop Virtual Machine (HHVM) from Facebook to serve Magento. This improvement optimizes our Magento performance incredibly (20 times faster), that is, the time required initially was 110 seconds while now it is less then 5 seconds.

Resources for Article:


Further resources on this subject:


Magento Site Performance Optimization Leverage the power of Magento to speed up your website with this book and ebook
Published: May 2014
eBook Price: £8.99
Book Price: £14.99
See more
Select your format and quantity:

About the Author :


Mathieu Nayrolles

Mathieu Nayrolles was born in France and lived in a small village in Côte d'Azur for almost 15 years. He started his computer science studies in France and continued them in Montréal, Canada, where he now lives with his wife. He holds two master degrees from eXia.Cesi (Software Engineering) and UQAM (Computer Science). He is now a PhD. student at Concordia University (Electrical and Computer Engineering), Montréal, Canada, under the supervision of Dr. Abdlewahab Hamou-Lhadj, where he tries to improve the bug fixing process.

Along with his academic journey, Mathieu has been consulting as a Magento Performance Specialist since the development of Magento 1.6 (August 2011) and has also worked for worldwide companies such as Eurocopter or Saint-Gobain, where he learned how important good technical resources are.

You can discover more about his work by referring to other texts he has written, such as Instant Magento Performance Optimization How-to, Packt Publishing (February 2013), Mastering Apache Solr, or its latest realization at http://caramboles.fr.

You can find even more information on his personal website: http://mathieu-nayrolles.com.

Books From Packt


 Instant Magento Shipping How-to [Instant]
Instant Magento Shipping How-to [Instant]

Magento 1.4 Development Cookbook
Magento 1.4 Development Cookbook

Magento 1.3 Theme Design
Magento 1.3 Theme Design

Magento Beginner’s Guide - Second Edition
Magento Beginner’s Guide - Second Edition

Mastering Magento
Mastering Magento

 Magento 1.4 Themes Design
Magento 1.4 Themes Design

Magento Responsive Theme Design
Magento Responsive Theme Design

Magento: Beginner's Guide
Magento: Beginner's Guide


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software