初次体验hiphop-php

昨天facebook在github上发布了hiphop-php的源代码。之前听说这玩意能把php代码翻译成c++代码,然后带来巨大的性能提升,所以第一时间编译了一份hiphop-php。

我的机器环境是

  • Centos 5.3 x86_64
  • 8G内存
  • Intel(R) Xeon(R) CPU E5420 @ 2.50GHz

安装注意事项

编译的时候碰到的问题很多,但是基本上都是按照wiki上的步骤进行的。我觉得比较重要的几点:

  • wiki上的Required Packages包包列表都要检查一遍,比如版本号,是否安装过,像binutils-dev这种就很容易忽略
  • 版本符合的话,直接用yum安装这些包就可以了
  • wiki上有类似Boost 1.37 is the minimum version字样,说明开发者可能就是在这个版本下开发的,我试了下最新版本的boost,编译到后来反而出错
  • 如果yum上没有符合版本的lib库,可以手动编译,但是编译时建议就放在自己的home下,比如:
    ./configure --prefix=/home/user
  • tbb Intel’s Thread Building Blocks这个包有些麻烦,记得按照wiki上说的步骤安装

测试hiphop-php

安装完成之后,时间也不是太多,所以我仅仅是简单的测试了一个php文件,代码如下:

PHP:
  1. <?php
  2. $i = 0;
  3. for($j = 0; $j <1000000; $j++)
  4.         $i += $j;
  5.  
  6. echo $i, "\n";
  7. ?>

用hphp进行编译:

CODE:
  1. hphp/hphp test.php --keep-tempdir=1 --log=3

提示生成新的可执行文件

CODE:
  1. /tmp/hphp_c9sbnG/program

做一下运行时间对比:

CODE:
  1. $ time php test.php
  2. 499999500000
  3.  
  4. real    0m0.307s
  5. user    0m0.299s
  6. sys     0m0.007s
  7.  
  8. $ time /tmp/hphp_c9sbnG/program
  9. 499999500000
  10.  
  11. real    0m0.259s
  12. user    0m0.194s
  13. sys     0m0.008s

没看出来编译成c++代码之后有太大的性能提升,估计是俺的使用手法问题?在邮件组里观察几天再说。

Update

facebook将优化之后的编译参数提交到了github,于是我重新编译并测试一遍这段相同的代码:

CODE:
  1. $ time /tmp/hphp_c9sbnG/program
  2. 499999500000
  3.  
  4. real    0m0.140s
  5. user    0m0.076s
  6. sys     0m0.006s

可以看到,经hiphop编译后的php,执行时间几乎快了一倍。

作者: Volcano 发表于February 21, 2010 at 10:19 am

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

Tags: ,

20 条评论 »

  1. emptyhua 于 2010-02-21 @ 11:02:55 留言

    调用一下php库里的函数试试,这种纯运算应该看不出什么~

  2. xLight 于 2010-02-21 @ 12:29:53 留言

    你真够快的,我还在build过程中。。。
    都恨不得直接装个Ubuntu了,有完整的wiki说明

  3. riku 于 2010-02-21 @ 14:00:58 留言

    在 Freebsd 上编译简直是噩梦,还是在 Ubuntu 上快速了当。我准备转换整个 WordPress ,看看效果如何。

  4. 懒人 于 2010-02-21 @ 15:33:36 留言

    我也觉得这个对于简单的计算看不出来,
    最后编译成的机器命令无非是谁的行数多谁慢。之前还试过在嵌入式系统里把for 100的循环直接展开,写100遍,编译之后的计算都比用循环要快一点。

    对于一般的网站来说应该用处不大,瓶颈也不在PHP的运算上。

    不过如果大到像facebook这样的庞然大物的话,能优化一点点,乘上其规模也是很大的一块肉了。

  5. david 于 2010-02-21 @ 17:43:30 留言

    性能差异可能体现在include之类的动态特性上,简单的for循环应该是经过优化的

  6. ayanamist 于 2010-02-21 @ 21:05:35 留言

    其实在user时间块上已经有比较大的性能提升了,你可以看看百分比,高达1/3的时间,而且还只是这样的简单运算。在大量应用了OOP后,肯定性能提升将会相当明显的。PHP VM里对于函数调用和OOP上的性能损失是相当的大的

  7. 李博 于 2010-02-23 @ 11:35:07 留言

    可否给一个centos下build的过程呢。我到最后的cmake的时候有两个莫名其妙的warning:
    runtime library [libmcrypt.so.4] in /usr/lib64 may be hidden by files in:
    /usr/local/lib
    但是检查了没问题……make的时候就有两个error
    谢谢!

  8. Volcano 于 2010-02-23 @ 18:25:47 留言

    @李博 libmcrypt还是用yum安装吧,实在不行去下载一个,比如这里

  9. linsk 于 2010-03-28 @ 18:51:14 留言

    内存方面的信息呢?关注是否合适在VPS下使用

  10. jianzi 于 2010-04-29 @ 09:39:07 留言

    朋友,你能贴出编译过程么?
    我在CentOS5.3尝试了N次了,cmake过程全部通过了,但是make的时候会在结束的时候出现异常
    生成了hphp文件,但是hphpi生成错误

    而且经过测试hphp是不好用的,

  11. jianzi 于 2010-04-29 @ 09:40:08 留言

    [root@localhost hiphop-php-32b]# cmake .
    – The C compiler identification is GNU
    – The CXX compiler identification is GNU
    – Check for working C compiler: /usr/bin/gcc
    – Check for working C compiler: /usr/bin/gcc — works
    – Detecting C compiler ABI info
    – Detecting C compiler ABI info – done
    – Check for working CXX compiler: /usr/bin/c++
    – Check for working CXX compiler: /usr/bin/c++ — works
    – Detecting CXX compiler ABI info
    – Detecting CXX compiler ABI info – done
    – Boost version: 1.37.0
    – Found the following Boost libraries:
    – system
    – program_options
    – filesystem
    – MySQL Include dir: /usr/include library dir: /usr/lib/mysql
    – MySQL client libraries: mysqlclient
    – Found PCRE: /usr/local/lib/libpcre.so
    – Found libevent: /usr/lib/libevent.so
    – Looking for evhttp_bind_socket_with_fd
    – Looking for evhttp_bind_socket_with_fd – found
    – Looking for gdImagePng in /usr/lib/libgd.so
    – Looking for gdImagePng in /usr/lib/libgd.so – found
    – Found ZLIB: /usr/lib/libz.so
    – Found PNG: /usr/lib/libpng.so
    – Looking for gdImageJpeg in /usr/lib/libgd.so
    – Looking for gdImageJpeg in /usr/lib/libgd.so – found
    – Found JPEG: /usr/lib/libjpeg.so
    – Looking for gdImageGif in /usr/lib/libgd.so
    – Looking for gdImageGif in /usr/lib/libgd.so – found
    – Found GD: /usr/lib/libgd.so
    – Found CURL: /usr/lib/libcurl.so
    – Looking for curl_multi_select
    – Looking for curl_multi_select – found
    – checking for module ‘libxml-2.0′
    – found libxml-2.0, version 2.6.26
    – Found LibXml2: /usr/lib/libxml2.so
    – Found EXPAT: /usr/lib/libexpat.so
    – Found ICU header files in /usr/local/include
    – Found ICU libraries: /usr/local/lib/libicuuc.so
    – Found Intel TBB
    – Found mcrypt: /usr/lib/libmcrypt.so
    – Found OpenSSL: /usr/lib/libssl.so
    – Found ONIGURUMA: /usr/local/lib/libonig.so
    – Found BISON: /usr/local/bin/bison
    – Found FLEX: /usr/local/bin/flex
    – Found RE2C: /usr/local/bin/re2c
    – Looking for arpa/inet.h
    – Looking for arpa/inet.h – found
    – Looking for netinet/in.h
    – Looking for netinet/in.h – found
    – Looking for stddef.h
    – Looking for stddef.h – found
    – Looking for stdint.h
    – Looking for stdint.h – found
    – Looking for string.h
    – Looking for string.h – found
    – Looking for sys/socket.h
    – Looking for sys/socket.h – found
    – Looking for sys/time.h
    – Looking for sys/time.h – found
    – Looking for unistd.h
    – Looking for unistd.h – found
    – Looking for sys/types.h
    – Looking for sys/types.h – found
    – Looking for stdint.h
    – Looking for stdint.h – found
    – Looking for stddef.h
    – Looking for stddef.h – found
    – Check size of size_t
    – Check size of size_t – done
    – Check size of ssize_t
    – Check size of ssize_t – done
    – Check size of uint32_t
    – Check size of uint32_t – done
    – Check size of uint8_t
    – Check size of uint8_t – done
    – Looking for AF_LOCAL
    – Looking for AF_LOCAL – found
    – Looking for PF_LOCAL
    – Looking for PF_LOCAL – found
    – Looking for memset
    – Looking for memset – found
    – Looking for socket
    – Looking for socket – found
    – Looking for strerror
    – Looking for strerror – found
    – Found libevent: /usr/lib/libevent.so
    – Looking for event_get_version_number
    – Looking for event_get_version_number – not found.
    – Performing Test HAVE_LIBEVENT_145
    – Performing Test HAVE_LIBEVENT_145 – Success
    – Found libevent 1.4.5+
    – Looking for include files INCLUDE_CHECK_stdlib.h
    – Looking for include files INCLUDE_CHECK_stdlib.h – found
    – Looking for include files INCLUDE_CHECK_assert.h
    – Looking for include files INCLUDE_CHECK_assert.h – found
    – Looking for strcasecmp
    – Looking for strcasecmp – found
    – Looking for strchr
    – Looking for strchr – found
    – Downloading 8859-1.TXT
    – Downloading 8859-2.TXT
    – Downloading 8859-3.TXT
    – Downloading 8859-4.TXT
    – Downloading 8859-5.TXT
    – Downloading 8859-6.TXT
    – Downloading 8859-7.TXT
    – Downloading 8859-8.TXT
    – Downloading 8859-9.TXT
    – Downloading 8859-10.TXT
    – Downloading 8859-11.TXT
    – Downloading 8859-13.TXT
    – Downloading 8859-14.TXT
    – Downloading 8859-15.TXT
    – Downloading 8859-16.TXT
    – Downloading EastAsianWidth.txt
    – Configuring done
    – Generating done
    – Build files have been written to: /root/hiphop-32b-softs/hiphop-php-32b

    这是我的cmake过程,应该没有太大问题吧

  12. jianzi 于 2010-04-29 @ 09:42:36 留言

    [ 2%] [BISON][XHPParser] Building parser with bison 2.4.2
    [ 2%] [RE2C] Building re2c scanner with re2c 001305
    [ 3%] [FLEX][XHPScanner] Building scanner with flex 2.5.35
    Scanning dependencies of target xhp
    [ 3%] Building CXX object src/third_party/xhp/xhp/CMakeFiles/xhp.dir/code_rope.cpp.o
    在包含自 /root/hiphop-32b-softs/hiphop-php-32b/src/third_party/xhp/xhp/code_rope.cpp:17 的文件中:
    /root/hiphop-32b-softs/hiphop-php-32b/src/third_party/xhp/xhp/code_rope.hpp:17:20: 错误:ext/rope:没有那个文件或目录
    /root/hiphop-32b-softs/hiphop-php-32b/src/third_party/xhp/xhp/code_rope.hpp:18:32: 错误:ext/pool_allocator.h:没有那个文或目录

    到这个地方会出现错误!!!!!!!!!! 是跟flex有关系呢,还是跟xhp有关系?实在是不解

  13. Volcano 于 2010-04-29 @ 09:47:28 留言

    如果你是用32bit的centos,不能从facebook的git上获取代码,那会编译失败的。应该从这里获取代码。除此之外我的编译步骤都是按照它的wiki操作的,没有不同。

  14. jianzi 于 2010-04-29 @ 11:22:28 留言

    @Volcano 我checkout的hiphop-php版本就是32bit的,无论我怎么尝试都是在xhp这个地方出错,非常让人困惑,我感觉跟flex有关系,但是我的flex已经升级到2.5.35版本

    [ 13%] [BISON][XHPParser] Building parser with bison 2.4.1
    [ 13%] [RE2C] Building re2c scanner with re2c 001305
    [ 14%] [FLEX][XHPScanner] Building scanner with flex 2.5.35
    Scanning dependencies of target xhp
    [ 14%] Building CXX object src/third_party/xhp/xhp/CMakeFiles/xhp.dir/code_rope.cpp.o
    在包含自 /root/hiphop-32b-softs/hiphop-php-32b/src/third_party/xhp/xhp/code_rope.cpp:17 的文件中:
    /root/hiphop-32b-softs/hiphop-php-32b/src/third_party/xhp/xhp/code_rope.hpp:17:20: 错误:ext/rope:没有那个文件或目录
    /root/hiphop-32b-softs/hiphop-php-32b/src/third_party/xhp/xhp/code_rope.hpp:18:32: 错误:ext/pool_allocator.h:没有那个文或目录

  15. jianzi 于 2010-05-04 @ 17:34:05 留言

    你好博主,我花费了很多时间去编译hiphop-php,总是出现一些莫名其妙的错误
    不知道你编译的时候是否碰到这个错误呢,在make到最后的时候,hphp已经生成,但是在生成hphpi的时候,却提示一些关于libiconv的错误
    libiconv我已经装了,我找了很多资料都没有相关的文章,不知道博主是否能给一点点提示,先谢了:)

    Linking CXX static library ../../bin/libhphp_analysis.a
    [ 87%] Built target hphp_analysis
    [ 87%] Building CXX object src/hphp/CMakeFiles/hphp.dir/externals.cpp.o
    [ 87%] Building CXX object src/hphp/CMakeFiles/hphp.dir/main.cpp.o
    Linking CXX executable hphp
    Building hphpi
    /root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `HPHP::_php_iconv_strlen(unsigned int*, char const*, unsigned int, char const*)’:\next_iconv.cpp:(.text+0x1d3): undefined reference to `libiconv_open’\next_iconv.cpp:(.text+0×268): undefined reference to `libiconv’\next_iconv.cpp:(.text+0x2a5): undefined reference to `libiconv_close’\n/root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `HPHP::php_iconv_string(char const*, unsigned int, char**, unsigned int*, char const*, char const*)’:\next_iconv.cpp:(.text+0x33a): undefined reference to `libiconv_open’\next_iconv.cpp:(.text+0×420): undefined reference to `libiconv’\next_iconv.cpp:(.text+0×490): undefined reference to `libiconv’\next_iconv.cpp:(.text+0x4a8): undefined reference to `libiconv_close’\next_iconv.cpp:(.text+0x4b7): undefined reference to `libiconv_close’\next_iconv.cpp:(.text+0x4f0): undefined reference to `libiconv_close’\n/root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `HPHP::_php_iconv_strpos(unsigned int*, char const*, unsigned int, char const*, unsigned int, int, char const*)’:\next_iconv.cpp:(.text+0x5d3): undefined reference to `libiconv_open’\next_iconv.cpp:(.text+0×660): undefined reference to `libiconv’\next_iconv.cpp:(.text+0x74f): undefined reference to `libiconv_close’\n/root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `HPHP::_php_iconv_appendl(HPHP::StringBuffer&, char const*, unsigned int, void*)’:\next_iconv.cpp:(.text+0x9ca): undefined reference to `libiconv’\next_iconv.cpp:(.text+0xaac): undefined reference to `libiconv’\n/root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `HPHP::_php_iconv_mime_decode(HPHP::StringBuffer&, char const*, unsigned int, char const*, char const**, int)’:\next_iconv.cpp:(.text+0xb29): undefined reference to `libiconv_open’\n/root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `.L298′:\next_iconv.cpp:(.text+0xf0d): undefined reference to `libiconv_close’\next_iconv.cpp:(.text+0xf1b): undefined reference to `libiconv_close’\n/root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `.L299′:\next_iconv.cpp:(.text+0×1276): undefined reference to `libiconv_close’\next_iconv.cpp:(.text+0x128b): undefined reference to `libiconv_open’\n/root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `HPHP::f_iconv_mime_encode(HPHP::String const&, HPHP::String const&, HPHP::Variant const&)’:\next_iconv.cpp:(.text+0×2835): undefined reference to `libiconv_close’\next_iconv.cpp:(.text+0×2915): undefined reference to `libiconv_open’\next_iconv.cpp:(.text+0×2950): undefined reference to `libiconv_open’\next_iconv.cpp:(.text+0×3169): undefined reference to `libiconv’\next_iconv.cpp:(.text+0x31a7): undefined reference to `libiconv’\next_iconv.cpp:(.text+0x31f0): undefined reference to `libiconv’\next_iconv.cpp:(.text+0×3257): undefined reference to `libiconv_close’\next_iconv.cpp:(.text+0×3514): undefined reference to `libiconv’\next_iconv.cpp:(.text+0x354f): undefined reference to `libiconv’\next_iconv.cpp:(.text+0×3773): undefined reference to `libiconv’\n/root/software/hiphop-php-32b/bin/libhphp_runtime.a(ext_iconv.cpp.o): In function `HPHP::f_iconv_substr(HPHP::String const&, int, int, HPHP::String const&)’:\next_iconv.cpp:(.text+0x40dc): undefined reference to `libiconv_open’\next_iconv.cpp:(.text+0×4172): undefined reference to `libiconv’\next_iconv.cpp:(.text+0×4218): undefined reference to `libiconv_close’\next_iconv.cpp:(.text+0x422e): undefined reference to `libiconv_close’\next_iconv.cpp:(.text+0x428e): undefined reference to `libiconv_open’\ncollect2: ld returned 1 exit status\nmake[5]: *** [program] Error 1\nmake[4]: *** [CMakeFiles/program.dir/all] Error 2\nmake[3]: *** [all] Error 2\n
    mv: cannot stat `gen/program’: No such file or directory
    make[2]: *** [src/hphp/hphp] Error 1
    make[1]: *** [src/hphp/CMakeFiles/hphp.dir/all] Error 2
    make: *** [all] Error 2

  16. Volcano 于 2010-05-04 @ 20:51:17 留言

    我编译成功的环境有:

    • centos 64bit
    • ubuntu 32bit

    不知道你现在的操作系统是什么。建议你装个Ubuntu或者centos 64,都有现成的安装步骤,32位的还不算正式支持,所以出现问题也很正常。

  17. jianzi 于 2010-05-05 @ 09:07:40 留言

    我用的是CentOS5.3 32位

  18. wisheslight 于 2010-05-10 @ 23:49:40 留言

    个人觉得,Hip-hop没有找到重点,因为PHP的运算开销,比起读一次memcache来说,真是微不足道的。

  19. hello 于 2010-06-08 @ 17:50:59 留言

    你好,麻烦问下

    facebook将优化之后的编译参数提交到了github,于是我重新编译并测试一遍这段相同的代码:

    这段优化之后的编译参数在哪里可以找到?

    我编译后执行的结果并没有比纯用PHP跑得快。

  20. Volcano 于 2010-06-08 @ 21:28:51 留言

    你现在下载最新的hiphop-php的源码,编译参数已经是优化之后的了。

    一般的普通运算并不会比php快,但是如果有n个class的php页面,执行速度是有显著提升的。

RSS 为此帖反馈评论

留条评论