我們一直致力於提升PHP7的性能, 上個月我們注意到GCC的PGO能在Wordpress上能帶來近10%的性能提升, 這個讓我們很激動.
然而, PGO正如名字所說(Profile Guided Optimization 有興趣的可以Google), 他需要用一些用例來獲得反饋, 也就是說這個優化是需要和一個特定的場景綁定的.
你對一個場景的優化, 也許在另外一個場景就事與願違了. 它不是一個通用的優化. 所以我們不能簡單的就包含這些優化, 也無法直接發布PGO編譯後的PHP7.
當然, 我們正在嘗試從PGO找出一些共性的優化, 然後手部Apply到PHP7上去, 但這個很明顯不能做到針對一個場景的特別優化所能達到的效果, 所以我決定寫這篇文章簡單介紹下怎麼使用PGO來編譯PHP7, 讓你編譯的PHP7能特別的讓你自己的獨立的應用變得更快.
首先, 要決定的就是拿什麼場景去Feedback GCC, 我們一般都會選擇: 在你要優化的場景中: 訪問量最大的, 耗時最多的,資源消耗最重的一個頁面.
拿Wordpress為例, 我們選擇Wordpress的首頁(因為首頁往往是訪問量最大的).
我們以我的機器為例:
<ol> <li><span><span>Intel(R) Xeon(R) CPU X5687 @ 3.60GHz X 16(超线程), </span></span></li> <li><span> </span></li> <li><span>48G Memory </span></li> </ol>
php-fpm 採用固定32個worker, opcache採用預設的設定(一定要記得載入opcache)
以wordpress 4.1為最佳化場景..
首先我們來測試下目前WP在PHP7的效能(ab -n 10000 -c 100):
<ol> <li><span><span>$ ab -n 10000 -c 100 http:</span><span>//inf-dev-maybach.weibo.com:8000/wordpress/</span><span> </span></span></li> <li><span> </span></li> <li><span>This is ApacheBench, Version 2.3 <<span>$Revision</span><span>: 655654 $> </span></li> <li><span> </span></li> <li><span>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http:<span>//www.zeustech.net/</span><span> </span></span></li> <li><span> </span></li> <li><span>Licensed to The Apache Software Foundation, http:<span>//www.apache.org/</span><span> </span></span></li> <li><span> </span></li> <li><span>Benchmarking inf-dev-maybach.weibo.com (be patient) </span></li> <li><span> </span></li> <li><span>Completed 1000 requests </span></li> <li><span> </span></li> <li><span>Completed 2000 requests </span></li> <li><span> </span></li> <li><span>Completed 3000 requests </span></li> <li><span> </span></li> <li><span>Completed 4000 requests </span></li> <li><span> </span></li> <li><span>Completed 5000 requests </span></li> <li><span> </span></li> <li><span>Completed 6000 requests </span></li> <li><span> </span></li> <li><span>Completed 7000 requests </span></li> <li><span> </span></li> <li><span>Completed 8000 requests </span></li> <li><span> </span></li> <li><span>Completed 9000 requests </span></li> <li><span> </span></li> <li><span>Completed 10000 requests </span></li> <li><span> </span></li> <li><span>Finished 10000 requests </span></li> <li><span> </span></li> <li><span>Server Software: nginx/1.7.12 </span></li> <li><span> </span></li> <li><span>Server Hostname: inf-dev-maybach.weibo.com </span></li> <li><span> </span></li> <li><span>Server Port: 8000 </span></li> <li><span> </span></li> <li><span>Document Path: /wordpress/ </span></li> <li><span> </span></li> <li><span>Document Length: 9048 bytes </span></li> <li><span> </span></li> <li><span>Concurrency Level: 100 </span></li> <li><span> </span></li> <li><span>Time taken <span>for</span><span> tests: 8.957 seconds </span></span></li> <li><span> </span></li> <li><span>Complete requests: 10000 </span></li> <li><span> </span></li> <li><span>Failed requests: 0 </span></li> <li><span> </span></li> <li><span>Write errors: 0 </span></li> <li><span> </span></li> <li><span>Total transferred: 92860000 bytes </span></li> <li><span> </span></li> <li><span>HTML transferred: 90480000 bytes </span></li> <li><span> </span></li> <li><span>Requests per second: 1116.48 [#/sec] (mean) </span></li> <li><span> </span></li> <li><span>Time per request: 89.567 [ms] (mean) </span></li> <li><span> </span></li> <li><span>Time per request: 0.896 [ms] (mean, across all concurrent requests) </span></li> <li><span> </span></li> <li><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><li><span><span>$ make prof-gen <br></span></span></li></ol>
注意, 我們用到了prof -gen參數(這個是PHP7的Makefile特有的, 不要嘗試在其他項目上也這麼搞哈:) )
然後, 讓我們開始訓練GCC:
<ol><li><span><span>$ sapi/cgi/php-cgi -T </span><span>100</span><span> /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/</span><span>null</span><span> </span></span></li></ol>
也就是讓php-cgi跑100遍wordpress的首頁, 從而產生一些在這個過程中的profile信息.
然後, 我們開始第二次編譯PHP7.
<ol> <li><span><span>$ make prof-clean </span></span></li> <li><span>$ make prof-<span>use</span><span> && make install </span></span></li> </ol>
好的, 就這麼簡單, PGO編譯完成了, 現在我們看看PGO編譯以後的PHP7的性能:
<ol> <li><span><span>$ ab -n10000 -c </span><span>100</span><span> http:</span><span>//inf-dev-maybach.weibo.com:8000/wordpress/</span><span> </span></span></li> <li><span> </span></li> <li><span>This is ApacheBench, Version <span>2.3</span><span> <$Revision: </span><span>655654</span><span> $> </span></span></li> <li><span> </span></li> <li><span>Copyright <span>1996</span><span> Adam Twiss, Zeus Technology Ltd, http:</span><span>//www.zeustech.net/</span><span> </span></span></li> <li><span> </span></li> <li><span>Licensed to The Apache Software Foundation, http:<span>//www.apache.org/</span><span> </span></span></li> <li><span> </span></li> <li><span>Benchmarking inf-dev-maybach.weibo.com (be patient) </span></li> <li><span> </span></li> <li><span>Completed <span>1000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>2000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>3000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>4000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>5000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>6000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>7000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>8000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>9000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Completed <span>10000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Finished <span>10000</span><span> requests </span></span></li> <li><span> </span></li> <li><span>Server Software: nginx/<span>1.7</span><span>.</span><span>12</span><span> </span></span></li> <li><span> </span></li> <li><span>Server Hostname: inf-dev-maybach.weibo.com </span></li> <li><span> </span></li> <li><span>Server Port: <span>8000</span><span> </span></span></li> <li><span> </span></li> <li><span>Document Path: /wordpress/ </span></li> <li><span> </span></li> <li><span>Document Length: <span>9048</span><span> bytes </span></span></li> <li><span> </span></li> <li><span>Concurrency Level: <span>100</span><span> </span></span></li> <li><span> </span></li> <li><span>Time taken <span>for</span><span> tests: </span><span>8.391</span><span> seconds </span></span></li> <li><span> </span></li> <li><span>Complete requests: <span>10000</span><span> </span></span></li> <li><span> </span></li> <li><span>Failed requests: <span>0</span><span> </span></span></li> <li><span> </span></li> <li><span>Write errors: <span>0</span><span> </span></span></li> <li><span> </span></li> <li><span>Total transferred: <span>92860000</span><span> bytes </span></span></li> <li><span> </span></li> <li><span>HTML transferred: <span>90480000</span><span> bytes </span></span></li> <li><span> </span></li> <li><span>Requests per second: <span>1191.78</span><span> [#/sec] (mean) </span></span></li> <li><span> </span></li> <li><span>Time per request: <span>83.908</span><span> [ms] (mean) </span></span></li> <li><span> </span></li> <li><span>Time per request: <span>0.839</span><span> [ms] (mean, across all concurrent requests) </span></span></li> <li><span> </span></li> <li><span>Transfer rate: <span>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
以上就介紹了讓你的 PHP 7 更快 GCC PGO,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。