讓你的 PHP 7 更快 (GCC PGO)

jopen 9年前發布 | 10K 次閱讀 PHP

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

我們一直致力于提升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特有的, 不要嘗試在其他項目上也這么搞哈 讓你的 PHP 7 更快 (GCC PGO) )

然后, 讓我們開始訓練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, 簡單幾步, 獲得提升, 何樂而不為呢 讓你的 PHP 7 更快 (GCC PGO)

thanks

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!