Press "Enter" to skip to content

让你的PHP7更快(GCC PGO)

我们一直致力于提升PHP7的性能, 上个月我们注意到GCC的PGO能在Wordpress上能带来近10%的性能提升, 这个让我们很激动.
然而, PGO正如名字所说(Profile Guided Optimization 有兴趣的可以Google), 他需要用一些用例来获得反馈, 也就是说这个优化是需要和一个特定的场景绑定的.
你对一个场景的优化, 也许在另外一个场景就事与愿违了. 它不是一个通用的优化. 所以我们不能简单的就包含这些优化, 也无法直接发布PGO编译后的PHP7.
当然, 我们正在尝试从PGO找出一些共性的优化, 然后手工Apply到PHP7上去, 但这个很明显不能做到针对一个场景的特别优化所能达到的效果, 所以我决定写这篇文章简单介绍下怎么使用PGO来编译PHP7, 让你编译的PHP7能特别的让你自己的独立的应用变得更快.
首先, 要决定的就是拿什么场景去Feedback GCC, 我们一般都会选择: 在你要优化的场景中: 访问量最大的, 耗时最多的, 资源消耗最重的一个页面.
拿Wordpress为例, 我们选择Wordpress的首页(因为首页往往是访问量最大的).
我们以我的机器为例:

       Intel(R) Xeon(R) CPU           X5687  @ 3.60GHz X 16(超线程),
       48G Memory

php-fpm 采用固定32个worker, opcache采用默认的配置(一定要记得加载opcache)
以wordpress 4.1为优化场景..
首先我们来测试下目前WP在PHP7的性能(ab -n 10000 -c 100):

$ ab -n 10000 -c 100 http://inf-dev-maybach.weibo.com:8000/wordpress/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking inf-dev-maybach.weibo.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software:        nginx/1.7.12
Server Hostname:        inf-dev-maybach.weibo.com
Server Port:            8000
Document Path:          /wordpress/
Document Length:        9048 bytes
Concurrency Level:      100
Time taken for tests:   8.957 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      92860000 bytes
HTML transferred:       90480000 bytes
Requests per second:    1116.48 [#/sec] (mean)
Time per request:       89.567 [ms] (mean)
Time per request:       0.896 [ms] (mean, across all concurrent requests)
Transfer rate:          10124.65 [Kbytes/sec] received

可见Wordpress 4.1 目前在这个机器上, 首页的QPS可以到1116.48. 也就是每秒钟可以处理这么多个对首页的请求,
现在, 让我们开始教GCC, 让他编译出跑Wordpress4.1更快的PHP7来, 首先要求GCC 4.0以上的版本, 不过我建议大家使用GCC-4.8以上的版本(现在都GCC-5.1了).
第一步, 自然是下载PHP7的源代码了, 然后做./configure. 这些都没什么区别
接下来就是有区别的地方了, 我们要首先第一遍编译PHP7, 让它生成会产生profile数据的可执行文件:

$ make prof-gen

注意, 我们用到了prof-gen参数(这个是PHP7的Makefile特有的, 不要尝试在其他项目上也这么搞哈 :))
然后, 让我们开始训练GCC:

$ sapi/cgi/php-cgi -T 100 /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/null

也就是让php-cgi跑100遍wordpress的首页, 从而生成一些在这个过程中的profile信息.
然后, 我们开始第二次编译PHP7.

$ make prof-clean
$ make prof-use && make install

好的, 就这么简单, PGO编译完成了, 现在我们看看PGO编译以后的PHP7的性能:

$ ab -n10000 -c 100 http://inf-dev-maybach.weibo.com:8000/wordpress/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking inf-dev-maybach.weibo.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software:        nginx/1.7.12
Server Hostname:        inf-dev-maybach.weibo.com
Server Port:            8000
Document Path:          /wordpress/
Document Length:        9048 bytes
Concurrency Level:      100
Time taken for tests:   8.391 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      92860000 bytes
HTML transferred:       90480000 bytes
Requests per second:    1191.78 [#/sec] (mean)
Time per request:       83.908 [ms] (mean)
Time per request:       0.839 [ms] (mean, across all concurrent requests)
Transfer rate:          10807.45 [Kbytes/sec] received

现在每秒钟可以处理1191.78个QPS了, 提升是~7%. 还不赖哈(咦, 你不是说10%么? 怎么成7%了? 呵呵, 正如我之前说过, 我们尝试分析PGO都做了些什么优化, 然后把一些通用的优化手工Apply到PHP7中. 所以也就是说, 那~3%的比较通用的优化已经包含到了PHP7里面了, 当然这个工作还在继续).
于是就这么简单, 大家可以用自己的产品的经典场景来训练GCC, 简单几步, 获得提升, 何乐而不为呢 🙂
thanks

59 Comments

  1. Birla Trimaya
    Birla Trimaya July 14, 2023

    Birla Trimaya developed by Birla Estates, is an esteemed residential project situated in North Bangalore, Karnataka, India. The project is strategically located in a rapidly developing area, offering a perfect blend of urban conveniences and serene surroundings. Birla Trimaya benefits from its prime location in North Bangalore, which is known for its excellent connectivity, robust infrastructure and entertainment options.

  2. Sobha Townpark
    Sobha Townpark June 24, 2023

    Sobha Town Park is the brand new New Apartment project launched in Sobha Town Park, Hosur Road, Attibele, Yadavanahalli, Bangalore.The massive residential enclave Manhattan Towers inside Sobha Town Park spread out across 32 acres features the very best in Sobha Limited’s luxury high-rise living segment.

  3. Birla Arnaa
    Birla Arnaa June 24, 2023

    Birla Arnaa is Loacated in Shettigere, Devanahalli, Bangalore. The ideal residence for urban homebuyers, these bright and airy residences are designed with the most available space. We understand that no two properties or two clients are alike. The property is situated in Shettigere, Devanahalli, Bangalore.

  4. Prestige Sunset Park
    Prestige Sunset Park June 16, 2023

    Prestige Sunset Park is a wonderful enclave of exclusive homes. The perfect address for those who wish to beat the daily commute to work and yet be insulated from the bustle of urban life. Enjoy the luxury of a fully equipped clubhouse with the gamut of recreational amenities including a swimming pool, gym, health club, indoor badminton court, ect.

  5. naza619
    naza619 May 24, 2022

    We are the number 1 mobile online slots service provider. Support both IOS and Android. No need to download to waste internetnaza619

  6. bet game tv
    bet game tv April 29, 2022

    I’ve read your article, it’s really good, you have a really creative idea. It’s all interesting.

  7. Megadede
    Megadede December 17, 2019

    衷心感谢鸟哥的文章!

  8. mapquest directions
    mapquest directions September 30, 2019

    Thanks for the information your post brings. I see the novelty in your text, I will share it for everyone to read. I look forward to reading more articles from you.

  9. driving directions
    driving directions September 3, 2019

    Thank you for providing this great information, I am not impressed with your post.

  10. 明月登楼
    明月登楼 August 14, 2019

    我是LNMP环境,虽然多次尝试GCC PGO都失败了,但还是要给文章点个赞!

  11. Andy
    Andy July 21, 2019

    Best Idea
    Thanks for your information..

  12. fdsfds
    fdsfds December 29, 2018

    gsfsdf

  13. […] 我之前的文章: 让你的PHP7更快(GCC PGO) 也介绍过, 如果你的PHP是专门为一个项目服务, 比如只是为你的Wordpress, 或者drupal, 或者其他什么, 那么你就可以尝试通过PGO, 来提升PHP, 专门为你的这个项目提高性能. […]

  14. […] 让你的PHP7更快(GCC PGO) 建议先训练一下 GCC 再编译针对 WordPress 的定制化 PHP7 版本似乎也值得采纳。但是,由于服务器资源严重不足,基本上所有 WordPress 页面都不是即时生成的,而是走了时效为半天的 WP Super Cache 缓存。所以,生成 WordPress 首页的速度提升 7-10% 对本例的服务器来说意义很有限,所以也没有做了。 […]

  15. […] 我之前的文章: 让你的PHP7更快(GCC PGO) 也介绍过, 如果你的PHP是专门为一个项目服务, 比如只是为你的Wordpress, 或者drupal, 或者其他什么, 那么你就可以尝试通过PGO, 来提升PHP, 专门为你的这个项目提高性能. […]

  16. Wy Zhang
    Wy Zhang August 8, 2018

    针对特定项目场景训练GCC,那对项目的要求就是以后不要有太大的变动,如果后期变动比较大的话,那就需要重新训练;不过确实是一种性能压榨的思路,赞!

  17. […] 我之前的文章: 让你的PHP7更快(GCC PGO) 也介绍过, 如果你的PHP是专门为一个项目服务, 比如只是为你的Wordpress, 或者drupal, 或者其他什么, 那么你就可以尝试通过PGO, 来提升PHP, 专门为你的这个项目提高性能. […]

  18. zifeng
    zifeng December 17, 2016

    高写入并发,这种貌似没用, 还有大部分不做脚本本身的优化,比如sql优化,不只是看看是否使用索引,还要考虑锁问题.然后就直接挂上缓存,造成大部分程序找不到问题根源. 建议还是先从问题本身出发

  19. cleey
    cleey September 2, 2016

    ab -n 10000 -c100 不是100个并发么,-c1000是不是更精确点

  20. Krystal
    Krystal May 4, 2016

    It really is rare to encounter a prnoessifoal in whom you will surely have some faith. In the world nowadays, nobody absolutely cares about showing others the way out in this matter. How happy I am to have now found a real wonderful web-site as this. It’s people like you who really make a genuine difference currently through the strategies they share.

  21. anru
    anru March 11, 2016

    2:php7在对for循环处理一些数据量较大的任务时内存溢出的问题是否有优化
    这个能说一下是什么意思吗?

  22. 袁源
    袁源 February 14, 2016

    已经开启了 WP Super Cache 了,没啥必要了。感觉贫民服务器还是缓存为王。

  23. coach factory
    coach factory December 12, 2015

    Finding the motivation to do something you don’t feel like doing at the moment, even though you know you should, is a challenge that many people face. I’m sure you’ve had the experience where even though you didn’t feel like taking action, you did so anyway and after you were done, you felt energized or even proud of yourself. People who get themselves to work out despite feeling lazy often experience this.
    coach factory http://coachhandbagssale.hobo2015.com/

  24. ExplorePress
    ExplorePress November 11, 2015

    追求永无止境的效率!

  25. Jenner
    Jenner October 19, 2015

    貌似啥也不用干就能带来这么高的性能提升~赞

  26. phping
    phping September 17, 2015

    顶一个期待php7的大面积应用

  27. RDY
    RDY September 7, 2015

    期待。。。

  28. Kermit
    Kermit September 1, 2015

    顶,期待php7的大量采用,现在行业里还有很多采用php5.2的,有没有办法让他们都去升个级呢。

  29. dsphper
    dsphper August 18, 2015

    哈哈,不错,支持鸟哥!

  30. 啊宅
    啊宅 August 4, 2015

    好长啊!!终于看完了

  31. elick
    elick July 29, 2015

    支持鸟哥

  32. space
    space July 21, 2015

    衷心感谢鸟哥的文章!

  33. aureolex
    aureolex July 14, 2015

    不得不说php7非常令人期待,但是鸟哥,在下有几个问题:
    1:php7在对较多网络请求任务会导致的fpm拥堵的情况是否有优化
    2:php7在对for循环处理一些数据量较大的任务时内存溢出的问题是否有优化

  34. yesins
    yesins July 11, 2015

    这是优化到死,不得不说,我很喜欢!!!

  35. jatvsjat
    jatvsjat July 10, 2015

    详细php7会更好用! 期待

  36. baibing
    baibing July 6, 2015

    这个php 新特性真的令人兴奋呀~

  37. kn007
    kn007 June 21, 2015

    居然还可以这样,学习了

  38. 陈佳
    陈佳 June 19, 2015

    可以针对比较专业的场景进行编译优化,这个方案,有机会试试看。

  39. Anonymous
    Anonymous June 19, 2015

    看完。

  40. kenfang
    kenfang June 19, 2015

    我的沙发呢

  41. Anonymous
    Anonymous June 19, 2015

    好像应用不到这个场景啊。。。。

  42. agx
    agx June 19, 2015

    沙发也看完。

  43. agx
    agx June 19, 2015

    沙发

  44. 坑长
    坑长 June 19, 2015

    沙发看完。

Comments are closed.