Facebook开源项目Hiphop-php使用介绍(原创)
作者:余超 EMail:yuchao86@gmail.com
Facebook开源的Hiphop-php能将php编译成C++程序,提高站点的运行效率,在github上有最新的hiphop-php的源代码。
如下是Hiphop-php的作者Haiping Zhao(赵海平)对Hiphop的定义:
HipHop是一个源代码转换器。它将PHP代码转换为高度优化的C++代码,然后再使用g++编译器编译。它可以保持语义等效地执行源代码,但牺牲了一些很少会使用到的功能,比如 eval()。为了进一步的提升性能,HipHop包含一个code transformer,一个重新实现的PHP runtime系统,并利用这些性能的优化的优势,对许多共同的PHP扩展进行了重写。
编译的时候碰到的很多问题,中间还有很多包需要装,但基本上是按照wiki的步骤进行的:
=====================================================================================
1. 安装关联库,在编译过程中还发现有其它包需要安装请使用命令sudo yum -y install **安装:
[yuchao@yuchao-Latitude-E5410 hiphop-php]$ls
bin CMake CMakeCache.txt CMakeFiles CMakeLists.txt configure doc LICENSE.PHP LICENSE.ZEND local phpt README.md src
[yuchao@yuchao-Latitude-E5410 hiphop-php]$pwd
/home/yuchao/dev/hiphop-php
[yuchao@yuchao-Latitude-E5410 hiphop-php]$sudo yum -y install git cmake boost pcre-devel libicu-devel\
libmcrypt-devel oniguruma-devel mysql-devel gd-devel boost-devel\
libxml2-devel libcap-devel binutils-devel flex bison\
expat-devel
=====================================================================================
2. 下载Hiphop-php安装包,以及依赖库libevent, curl,从二进制可执行文件能够运行请求都依赖于这两个库。
1) 获取Hiphop-php源代码.
[yuchao@yuchao-Latitude-E5410 hiphop-php]$git clone git://github.com/facebook/hiphop-php.git
2) 获取编译所需要的各种库,re2c,tbb,curl和libevent等等。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$wget "http://downloads.sourceforge.net/project/re2c/re2c/0.13.5/re2c-0.13.5.tar.gz?use_mirror=cdnetworks-us-2"
[yuchao@yuchao-Latitude-E5410 hiphop-php]$wget "http://www.threadingbuildingblocks.org/uploads/77/142/2.2/tbb22_20090809oss_src.tgz"
[yuchao@yuchao-Latitude-E5410 hiphop-php]$wget "http://curl.haxx.se/download/curl-7.20.0.tar.bz2"
[yuchao@yuchao-Latitude-E5410 hiphop-php]$wget "http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz"
3. 解压缩以上下载的各种依赖包,gz包用xvzf而bz2包用xvjf
[yuchao@yuchao-Latitude-E5410 hiphop-php]$tar xvjf curl-7.20.0.tar.bz2
[yuchao@yuchao-Latitude-E5410 hiphop-php]$tar xvzf libevent-1.4.13-stable.tar.gz
[yuchao@yuchao-Latitude-E5410 hiphop-php]$tar xvzf re2c-0.13.5.tar.gz
[yuchao@yuchao-Latitude-E5410 hiphop-php]$tar xvzf tbb22_20090809oss_src.tgz
4. 安装解压出来的各种依赖包,编译过程中如果出现错误,请自己根据提示信息拍错,那样你学到的东西更多。
1) 安装Intel并行线程库
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd tbb22_20090809oss
[yuchao@yuchao-Latitude-E5410 hiphop-php]$gmake
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cp ./build/*_release/*.so.* /usr/lib/
[yuchao@yuchao-Latitude-E5410 hiphop-php]$ldconfig
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd ..
2) 安装比lex更有效率的re2c嵌入式的词法解析工具.
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd re2c-0.13.5
[yuchao@yuchao-Latitude-E5410 hiphop-php]$mkdir -p /usr/local/re2c
[yuchao@yuchao-Latitude-E5410 hiphop-php]$./configure –prefix=/usr/local/re2c
[yuchao@yuchao-Latitude-E5410 hiphop-php]$make && make install
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd ..
3) 安装用于处理网络IO,timer,signal的事件触发机制处理库libevent,
可以参考我的另一篇文章《libevent源码分析》http://blog.csdn.net/cyberexp2008/article/details/5674693
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd libevent-1.4.13-stable
必须用hiphop-php/src/third_party/目录下对应libevent版本号打好补丁。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cp ../hiphop-php/src/third_party/libevent-1.4.13.fb-changes.diff ./
[yuchao@yuchao-Latitude-E5410 hiphop-php]$patch -p1 < libevent-1.4.13.fb-changes.diff
[yuchao@yuchao-Latitude-E5410 hiphop-php]$./configure --prefix=/usr/local/libevent/
[yuchao@yuchao-Latitude-E5410 hiphop-php]$make && make install
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd ..
4) 安装用于处理URL语法在命令行方式下工作的文件传输工具CURL,如需了解更多请自己Google之。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd curl-7.20.0
同样必须用hiphop-php/src/third_party/目录下对应libcurl版本号打好补丁。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cp ../hiphop-php/src/third_party/libcurl.fb-changes.diff ./
[yuchao@yuchao-Latitude-E5410 hiphop-php]$patch -p1 < libcurl.fb-changes.diff
[yuchao@yuchao-Latitude-E5410 hiphop-php]$./configure --prefix=/usr/local/curl
[yuchao@yuchao-Latitude-E5410 hiphop-php]$make && make install
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd ..
==========================================================================================
5. 接下来就开始安装hiphop-php,在正式编译之前,需要设置CMAKE_PREFIX_PATH,HPHP_HOME和HPHP_LIB环境变量
1) 设置环境变量,cmake查找libevent与curl时的路径,如果设置不正确,我在编译的过程中提示CMake Error: The following variables are used in this project, but they are set to NOTFOUND.Please set them or make sure they are set and tested correctly in the CMake files:
CCLIENT_INCLUDE_PATH (ADVANCED)的错误信息,请自己排错。
[yuchao@yuchao-Latitude-E5410 hiphop-php]$export CMAKE_PREFIX_PATH=/usr/local/libevent/
[yuchao@yuchao-Latitude-E5410 hiphop-php]$export CMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH:/usr/local/curl/
2) 安装hiphop-php
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cd hiphop-php
[yuchao@yuchao-Latitude-E5410 hiphop-php]$git submodule init
[yuchao@yuchao-Latitude-E5410 hiphop-php]$git submodule update
[yuchao@yuchao-Latitude-E5410 hiphop-php]$export HPHP_HOME=`pwd`
[yuchao@yuchao-Latitude-E5410 hiphop-php]$export HPHP_LIB=`pwd`/bin
[yuchao@yuchao-Latitude-E5410 hiphop-php]$cmake .
[yuchao@yuchao-Latitude-E5410 hiphop-php]$make
===============================================================================
安装完成之后,我们就可以开始使用hphp命令了,下面来简单的测试一个php文件,代码如下:
运行php编译文件参考了文章如下:
https://github.com/facebook/hiphop-php/wiki/Running-HipHop
<?php
$i = 0;
for($j = 0; $j < 1000000; $j++)
$i += $j;
echo $i, "n";
?>
用hphp进行编译: [yuchao@yuchao-Latitude-E5410 hiphop-php]$hphp test.php --keep-tempdir=1 --log=3
提示生成新的可执行文件
[yuchao@yuchao-Latitude-E5410 hiphop-php]$/tmp/hphp_c9sbnG/program
做一下运行时间对比:
[yuchao@yuchao-Latitude-E5410 hiphop-php]$time php test.php
499999500000
real 0m0.307s
user 0m0.299s
sys 0m0.007s
[yuchao@yuchao-Latitude-E5410 hiphop-php]$time /tmp/hphp_c9sbnG/program
499999500000
real 0m0.140s
user 0m0.076s
sys 0m0.006s
可以看到,经hiphop编译后的php,执行时间几乎快了一倍。特此声明
参考文章:
[1]. http://www.ioncannon.net/program ... and-32-bit-systems/
[2]. https://github.com/facebook/hiph ... ding-and-installing
[3]. https://github.com/facebook/hiph ... via-RPM-on-CentOS-5
|