Web lists-archives.com

Re: [PHP] Performance issue to PHP include file




Great thanks!
We tried both APC and OpCache, both could solve the problem. By enabling
APC or OpCache, the performance is as good as the solution that writing the
code from including file directly to PHP file.

Thanks!

-Ethan

On Tue, May 3, 2016 at 10:01 PM, Ryan Pallas <derokorian@xxxxxxxxx> wrote:

>
>
> On Tue, May 3, 2016 at 7:29 AM, Yicong Huang <hengha.mao@xxxxxxxxx> wrote:
>
>> Hi all,
>>
>> We observed the performance issue to PHP include file for PHP 5.4.41.
>> Sorry
>> for using the old version, we haven't got the time to check whether the
>> issue still exists in the recent version.
>>
>> PHP sample code:
>> include “core/Core.class.php”
>> include "core/View.class.php"
>> include ...
>> //do some work below
>> ...
>>
>> 1. From the perfomance test, we observed as high as 80%+ CPU sys% for
>> about
>> 100 concurret requests.
>> 2. We used strace to attach php-fpm process:
>>
>> open("/srv/www/simulation/my/core/View.class.php", O_RDONLY) = 9
>> fstat(9, {st_mode=S_IFREG|0644, st_size=2034, ...}) = 0
>> fstat(9, {st_mode=S_IFREG|0644, st_size=2034, ...}) = 0
>> fstat(9, {st_mode=S_IFREG|0644, st_size=2034, ...}) = 0
>> mmap(NULL, 2034, PROT_READ, MAP_SHARED, 9, 0) = 0x7fa21727c000
>> stat("/srv/www/simulation/my/core/View.class.php", {st_mode=S_IFREG|0644,
>> st_size=2034, ...}) = 0
>> fcntl(3, F_SETLKW, {type=F_WRLCK, whence=SEEK_SET, start=0, len=1}) = 0
>> fcntl(3, F_SETLKW, {type=F_UNLCK, whence=SEEK_SET, start=0, len=1}) = 0
>> munmap(0x7fa21727c000, 2034)            = 0
>> close(9)                                = 0
>>
>> From the above log, we observed php-fpm used fcntl to accqure write lock
>> for the including file "core/View.class.php". Similar log were observed as
>> well for other including files.
>> 3. We used perf, and found out most of CPU time were consumed by
>> fcntl_setlk:
>> -   88.23%     0.13%          php-fpm  [kernel.kallsyms]            [k]
>> fcntl_setlk
>>                                      ▒
>>    - fcntl_setlk
>>
>>                                   ▒
>>       - 100.00% sys_fcntl
>>
>>                                  ▒
>>            system_call_fastpath
>>
>>                                  ▒
>>            __GI___libc_fcntl
>>
>>
>> All the above messages seem to indicate that PHP would acquire file writer
>> lock to include file, and the lock contention lead to serious perfomance
>> issue.
>>
>> Then, we did another experiment by writing the code from includeing files
>> directly to the same PHP file.
>>
>> PHP sample code:
>> //the code from “core/Core.class.php"
>> ...
>> //the code from  "core/View.class.php"
>> ...
>> //the original code
>> ...
>>
>> From the test, we got more than twice perfomance improvements than the
>> pervious, measured by tps (transaction per second).
>>
>> Is this a known issue to PHP? And are there any good solutions to the
>> issue, except for do not use "include" file?
>>
>
> Well if you were on 5.5+ then you would have OpCache[1] bundled in. On 5.4
> there is a pecl extension[2] available that you might try. This can
> significantly help as it "improves PHP performance by storing precompiled
> script bytecode in shared memory, thereby removing the need for PHP to load
> and parse scripts on each request"[1]
>
> Outside of that, I'm not sure if APC was still part of 5.4? I think it was
> last bundled in 5.3 maybe... but this is the previous version of opcode
> caching (however, if possible, definitely try OpCache as its a noticeable
> improvement for my apps).
>
>>
>> Thanks!
>>
>> -Ethan
>>
>
> Good luck!
> Ryan
>
> [1] http://php.net/manual/en/intro.opcache.php
> [2] http://pecl.php.net/package/ZendOpcache
>