高性能网站,HipHop php使用说明

钮勇
2023-12-01
这里的HipHop,不是嘻哈乐,我不喜欢嘻哈。

HipHop是Facebook开源的php编译器,可以将php转换成C++再编译成可执行文件,用以提高php的执行效率。

安装过程比较复杂,依赖的东西很多。Boost,Onigumura,tbb,icu,mysql,flex,re2c等等,还有facebook补丁版的libevent和curl。

其实facebook开源两大网站利器,hiphop和scribe,按照科学的哲学思想,都属于伪科学,比较难安装和编译,安装编译过程基本不可重现,给人感觉是在用户在不修改源码的情况下能装上都属于蒙的。So you know that

所以,对于没有经验的用户,按照官方指引,也只能说有一定几率可以装上。

因此,就不用把安装过程复现出来了,即使我写出来,按照步骤也不一定能装的上。Facebook官方有在ubuntu和centos下的安装指南。还是很详细的,感兴趣的可以参照官方的安装指南尝试一下。不过官方指南里面少了一个安装libunwind库。

先写一段程序测试一下hiphop编译后的php性能

<?php
function BubbleSort($str)
{
                for ($i=0;$i<count($str);$i++)
                {
                                for ($j=count($str)-2;$j>=$i;$j--)
                                {
                                                if($str[$j+1]<$str[$j])
                                                {            
                                                                $tmp = $str[$j+1];
                                                                $str[$j+1]=$str[$j];
                                                                $str[$j]=$tmp;
                                                }
                                }
                }
                return $str;
}

for($i = 0;$i < 10000;$i++)
{
                $str[$i] = rand(0,20000);
}
BubbleSort($str);
echo "finished";
?>

生成10000个0-20000之间的随机数,然后冒泡排序。因为冒泡的效率比较低,可以比较直观的看出编译前和编译后的执行效率差别。

用hiphop编译

[root@localhost scribe]# hphp --keep-tempdir=1 --log=3 random.php    
running hphp...
creating temporary directory /tmp/hphp_4DOImV ...
parsing inputs...
parsing ./random.php...
parsing inputs took 0'00" (2 ms) wall time
pre-optimizing...
pre-optimizing took 0'00" (0 ms) wall time
inferring types...
inferring types took 0'00" (0 ms) wall time
post-optimizing...
post-optimizing took 0'00" (0 ms) wall time
creating CPP files...
creating CPP files took 0'00" (35 ms) wall time
compiling and linking CPP files...

compiling and linking CPP files took 0'35" (35732 ms) wall time
running executable /tmp/hphp_4DOImV/program --file random.php...
finishedall files saved in /tmp/hphp_4DOImV ...
running hphp took 0'42" (42126 ms) wall time

--keep-tempdir=1的意思是生成C++代码并编译后,临时目录不删除。--log=3是控制台输出日志的等级,4是最详细的notice都输出。最大就到4。

编译完成,路径在/tmp/hphp_4DOImV。

先不着急看生成文件,先看看原本php的执行效率如何,执行3次,估算一下平均时间。

[root@localhost scribe]# time php random.php
finished
real        0m14.776s
user        0m14.737s
sys         0m0.024s
[root@localhost scribe]# time php random.php
finished
real        0m14.801s
user        0m14.744s
sys         0m0.047s
[root@localhost scribe]# time php random.php
finished
real        0m14.787s
user        0m14.734s
sys         0m0.037s

然后到/tmp/hphp_4DOImV下

[root@localhost hphp_4DOImV]# ls
CMakeCache.txt    CMakeFiles    cmake_install.cmake    CMakeLists.txt    Makefile    php    program    sys
[root@localhost hphp_4DOImV]# time ./program
finished
real        0m6.562s
user        0m6.471s
sys         0m0.068s
[root@localhost hphp_4DOImV]# time ./program
finished
real        0m6.100s
user        0m6.067s
sys         0m0.012s
[root@localhost hphp_4DOImV]# time ./program
finished
real        0m6.107s
user        0m6.060s
sys         0m0.027s

Wow,看起来非常不错,从大约15秒的执行时间,编译后大约在6秒左右。性能提升还是非常可观的。对于一些压力大的网站,还是非常合适的。

然后看下怎么在web环境下使用。hiphop是这样,编译完成之后,可执行文件本身就自带了一个webserver,所以,我们只需要让可执行文件当成一个webserver启动就可以了,然后用nginx做反代,nginx反代就不说怎么配置了。就说启动hiphop编译后的程序就行了。我们先看看这个可执行文件提供什么选项。

[root@localhost hphp_4DOImV]# ./program --help
Usage:

                ./program [-m <mode>] [<options>] [<arg1>] [<arg2>] ...

Options:
    --help                                                    display this message
    -m [ --mode ] arg (=run)                run | server | daemon | replay | translate
    -c [ --config ] arg                         load specified config file
    -v [ --config-value ] arg             individual configuration string in a format
                                                                    of name=value, where name can be any valid
                                                                    configuration for a config file
    -p [ --port ] arg (=-1)                 start an HTTP server at specified port
    --admin-port arg (=-1)                    start admin listerner at specified port
    -u [ --user ] arg                             run server under this user account
    -f [ --file ] arg                             executing specified file
    --count arg (=1)                                how many times to repeat execution
    --no-safe-access-check arg (=0) whether to ignore safe file access check
    --arg arg                                             arguments
    --extra-header arg                            extra-header to add to log lines
    --build-id arg                                    unique identifier of compiled server code

这里面,-m选项和-p选项就是我们启动web服务需要用的。
作为调试,我们可以这样。

[root@localhost hphp_4DOImV]# ./program -m server -p 8080
loading static content...
loading static content took 0'00" (0 ms) wall time
page server started
admin server started
all servers started

然后我们用curl访问一下看看,注意,你编译前php叫什么名字,编译后运行在webserver里面还叫什么。

[root@localhost ~]# curl -i http://localhost:8080/random.php
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Date: Mon, 13 Aug 2012 10:39:09 GMT
Content-Length: 8

finished

在生产环境,可以把-m server选项替换为-m daemon,就启动后台进程了。

然后把你的nginx反代到8080端口,就可以正常在web环境下访问了。

有几个注意事项,网上文章里面基本没有提及。

1. hiphop编译后,可以把可执行文件抓下来,单独扔到与编译机操作系统相同的环境下运行。但是,运行环境需要配置安装hiphop时所需要的相同的boost, re2c, oniguruma, mysql, gd等等动态连接库。

2. eval函数不能用。socket相关函数,abstract,interface等,可能带来转换的Segmentation Fault错误,导致无法转换编译,不过是可能,不是必然。

3. 整站php编译后也都在一个可执行文件中。运行daemon后,你原来怎么访问,现在还是怎么访问,文件名,连接都不变。

4. 任何include或者require,不能用相对连接,必须用操作系统的绝对连接。否则会提示找不到文件。

5. 不要尝试编译phpinfo(),你什么都得不到。

6. 生成的C++可读性很好。

最后,愿上帝保佑你安装成功。
 类似资料: