phar缓存 编译缓存 提高phar文件包加载速度

phar文件可以把用到的PHP文件全部打包在一个文件中,十分方便网站部署。但是单个的PHP文件可以使用opcache缓存(字节码缓存),以提升PHP的运行速度。那么PHAR文件包如何使用缓存呢。

这里需要进行配置(php.ini)

phar.cache_list

手册上的解释为:

Allows mapping phar archives to be pre-parsed at web server startup, providing a performance improvement that brings running files out of a phar archive very close to the speed of running those files from a traditional disk-based installation.

允许在web服务器启动时预分析映射phar存档,从而提供性能改进,使phar存档中运行的文件的速度非常接近于从传统的基于磁盘的安装中运行这些文件的速度。

这个功能只有在phar 2.0.0开始支持。当然目前你安装的PHP都是2以上的。phar  2.0.0 的发布日期为 2009-07-29。手册上的配置说明。

# in php.ini (windows):
phar.cache_list =C:path ophar1.phar;C:path ophar2.phar
# in php.ini (unix):
phar.cache_list =/path/to/phar1.phar:/path/to/phar2.phar

windows上多个文件使用分号,LINUX上使用冒号。

启用了cache_list后,重启php-fpm(或者可能是Apache)。

请确保opcache处于启用状态(php.ini):

opcache.enable=1

那么效果如何呢?用我的NILCMS框架做测试。环境:

PHP 7.2.25

NGINX 1.14.2

CentOS 7.7.1908

测试命令:ab -n 500 -c 5 域名

1.opcache未启用,cache_list未配置

Concurrency Level:      5
Time taken for tests:   7.665 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      110500 bytes
HTML transferred:       21000 bytes
Requests per second:    65.23 [#/sec] (mean)
Time per request:       76.648 [ms] (mean)
Time per request:       15.330 [ms] (mean, across all concurrent requests)
Transfer rate:          14.08 [Kbytes/sec] received

2.opcache启用,cache_list未配置

Concurrency Level:      5
Time taken for tests:   1.406 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      110500 bytes
HTML transferred:       21000 bytes
Requests per second:    355.62 [#/sec] (mean)
Time per request:       14.060 [ms] (mean)
Time per request:       2.812 [ms] (mean, across all concurrent requests)
Transfer rate:          76.75 [Kbytes/sec] received

3.opcache未启用,cache_list配置

Concurrency Level:      5
Time taken for tests:   7.588 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      110500 bytes
HTML transferred:       21000 bytes
Requests per second:    65.89 [#/sec] (mean)
Time per request:       75.881 [ms] (mean)
Time per request:       15.176 [ms] (mean, across all concurrent requests)
Transfer rate:          14.22 [Kbytes/sec] received

4.opcache启用,cache_list配置

Concurrency Level:      5
Time taken for tests:   1.312 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      110500 bytes
HTML transferred:       21000 bytes
Requests per second:    381.01 [#/sec] (mean)
Time per request:       13.123 [ms] (mean)
Time per request:       2.625 [ms] (mean, across all concurrent requests)
Transfer rate:          82.23 [Kbytes/sec] received

测试结果看

1.启用opcache后,对性能提升非常明显。

7.665 -> 1.406

7.588 -> 1.312

2.启用phar.cache_list后:对性能有一点点的提升。

7.665 -> 7.588

1.406 -> 1.312

来此加密:Let’s Encrypt 网页版本,获取SSL网站证书:https://letsencrypt.osfipin.com/

所以最为通用的做法是使用opcache。

原文地址:https://www.cnblogs.com/osfipin/p/11926495.html