与pcntl_fork()一起使用时,APC可以提高CLI脚本的速度吗? [英] Can APC improve the speed of CLI scripts when used with pcntl_fork()?

查看:73
本文介绍了与pcntl_fork()一起使用时,APC可以提高CLI脚本的速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

APC通过将来自PHP文件的操作码存储在共享内存中来工作.当PHP与Web服务器(例如Apache)一起使用时,共享内存的寿命很长.从命令行调用时,将为每个进程创建并销毁APC缓存.可能是由于默认情况下,默认情况下在通讯线上禁用了APC.

APC works by storing the opcodes from PHP files in shared memory. When PHP is used with a web server (eg Apache) then the shared memory has a long life. When called from the commandline, then the APC cache is created and destroyed for each process. APC is disabled on the commadnline by default, probably due to this.

我有一个理论,如果派生了一个PHP进程(使用pcntl_fork(),因为可以使用相同的操作码缓存,那么使用APC将会有好处.这可能仅适用于派生之后的文件.

I have a theory that there will be benefits from using APC if a PHP process is forked (with pcntl_fork() as presumably the same opcode cache can be used. This may only apply to files included after the fork.

基准测试的最佳方法是什么?任何人都可以对此进行测试或解释该理论是否正确?

What's the best way to benchmark this? Can anyone either test this or explain whether the theory is correct or not?

<?php
if (pcntl_fork()) {
    // parent
    include 'huge-file.php';
} else {
    // child
    sleep(1); // stop race condition
    include 'huge-file.php'; // will this use APC's cache?
}

推荐答案

APC在CLI模式下有两种影响:

APC has an impact in CLI mode in two cases:

  • 通过相同的脚本(例如,包含某些数据缓存的文件)或多个进程重复包含相同的文件
  • 您使用 apc_fetch()(如果禁用了apc,则始终返回false)
  • The same file is included repeatedly, either by the same script (e.g. a file containing some data cache), or by multiple processes
  • You use apc_fetch() (always returns false if apc is disabled)

请注意,默认情况下,APC会在CLI中禁用自身;您必须使用apc.enable_cli=1启用它.

Note that APC disables itself in CLI by default; you have to enable it with apc.enable_cli=1.

这是一个快速基准测试:

Here is a quick benchmark:

<?php                                                                           

for ($i = 0; $i < 1000; ++$i) {                                                 
    // cache.php contains the output of 'var_export(get_defined_constants(true))'
    require 'cache.php';                                                   
}

没有apc的结果:

$ time php test.php

real    0m1.219s
user    0m1.208s
sys     0m0.008s

apc结果:

$ time php -dapc.enable_cli=1 test.php

real    0m0.252s
user    0m0.244s
sys     0m0.004s

在这种情况下,APC确实会对性能产生重大影响.

In this case, APC does have a significant impact on performance.

使用pcntl_fork(),与在Apache的mod_php或php-fpm下运行多个PHP脚本相比,APC的影响应该完全相同:如果多个子脚本包含相同的文件,则所包含的文件将仅被解析一次.

With pcntl_fork(), APC should have exactly the same impact than running multiple PHP scripts under apache's mod_php or php-fpm: if multiple children scripts include the same files, the included files will be parsed only once.

在PHP 5.5中,捆绑的opcache扩展替代了APC也会优化代码,因此它不仅会影响require的性能,而且还会影响代码本身的性能.

In PHP 5.5, the bundled opcache extension replacing APC also optimizes the code, so it should not only impact require performance, but also the performance of the code itself.

这篇关于与pcntl_fork()一起使用时,APC可以提高CLI脚本的速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆