Yii2 testing on HHVM

    Hello! Recently, Yii2 has been released with a bunch of new features and HHVM support, developers say 99% compatibility. Let's try to get this whole thing and try it in action using live examples, where there will be samples from the database, serialization (deserialization) of data, json - encode, decode, work with ActiveRecord. But first, a little about the car itself. HHVM is an experimental virtual machine from Facebook for executing and JIT compiling PHP code. Due to it, you can increase productivity by several, or even five - nine times on resource-intensive tasks. The project lives and is actively developing. Regarding the release of new versions, it is well written in an 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 have been added in recent versions.
    - A new Hack programming language has been written - php - a similar language with static typing.
    - Reworked APC cache, an alternative to which includes HHVM, the functions of serialization (desirialization), which are known to be very resource-intensive, have been removed.
    - Accelerated functions for JSON data encoding, UTF8 / UTF16 conversions.
    - Less costly reference counting - each line, array or object in php has a reference counter, the counter increases when the variable is associated with a value, and decrease when the variable goes out of scope. Such operations take up significant processor time. A separate compiler has been developed that try to avoid reference counting when it is not needed.
    - Improved memory allocation - problem areas have been optimized. Where memory is allocated and not used further, it is freed.

    Install HHVM:


    Currently, installation of packages and compilation of source codes for all popular distributions is available.
    You can see 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, the configuration file /etc/nginx/hhvm.conf is created, in which the basic settings for location are already recorded. we can only create a host for yii in / etc / nginx / sites-available /
    Minimal config example
    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;
       }
    }
    


    That's it, install Yii in the usual way through composer.
    We start hhvm, we restart nginx.
    HHVM should start. If not, you can look at the logs /var/log/hhvm/error.log
    You can also tweak the php configuration, in hhvm it has its own /etc/hhvm/server.ini.

    Test time.


    Testing was conducted on an old 2-core 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 be run by 10 times, calculating the average values ​​for the hours worked.

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

    2. Output 300 news with pagination and different widgets:

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


    3. Now let's take more data, for example, 5k products, with the prescribed links to suppliers, stores, and categories. We display 300 products per page so as not to be trifled. There is a profit, but so far not as we would like:

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


    4. The standard task. We have 500 categories. Count the number of products for each category. Of course, you can put the results in the cache and store count somewhere in a separate field. But we just want to work them out in 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, huh?

    5. Serialization (deserialization) For example, we wanted to store objects of goods in some kind of Memcache. Let's see how fast they will be packed / unpacked. The operation is quite expensive, you can’t argue, especially on big data. An increase of 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 relevant for different REST Api services or data sampling for Single Page Application. HHVM processing is impressive, 5 times faster:

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


    7. And finally, let's try to create a model that will write data in Redis. Yii2 provides us with a great opportunity for this . It is critical in tasks with frequent recording, data sampling, where it makes no sense to cache data:

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


    Results table:
    Test name Yii2 php5-fpm (sec.) Yii2 HHVM (sec.) Speed ​​gain
    1. Out of the box 0.10 0.09 1,1
    2. News output 0.17 0.16 1.06
    3. Conclusion 5k. of goods1.51 1.12 1.34
    4. Counting goods in a category 2.82 0.63 3.61
    5. Serialization / Deserialization 3.24 0.88 3.68
    6. JSON (encode, decode) 2.73 0.51 5.35
    7. Redis (Model ActiveRecord) 10.53 4.43 2,37

    Well, that’s all, the results in figures and on the face, in principle, are not bad enough, from HHVM there were only positive impressions, you do not need any magic crutches and dances to get it all done. Everything you need in PHP and Yii is well supported. I think we still have to drive on some small projects. It would be interesting to listen to see the stability of the work, will it not fall if someone has experience in using it in production? Yes, if you have any suggestions for testing, write, we’ll try to get rid of it. Good luck to all!

    A few links:
    Yii2
    HHVM

    Also popular now: