07/09/2018, 16:01

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. ...

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|
  config.vm.box = "hashicorp/precise64"
  config.vm.network "public_network" # ネットワークブリッジを作成する
  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) . "
";
?>

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.

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.

nativeHHVM
execute time[s]83.16.4
ratio of exec10.077
ratio of speed112.98

(Following Line is row data, and please skip)

nativeHHVM
startfinishdiffstartfinishdiff
11420521332142052141482142052316814205231746
21420521462142052154179142052318014205231866
31420520856142052093579142052319314205231996
41420521898142052198890142052320114205232087
51420522031142052211281142052321014205232177
61420522115142052219883142052322114205232287
71420522359142052244182142052323114205232376
81420522488142052256981142052324314205232496
91420522576142052266690142052325214205232597
101420522771142052285584142052326214205232686

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.

0