📜 ⬆️ ⬇️

Yii2 test on HHVM

Hello! Recently released Yii2 with a bunch of new features and support for HHVM, the developers talk about compatibility in 99%. Let's try to start the whole thing and try it out in action on live examples, where there will be samples from the database, serialization (desiraralization) of data, json-encode, decode, work with ActiveRecord. But first, a little about the car itself. HHVM is an experimental Facebook virtual machine for executing and compiling PHP JIT code. Due to it, you can increase productivity by several, or even five - nine times for resource-intensive tasks. The project lives and is actively developing. About the release of new versions is well written in the article on Habré .

What has been done is optimized in HHVM:


- Support for php functionality in particular 5.6, support for functions: eval and create_function added in recent versions.
- Written a new programming language Hack - php - a similar language with static typing.
- The APC cache has been reworked, an alternative to which includes HHVM, the serialization functions (desrialization) have been removed, which, as is well known, are very expensive in terms of resources.
- Accelerated functions for JSON data encoding, UTF8 / UTF16 transformations.
- Less expensive reference counting - each line, array or object in php has a reference count, the counter increases when a variable is associated with a value, and decreases when the variable goes out of scope. Such operations take considerable processor time. A separate compiler was developed that tries to avoid reference counting when it is not needed.
- Improved memory allocation - problem areas have been optimized. Where memory is allocated and not used in the future, it is released.

Installing HHVM:


Currently available package installation and compilation of source codes for all popular distributions.
View support here :
HHVM (version 3.3.1) started up without problems from packages on Debian 7.7 and Ubuntu 14.04
Installation
wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add - echo deb http://dl.hhvm.com/debian wheezy main | sudo tee /etc/apt/sources.list.d/hhvm.list sudo apt-get update sudo apt-get install hhvm 


After installation, a configuration file /etc/nginx/hhvm.conf is created, which already contains the basic settings for location. we just need to create a host for yii in / etc / nginx / sites-available /
An example of the minimum config
 server {   root /www/hhvm.re/public_html;   index index.html index.htm index.php;   server_name hhvm-yii.local;   include hhvm.conf;   location / {       try_files $uri $uri/ /index.php?$args;   }   location ~ /\.ht {       deny all;   } } 


All set Yii in the usual way through the composer.
Start hhvm, restart nginx.
HHVM should start. If not, you can see the logs /var/log/hhvm/error.log
You can also tweak the configuration of php, in hhvm it has its own /etc/hhvm/server.ini.

Test time.


Testing was conducted on an old 2 nuclear laptop, amd 64, @ 1.9 GHz 4 GB of RAM:
Debian 7.7,
Nginx 1.2.1,
MySQL 5.5.38
One configuration: php-fpm 5.6
Second: hhvm 3.3.1
')
Each test will run 10 times, calculating the average values ​​for the hours worked.

1. Running Yii2 out of the box showed almost the same performance. Apparently, the framework is easy enough to optimize something. Hi WordPress)

2. The output of 300 news with pagination and different widgets:

 $newsList = new ActiveDataProvider([ 'query' => News::find(), 'pagination' => [ 'pageSize' => 30, ], 'sort' => false, ]); 


3. And now let's take more data, for example, 5k products, with registered links to suppliers, stores, and categories. We will display 300 products per page, so as not to waste time on trifles. There is a profit, but not the same as I would like:

 $productList = ActiveDataProvider([ 'query' => Product::find() ->where([ 'statusId' => 1, ]), 'pagination' => [ 'pageSize' => 300, ], 'sort' => false, ]); 


4. Standard task. We have 500 categories. Count the number of products for each category. Of course, the results can be put in the cache and the count is stored somewhere in a separate field. But we now want to work them out at runtime, let's see:

 $categoryList = ProductCategory::find()->all(); $listCount = []; foreach ($categoryList as $category){ $listCount[] = Product::find() ->where(['id_category' => $category->id]) ->count(); } 

Here the results are already more interesting, an increase of almost 4 times. Not bad, yes?

5. Serialization (deserialization) For example, we wanted to store the goods objects in some Memcache. Let's see how fast they will be packed / unpacked. The operation is quite expensive, you can not argue, especially on big data. Increase 3.67 times:

 $productList = Product::find()->all(); foreach ($productList as $product){ $serialize = serialize($product); unserialize($serialize); } 


6. Often have to encode / decode data in json. Especially true for different REST Api services or sample data for Single Page Application. Handling with HHVM is impressive, 5 times faster:

 $productList = Product::find()->all(); foreach ($productList as $product){ $encode = json_encode($product); json_decode($encode); } 


7. And finally, we will try to create a model that will write data to Redis. Yii2 provides us with an excellent opportunity for this . Critical in tasks with frequent recording, data sampling, where it does not make sense to cache data:

 for ($i=0; $i < 5000; $i++){ $customer = new Customer(); $customer->attributes = ['name' => $i]; $customer->save(); } 


Result table:
Test nameYii2 php5-fpm (sec.)Yii2 HHVM (sec.)Speed ​​boost
1. Out of the box0.100.091.1
2. News output0.170.161.06
3. Conclusion 5k. goods1.511.121.34
4. Counting products in the category2.820.633.61
5. Serialization / Deserialization3.240.883.68
6. JSON (encode, decode)2.730.515.35
7. Redis (Model ActiveRecord)10.534.432.37

Well, that's all, the results in numbers and on the face, in principle, not bad enough, from the HHVM left only positive impressions, do not need any magic crutches and dances to start it all. Everything you need in PHP and Yii is perfectly supported. I think we still need to drive on some small projects. To look at the stability of the work, if it would not fall if someone has experience in production, it would be interesting to listen. Yes, if you have suggestions for testing, write, try to get rid of. Good luck to all!

A few links .:
Yii2
HHVM

Source: https://habr.com/ru/post/243361/


All Articles