php预加载,preload

______心丶

php预加载,preload

php.ini

# 只用设置成0以后重启php才能清空preload的文件
opcache.enable = 1
# 配置进行preload的php脚本,php-fpm启动的时候会先根据这个文件进行预加载
opcache.preload=/home/www/test/preload.php 
# preload的用户,不建议使用root 
opcache.preload_user = www 

使用opcache_compile_file进行文件缓存,一旦缓存不会被清除,使用opcache_reset()不会清除预加载的文件,opcache_invalidate()也不能进行清除,使用opcache_get_status()可以查看缓存占用的内存大小,以及缓存的文件名

//preload.php
$files = glob('/home/www/test/*.php');

foreach ($files as $file) {
    opcache_compile_file($file);
}
//parent.php
class A{
   public function a(){
      echo 'class A';
   }
}
//child.php
require('./parent.php');
class B extends A{
  public function b (){
          echo 'clsss B'.PHP_EOL;
  }
}
//conf.php
return array_fill(0,20,'abc');
//index.php
<?php
require('./child.php');
require('./conf.php');
opcache_reset();
print_r(opcache_get_status());
$obj = new B;
$obj->b();

ab -c 1 -n 10000

开启预加载

Concurrency Level:      1
Time taken for tests:   2.864 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      1620000 bytes
HTML transferred:       160000 bytes
Requests per second:    3492.01 [#/sec] (mean)
Time per request:       0.286 [ms] (mean)
Time per request:       0.286 [ms] (mean, across all concurrent requests)
Transfer rate:          552.45 [Kbytes/sec] received

未开启预加载

Concurrency Level:      1
Time taken for tests:   4.043 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      1860000 bytes
HTML transferred:       400000 bytes
Requests per second:    2473.23 [#/sec] (mean)
Time per request:       0.404 [ms] (mean)
Time per request:       0.404 [ms] (mean, across all concurrent requests)
Transfer rate:          449.24 [Kbytes/sec] received

Laravel7预加载测试 ab -c 1 -n 1000

//preload.php 
function a($path){
   $files = glob($path.'*');
  // print_r($files);
   foreach($files as $file){
        
        if(is_dir($file)){
           a($file.'/');
        }else{
          //使用pathinfo 然后获取extension 报错,php启动不起来
          if(basename($file) == 'index.php'){
              continue;
          }
          if(strstr($file,'.' ) == '.php'){
            opcache_compile_file($file);
          }
            
        }
   }
}
a('/home/www/test/Laravel7/vendor/laravel/');

未开启预加载

Concurrency Level:      1
Time taken for tests:   100.983 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      3355000 bytes
HTML transferred:       2426000 bytes
Requests per second:    9.90 [#/sec] (mean)
Time per request:       100.983 [ms] (mean)
Time per request:       100.983 [ms] (mean, across all concurrent requests)
Transfer rate:          32.44 [Kbytes/sec] received

开启预加载Laravel, 只缓存vendor/laravel/ 目录

Concurrency Level:      1
Time taken for tests:   12.377 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      3355000 bytes
HTML transferred:       2426000 bytes
Requests per second:    80.79 [#/sec] (mean)
Time per request:       12.377 [ms] (mean)
Time per request:       12.377 [ms] (mean, across all concurrent requests)
Transfer rate:          264.71 [Kbytes/sec] received

总结

  1. 自己写的,缓存文件数4,性能有40%提升
  2. 将laravel核心文件进行预加载,总缓存大小2200000左右,性能提升10倍
  3. 缺点:一旦缓存不能修改,而且只能通过重启php进程生效
阅读 827
2 声望
0 粉丝
0 条评论
你知道吗?

2 声望
0 粉丝
宣传栏