php7教程栏目介绍PHP7.2、PHP7.1的性能对比。 推荐:PHP7教程 服务器配置 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$
推荐:PHP7教程
服务器配置
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 配置
仅供测试,非生产环境使用,摘录自 www.laruence.com/2016/12/18/…
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
软件应用测试
配置说明
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
在开启opcache 之后,相对之前未开启的情况性能简直天壤之别。相比之下php7.2在wordpress压测上,QPS 稳定在70+ 相对php7.1 增加不少。
测试结果和配置参数以及服务器配置有关,仅供对比php7.1与7.2的性能。