>백엔드 개발 >PHP 튜토리얼 >让你的 PHP 7 更快 (GCC PGO)_PHP教程

让你的 PHP 7 更快 (GCC PGO)_PHP教程

WBOY
WBOY원래의
2016-07-13 09:48:411434검색

让你的 PHP 7 更快 (GCC PGO)

我们一直致力于提升PHP7的性能,  上个月我们注意到GCC的PGO能在Wordpress上能带来近10%的性能提升,  这个让我们很激动.

 让你的 PHP 7 更快 (GCC PGO)

然而,  PGO正如名字所说(Profile Guided Optimization 有兴趣的可以Google), 他需要用一些用例来获得反馈, 也就是说这个优化是需要和一个特定的场景绑定的.

你对一个场景的优化, 也许在另外一个场景就事与愿违了.  它不是一个通用的优化. 所以我们不能简单的就包含这些优化, 也无法直接发布PGO编译后的PHP7.

当然, 我们正在尝试从PGO找出一些共性的优化,  然后手工Apply到PHP7上去, 但这个很明显不能做到针对一个场景的特别优化所能达到的效果,  所以我决定写这篇文章简单介绍下怎么使用PGO来编译PHP7, 让你编译的PHP7能特别的让你自己的独立的应用变得更快.

首先,  要决定的就是拿什么场景去Feedback GCC,  我们一般都会选择: 在你要优化的场景中: 访问量最大的, 耗时最多的, 资源消耗最重的一个页面.

拿Wordpress为例,  我们选择Wordpress的首页(因为首页往往是访问量最大的).

我们以我的机器为例:

<ol class="dp-c"><li class="alt"><span><span>Intel(R) Xeon(R) CPU           X5687  @ 3.60GHz X 16(超线程), </span></span></li><li><span> </span></li><li class="alt"><span>48G Memory </span></li></ol>

php-fpm 采用固定32个worker, opcache采用默认的配置(一定要记得加载opcache)

以wordpress 4.1为优化场景..

首先我们来测试下目前WP在PHP7的性能(ab -n 10000 -c 100):

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

可见Wordpress 4.1 目前在这个机器上, 首页的QPS可以到1116.48. 也就是每秒钟可以处理这么多个对首页的请求,

现在, 让我们开始教GCC, 让他编译出跑Wordpress4.1更快的PHP7来,  首先要求GCC 4.0以上的版本, 不过我建议大家使用GCC-4.8以上的版本(现在都GCC-5.1了).

第一步, 自然是下载PHP7的源代码了,  然后做./configure. 这些都没什么区别

接下来就是有区别的地方了, 我们要首先第一遍编译PHP7, 让它生成会产生profile数据的可执行文件:

<ol class="dp-c"><li class="alt"><span><span>$ make prof-gen <br /></span></span></li></ol>

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

然后, 让我们开始训练GCC:

<ol class="dp-j"><li class="alt"><span><span>$ sapi/cgi/php-cgi -T </span><span class="number">100</span><span> /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/</span><span class="keyword">null</span><span> </span></span></li></ol>

也就是让php-cgi跑100遍wordpress的首页, 从而生成一些在这个过程中的profile信息.

然后, 我们开始第二次编译PHP7.

<ol class="dp-c"><li class="alt"><span><span>$ make prof-clean </span></span></li><li><span>$ make prof-<span class="keyword">use</span><span> && make install </span></span></li></ol>

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

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

现在每秒钟可以处理1191.78个QPS了,  提升是~7%.  还不赖哈(咦, 你不是说10%么? 怎么成7%了?  呵呵, 正如我之前说过, 我们尝试分析PGO都做了些什么优化, 然后把一些通用的优化手工Apply到PHP7中. 所以也就是说, 那~3%的比较通用的优化已经包含到了PHP7里面了, 当然这个工作还在继续).

于是就这么简单, 大家可以用自己的产品的经典场景来训练GCC, 简单几步, 获得提升, 何乐而不为呢 :)

thanks

编者注:本文是 PHP 大神 —— 鸟哥 @Laruence 的作品,原文地址:http://www.laruence.com/2015/06/19/3063.html

 



www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1021432.htmlTechArticle让你的 PHP 7 更快 (GCC PGO) 我们一直致力于提升PHP7的性能, 上个月我们注意到GCC的PGO能在Wordpress上能带来近10%的性能提升, 这个让我们很激动...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.