讓你的PHP 7更快(GCC PGO)

來源:互聯網
上載者:User

讓你的PHP 7更快(GCC PGO)

我們一直致力於提升PHP7的效能, 上個月我們注意到GCC的PGO能在Wordpress上能帶來近10%的效能提升, 這個讓我們很激動.

然而, PGO正如名字所說(Profile Guided Optimization 有興趣的可以Google), 他需要用一些用例來獲得反饋, 也就是說這個最佳化是需要和一個特定的情境綁定的.

你對一個情境的最佳化, 也許在另外一個情境就事與願違了. 它不是一個通用的最佳化. 所以我們不能簡單的就包含這些最佳化, 也無法直接發布PGO編譯後的PHP7.

當然, 我們正在嘗試從PGO找出一些共性的最佳化, 然後手工Apply到PHP7上去, 但這個很明顯不能做到針對一個情境的特別最佳化所能達到的效果, 所以我決定寫這篇文章簡單介紹下怎麼使用PGO來編譯PHP7, 讓你編譯的PHP7能特別的讓你自己的獨立的應用變得更快.

首先, 要決定的就是拿什麼情境去Feedback GCC, 我們一般都會選擇: 在你要最佳化的情境中: 訪問量最大的, 耗時最多的, 資源消耗最重的一個頁面.

拿Wordpress為例, 我們選擇Wordpress的首頁(因為首頁往往是訪問量最大的).

我們以我的機器為例:

  1.        Intel(R) Xeon(R) CPU X5687 @ 3.60GHz X 16(超執行緒),
  2.        48G Memory

php-fpm 採用固定32個worker, opcache採用預設的配置(一定要記得載入opcache)

以wordpress 4.1為最佳化情境..

首先我們來測試下目前WP在PHP7的效能(ab -n 10000 -c 100):

  1. $ ab -n 10000 -c 100 http://inf-dev-maybach.weibo.com:8000/wordpress/
  2. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5.  
  6. Benchmarking inf-dev-maybach.weibo.com (be patient)
  7. Completed 1000 requests
  8. Completed 2000 requests
  9. Completed 3000 requests
  10. Completed 4000 requests
  11. Completed 5000 requests
  12. Completed 6000 requests
  13. Completed 7000 requests
  14. Completed 8000 requests
  15. Completed 9000 requests
  16. Completed 10000 requests
  17. Finished 10000 requests
  18.  
  19. Server Software: nginx/1.7.12
  20. Server Hostname: inf-dev-maybach.weibo.com
  21. Server Port: 8000
  22.  
  23. Document Path: /wordpress/
  24. Document Length: 9048 bytes
  25.  
  26. Concurrency Level: 100
  27. Time taken for tests: 8.957 seconds
  28. Complete requests: 10000
  29. Failed requests: 0
  30. Write errors: 0
  31. Total transferred: 92860000 bytes
  32. HTML transferred: 90480000 bytes
  33. Requests per second: 1116.48 [#/sec] (mean)
  34. Time per request: 89.567 [ms] (mean)
  35. Time per request: 0.896 [ms] (mean, across all concurrent requests)
  36. 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資料的可執行檔:

  1. $ make prof-gen

注意, 我們用到了prof-gen參數(這個是PHP7的Makefile特有的, 不要嘗試在其他項目上也這麼搞哈 )

然後, 讓我們開始訓練GCC:

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

也就是讓php-cgi跑100遍wordpress的首頁, 從而產生一些在這個過程中的profile資訊.

然後, 我們開始第二次編譯PHP7.

  1. $ make prof-clean
  2. $ make prof-use && make install

好的, 就這麼簡單, PGO編譯完成了, 現在我們看看PGO編譯以後的PHP7的效能:

  1. $ ab -n10000 -c 100 http://inf-dev-maybach.weibo.com:8000/wordpress/
  2. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5.  
  6. Benchmarking inf-dev-maybach.weibo.com (be patient)
  7. Completed 1000 requests
  8. Completed 2000 requests
  9. Completed 3000 requests
  10. Completed 4000 requests
  11. Completed 5000 requests
  12. Completed 6000 requests
  13. Completed 7000 requests
  14. Completed 8000 requests
  15. Completed 9000 requests
  16. Completed 10000 requests
  17. Finished 10000 requests
  18.  
  19. Server Software: nginx/1.7.12
  20. Server Hostname: inf-dev-maybach.weibo.com
  21. Server Port: 8000
  22.  
  23. Document Path: /wordpress/
  24. Document Length: 9048 bytes
  25.  
  26. Concurrency Level: 100
  27. Time taken for tests: 8.391 seconds
  28. Complete requests: 10000
  29. Failed requests: 0
  30. Write errors: 0
  31. Total transferred: 92860000 bytes
  32. HTML transferred: 90480000 bytes
  33. Requests per second: 1191.78 [#/sec] (mean)
  34. Time per request: 83.908 [ms] (mean)
  35. Time per request: 0.839 [ms] (mean, across all concurrent requests)
  36. Transfer rate: 10807.45 [Kbytes/sec] received

現在每秒鐘可以處理1191.78個QPS了, 提升是~7%. 還不賴哈(咦, 你不是說10%麼? 怎麼成7%了? 呵呵, 正如我之前說過, 我們嘗試分析PGO都做了些什麼最佳化, 然後把一些通用的最佳化手工Apply到PHP7中. 所以也就是說, 那~3%的比較通用的最佳化已經包含到了PHP7裡面了, 當然這個工作還在繼續).

於是就這麼簡單, 大家可以用自己的產品的經典情境來訓練GCC, 簡單幾步, 獲得提升, 何樂而不為呢

PHP 7革新與效能最佳化

PHP 7 ,你值得擁有   

在 CentOS 7.x / Fedora 21 上面體驗 PHP 7.0   

CentOS 6.3 安裝LNMP (PHP 5.4,MyySQL5.6)  

在部署LNMP的時候遇到Nginx啟動失敗的2個問題  

Ubuntu安裝Nginx php5-fpm MySQL(LNMP環境搭建)  

《細說PHP》高清掃描PDF+光碟片源碼+全套教學視頻  

CentOS 6中配置PHP的LNMP的開發環境   

PHP 的詳細介紹:請點這裡
PHP 的:請點這裡

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.