讓你的PHP7更快 GCC PGO

2021-09-01 23:49:54 字數 3845 閱讀 3254

我們一直致力於提公升php7的效能, 上個月我們注意到gcc的pgo能在wordpress上能帶來近10%的效能提公升, 這個讓我們很激動.

然而, pgo正如名字所說(profile guided optimization 有興趣的可以google), 他需要用一些用例來獲得反饋, 也就是說這個優化是需要和乙個特定的場景繫結的.

你對乙個場景的優化, 也許在另外乙個場景就事與願違了. 它不是乙個通用的優化. 所以我們不能簡單的就包含這些優化, 也無法直接發布pgo編譯後的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 

this is apachebench, version 2.3

licensed to the apache software foundation,

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, 讓它生成會產生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 

this is apachebench, version 2.3

licensed to the apache software foundation,

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

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

GCC PGO 適應性優化 PHP7 編譯詳細介紹

pgo 即 profile guided optimization 歸檔引導優化 是一種自適應優化手段,不需要對軟體 本身做出改進,即可獲得效能的提公升,目前已經應用於 gcc vc 等常見的編譯器中。一些開源專案像 firefox 就預設使用 pgo 編譯來優化效能,但是大部分軟體上應用率並不高。...

GCC PGO 適應性優化 PHP7 編譯詳細介紹

pgo 即 profile guided optimization 歸檔引導優化 是一種自適應優化手段,不需要對軟體 本身做出改進,即可獲得效能的提公升,目前已經應用於 gcc vc 等常見的編譯器中。一些開源專案像 firefox 就預設使用 pgo 編譯來優化效能,但是大部分軟體上應用率並不高。...

PHP 7 新特性你知道多少?

1.運算子 null 合併運算子 把這個放在第乙個說是因為我覺得它很有用。用法 a get a 1 它相當於 把這個放在第乙個說是因為我覺得它很有用。用法 a get a 1 它相當於 a isset get a get a 1 我們知道三元運算子是可以這樣用的 a 1 但是這是建立在 a 已經定義...