PHP7.2、PHP7.1效能對比

周夢康發表於2017-10-09

原文地址:https://mengkang.net/1019.html
PHP7.2 更新內容
https://github.com/php/php-src/blob/php-7.2.0RC3/UPGRADING
從官方給的文件中,我似乎沒有看到關於整體效能方面的修改。

鳥哥點評 PHP7.2 相對於 PHP7.1 主要優化也是在 opcache 。

伺服器配置

2核 Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz
記憶體 4G
系統 Centos 6.2
gcc 4.4.7

安裝

# http://hk1.php.net/get/php-7.1.10.tar.gz/from/this/mirror
$ ./configure --prefix=/data/local/php71 --with-config-file-path=/data/local/php71/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-xsl
# https://downloads.php.net/~remi/php-7.2.0RC3.tar.gz
$ ./configure --prefix=/data/local/php72 --with-config-file-path=/data/local/php72/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-xsl
$ /data/local/php71/bin/php -v
PHP 7.1.10 (cli) (built: Oct  8 2017 16:08:01) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

$ /data/local/php72/bin/php -v
PHP 7.2.0RC3 (cli) (built: Oct  8 2017 18:11:35) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0-dev, Copyright (c) 1998-2017 Zend Technologies

php.ini 配置

僅供測試,非生產環境使用,摘錄自 http://www.laruence.com/2016/12/18/3137.html

engine = On
short_open_tag = Off
realpath_cache_size = 2M
max_execution_time = 86400
memory_limit = 1024M
error_reporting = 0
display_errors = 0
display_startup_errors = 0
log_errors = 0
default_charset = "UTF-8"
 
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
opcache.fast_shutdown=1
opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.use_cwd=1
opcache.max_accelerated_files=100000
opcache.max_wasted_percentage=5
opcache.memory_consumption=128
opcache.consistency_checks=0
opcache.huge_code_pages=1

純 CPU 基準測試

基於Zend/bench.php的指令碼測試

未開啟 opcache

$ for i in `seq 1 10`;do /data/local/php71/bin/php /data/soft/php-7.2.0RC3/Zend/bench.php|grep `Total`|awk `{print $0}` ;done 
Total              1.381
Total              1.416
Total              1.374
Total              1.366
Total              1.430
Total              1.394
Total              1.394
Total              1.369
Total              1.377
Total              1.358
# 取平均值  1.3859
$ for i in `seq 1 10`;do /data/local/php72/bin/php /data/soft/php-7.2.0RC3/Zend/bench.php|grep `Total`|awk `{print $0}` ;done                 
Total              1.448
Total              1.444
Total              1.445
Total              1.458
Total              1.440
Total              1.447
Total              1.486
Total              1.433
Total              1.449
Total              1.464
# 取平均值  1.4514

開啟 opcache 之後

$ for i in `seq 1 10`;do /data/local/php71/bin/php /data/soft/php-7.2.0RC3/Zend/bench.php|grep `Total`|awk `{print $0}` ;done
Total              0.794
Total              0.797
Total              0.798
Total              0.806
Total              0.808
Total              0.793
Total              0.814
Total              0.846
Total              0.859
Total              0.818
# 取平均值 0.8133
$ for i in `seq 1 10`;do /data/local/php72/bin/php /data/soft/php-7.2.0RC3/Zend/bench.php|grep `Total`|awk `{print $0}` ;done
Total              0.779
Total              0.774
Total              0.765
Total              0.772
Total              0.764
Total              0.769
Total              0.779
Total              0.839
Total              0.784
Total              0.842
# 取平均值 0.7867

在本次測試中,未開啟opcache的情況下,php7.2 效能反而有些下降,開啟opcache之後,效能追趕上來,比php7.1略有提升。所以更應該開啟opcache了。

基於Zend/micro_bench.php的指令碼測試

未開啟 opcache

$ for i in `seq 1 10`;do /data/local/php71/bin/php /data/soft/php-7.2.0RC3/Zend/micro_bench.php|grep `Total`|awk `{print $0}` ;done
Total              5.588
Total              5.689
Total              5.652
Total              5.702
Total              5.668
Total              5.641
Total              5.622
Total              5.580
Total              5.635
Total              5.588
# 取平均值 5.6365
$ for i in `seq 1 10`;do /data/local/php72/bin/php /data/soft/php-7.2.0RC3/Zend/micro_bench.php|grep `Total`|awk `{print $0}` ;done
Total              5.924
Total              5.597
Total              5.553
Total              5.579
Total              5.591
Total              5.523
Total              5.518
Total              5.503
Total              5.494
Total              5.558
# 取平均值 5.584

開啟 opcache

$ for i in `seq 1 10`;do /data/local/php71/bin/php /data/soft/php-7.2.0RC3/Zend/micro_bench.php|grep `Total`|awk `{print $0}` ;done
Total              4.369
Total              4.379
Total              4.352
Total              4.370
Total              4.375
Total              4.397
Total              4.311
Total              4.361
Total              4.313
Total              4.373
# 取平均值 4.36
$ for i in `seq 1 10`;do /data/local/php72/bin/php /data/soft/php-7.2.0RC3/Zend/micro_bench.php|grep `Total`|awk `{print $0}` ;done
Total              3.711
Total              3.711
Total              3.712
Total              3.734
Total              3.700
Total              3.712
Total              3.705
Total              3.769
Total              3.785
Total              3.695
# 取平均值 3.7234

在本次測試中,未開啟opcache的情況下,php7.2 效能比php7.1 略有提升,開啟opcache之後,php7.2比php7.1有很大的提升。

圖表呈現

彙總 bench.php bench.php + opcache micro_bench.php micro_bench.php + opcache
PHP7.1 1.3859 0.8133 5.6365 4.3600
PHP7.2 1.4514 0.7867 5.5840 3.7234

WX20171008-202655@2x.png

軟體應用測試

配置說明

2核 Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz
記憶體 4G
系統 Centos 6.2
nginx 1.10.1
mysql 5.5.28
wordpress 4.8.1

考慮到該伺服器上已經在執行我的部落格等其他服務,所以php-fpm子程式數只配置了15個,在相同配置檔案的條件下對比。

# php-fpm 簡單配置,僅供測試
[global]
pid = /data/local/php7{x}/var/run/php-fpm.pid
error_log = /data/log/php7{x}-fpm.log
log_level = notice

[www]
listen = /tmp/php7{x}-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = static
pm.max_children = 15

wordpress 壓測

未開啟 opcache

# php7.1
Concurrency Level:      10
Time taken for tests:   8.696 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      5215300 bytes
HTML transferred:       5189300 bytes
Requests per second:    11.50 [#/sec](mean)
Time per request:       869.637 [ms](mean)
Time per request:       86.964 [ms](mean, across all concurrent requests)
Transfer rate:          585.65 [Kbytes/sec] received
# php7.2
Concurrency Level:      10
Time taken for tests:   8.528 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      5215500 bytes
HTML transferred:       5189300 bytes
Requests per second:    11.73 [#/sec](mean)
Time per request:       852.793 [ms](mean)
Time per request:       85.279 [ms](mean, across all concurrent requests)
Transfer rate:          597.24 [Kbytes/sec] received

可以看到在未開啟opcache的情況下,效能非常糟糕,10個併發的情況下,每個請求的響應時間已經非常長了,沒有必要繼續增加併發數了。

開啟 opcache 之後

彙總 7.1 Requests per second (每秒) 7.1 Time per request (ms) 7.2 Requests per second (每秒) 7.2 Time per request (ms)
c10 n100 60.63 164.939 70.05 142.762
c20 n200 66.27 301.803 70.74 282.719
c30 n300 66.50 451.121 70.89 423.2
c40 n400 67.95 588.683 70.6 566.608

WX20171008-210819@2x.png

在開啟opcache 之後,相對之前未開啟的情況效能簡直天壤之別。相比之下php7.2在wordpress壓測上,QPS 穩定在70+ 相對php7.1 增加不少。

測試結果和配置引數以及伺服器配置有關,僅供對比php7.1與7.2的效能。

其他讓你的 PHP 更快的方式,請參考@Laruence http://www.laruence.com/2015/12/04/3086.html


相關文章