直播好久没有曝光量了,自荐一波《》(PHPer们,好久没有投资自己了呢?)
原文地址
PHP7.2 更新内容
从官方给的文档中,我似乎没有看到关于整体性能方面的修改。鸟哥点评 PHP7.2 相对于 PHP7.1 主要优化也是在 opcache 。
服务器配置
2核 Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz内存 4G系统 Centos 6.2gcc 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 -vPHP 7.1.10 (cli) (built: Oct 8 2017 16:08:01) ( NTS )Copyright (c) 1997-2017 The PHP GroupZend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies$ /data/local/php72/bin/php -vPHP 7.2.0RC3 (cli) (built: Oct 8 2017 18:11:35) ( NTS )Copyright (c) 1997-2017 The PHP GroupZend Engine v3.2.0-dev, Copyright (c) 1998-2017 Zend Technologies
php.ini 配置
仅供测试,非生产环境使用,摘录自
engine = Onshort_open_tag = Offrealpath_cache_size = 2Mmax_execution_time = 86400memory_limit = 1024Merror_reporting = 0display_errors = 0display_startup_errors = 0log_errors = 0default_charset = "UTF-8" [opcache]zend_extension=opcache.soopcache.enable=1opcache.enable_cli=1opcache.optimization_level=-1opcache.fast_shutdown=1opcache.validate_timestamps=1opcache.revalidate_freq=60opcache.use_cwd=1opcache.max_accelerated_files=100000opcache.max_wasted_percentage=5opcache.memory_consumption=128opcache.consistency_checks=0opcache.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.381Total 1.416Total 1.374Total 1.366Total 1.430Total 1.394Total 1.394Total 1.369Total 1.377Total 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.448Total 1.444Total 1.445Total 1.458Total 1.440Total 1.447Total 1.486Total 1.433Total 1.449Total 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}' ;doneTotal 0.794Total 0.797Total 0.798Total 0.806Total 0.808Total 0.793Total 0.814Total 0.846Total 0.859Total 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}' ;doneTotal 0.779Total 0.774Total 0.765Total 0.772Total 0.764Total 0.769Total 0.779Total 0.839Total 0.784Total 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}' ;doneTotal 5.588Total 5.689Total 5.652Total 5.702Total 5.668Total 5.641Total 5.622Total 5.580Total 5.635Total 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}' ;doneTotal 5.924Total 5.597Total 5.553Total 5.579Total 5.591Total 5.523Total 5.518Total 5.503Total 5.494Total 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}' ;doneTotal 4.369Total 4.379Total 4.352Total 4.370Total 4.375Total 4.397Total 4.311Total 4.361Total 4.313Total 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}' ;doneTotal 3.711Total 3.711Total 3.712Total 3.734Total 3.700Total 3.712Total 3.705Total 3.769Total 3.785Total 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 |
软件应用测试
配置说明
2核 Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz内存 4G系统 Centos 6.2nginx 1.10.1mysql 5.5.28wordpress 4.8.1
考虑到该服务器上已经在运行我的博客等其他服务,所以php-fpm子进程数只配置了15个,在相同配置文件的条件下对比。
# php-fpm 简单配置,仅供测试[global]pid = /data/local/php7{x}/var/run/php-fpm.piderror_log = /data/log/php7{x}-fpm.loglog_level = notice[www]listen = /tmp/php7{x}-cgi.socklisten.backlog = -1listen.allowed_clients = 127.0.0.1listen.owner = wwwlisten.group = wwwlisten.mode = 0666user = wwwgroup = wwwpm = staticpm.max_children = 15
wordpress 压测
未开启 opcache
# php7.1Concurrency Level: 10Time taken for tests: 8.696 secondsComplete requests: 100Failed requests: 0Write errors: 0Total transferred: 5215300 bytesHTML transferred: 5189300 bytesRequests 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.2Concurrency Level: 10Time taken for tests: 8.528 secondsComplete requests: 100Failed requests: 0Write errors: 0Total transferred: 5215500 bytesHTML transferred: 5189300 bytesRequests 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 |
在开启opcache 之后,相对之前未开启的情况性能简直天壤之别。相比之下php7.2在wordpress压测上,QPS 稳定在70+ 相对php7.1 增加不少。
测试结果和配置参数以及服务器配置有关,仅供对比php7.1与7.2的性能。
其他让你的 PHP 更快的方式,请参考@Laruence