+7

New generation of PHP

Welcome to Facebook's PHP

Do you know biggest of PHPer company? - That's Facebook. They have used PHP in a part of service since they started.

PHP is older language than other script language likes Ruby, Python and else. One says that PHP is older and would not use for web service. Otherwise, Facebook continues to use it. And, They made new PHP environment named "HHVM".

New environment has 4 features.

  • Souce code converting PHP into C++(JIT compiler)
  • Optimizing execution.
  • Supporting fcgi.
  • New language "Hack" compatible with PHP.

Taken together, PHP becomes very powerfully for execution performance!!

How about HHVM?

Do you know "Hiphop" what is one of the performance improvement way by converting PHP into C++. The idea same as HHVM is good, but it's manually and take much time. Facebook improved the process to be automatically, and, improves process of execution codes.

When you execute PHP (or Hack) code, HHVM makes C++ code automatically, and executes more faster than native code.

Would you have interresting with effects by FFVM? On one benchmarking says compiled code is faster 1.7~10 times than native. (The effects is very different by each environment and souce code)

How to install

Please look following page, this is souce code and written way to the installing. https://github.com/facebook/hhvm

If your pc's OS is Ubuntu, the way is very easy. You can do following steps.

$ sudo apt-get install wget

$ wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add
$ echo deb http://dl.hhvm.com/ubuntu saucy main | sudo tee /etc/apt/sources.list.d/hhvm.list

$ sudo apt-get update
$ sudo apt-get install hhvm

$ hhvm --version

Other way, If you use vagrant, more easy and it's no effects your current enviromnment. You make VagrantFile written following.

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

Ubuntu 12.04 LTS (64bit) を使う

  config.vm.box = "hashicorp/precise64"
  config.vm.network "public_network" # ネットワークブリッジを作成する

HHVM をインストールする

  config.vm.provision "shell", inline:<<-shell
    apt-get update
    apt-get install python-software-properties  -y --force-yes
    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-nightly -y --force-yes
    apt-get install screen vim -y --force-yes
    debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password pa$$'
    debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password pa$$'
    apt-get install mysql-server -y --force-yes
  shell
end

Using above two way, you may build HHVM environment in an hour.

Otherwise, If your have only CentOS PC or want to build without vagrant, The way to install is very hard work. In fact, I spent 2 days to build it.

Then, if you want to do it, please following page. This is facebook's official page.

https://github.com/facebook/hhvm/wiki/Building-and-installing-HHVM-on-CentOS-6.3

I've done above way, but it can't wakeup well.

There is also thirdparty's repository to install. But, it don't ensure.

http://nareshv.blogspot.jp/2014/04/installing-hhvm-301-on-centos-65-64-bit.html

My instration finally is this way.

Using hhvm

It's easy to use HHVM.

If you have php source code, execute follow way.

hhvm "your_php_code".php

Do do feel jsut it's easy? 😃

My HHVM's benchmark

What doing for PHP/HHVM

I tried it, and output effects. This benchmarks is simple program that finding biggest of prime number smaller then 30,000. In this case, I measured averages for 10 times.

The PHP code is following.

<?php
    $n = 30000;

    $prime = [];
    for ($i=3;$i<$n;$i++) {
        if ($i%2===0)   continue;

        $divided = array_filter($prime, function($v) use($i){
            return ($i%$v===0);
        });

        if (count($divided) === 0) $prime[] = $i;
    }
    echo array_pop($prime) . "\n";
?>

Attention, this code is wrong. Because, Time of compiling by HHVM is lack from benchmarking process. Then, I make shell script to measure all executing time.

The shell script is following.

/bin/sh

echo "try native php"
echo "----------"
echo "*** start time:"
 date +%s
php ./index.php
echo "*** end time:"
date +%s
echo "----------"

In case of using HHVM, change shell script.

php ./index.php

This line modified following

hhvm ./index.php

Result of effects

Above two way's result are following.

native HHVM
execute time[s] 83.1 6.4
ratio of exec 1 0.077
ratio of speed 1 12.98

(Following Line is row data, and please skip)

native HHVM
start finish diff start finish diff
1 1420521332 1420521414 82 1420523168 1420523174 6
2 1420521462 1420521541 79 1420523180 1420523186 6
3 1420520856 1420520935 79 1420523193 1420523199 6
4 1420521898 1420521988 90 1420523201 1420523208 7
5 1420522031 1420522112 81 1420523210 1420523217 7
6 1420522115 1420522198 83 1420523221 1420523228 7
7 1420522359 1420522441 82 1420523231 1420523237 6
8 1420522488 1420522569 81 1420523243 1420523249 6
9 1420522576 1420522666 90 1420523252 1420523259 7
10 1420522771 1420522855 84 1420523262 1420523268 6

Summary of first time

According to above result, HHVM is faster than native code. Especially in basically process, the speed is about 13 times.

Please remaind, I didn't change source code. If you using PHP, shift to HHVM is easy. and, don't worry if you use any frameworks.

Following is current state of supporting for any framceworks. http://hhvm.com/frameworks/

Epilogue

Can you believe this result? If you do, please setup HHVM and exec anything code. I want to know how HHVM with web framework is effective.

Next banchmark, I try it.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí