PHP7 enables opcache to create powerful performance

Keywords: PHP

Bird brother said in his blog that the first of several tips to improve PHP 7 performance is to enable opcache:

Remember to enable Zend Opcache, because even if PHP7 does not enable Opcache, it is faster than PHP-5.6 with Opcache enabled,  
Therefore, during the previous testing period, it happened that someone had not enabled Opcache

So what is Opcache?

The previous life of Opcache is   Optimizer+  , It is a closed source but free PHP optimization acceleration component developed by Zend, the official company of PHP. Optimizer + precompiles PHP code to generate script files   Opcode   The cache is stored in shared memory for repeated use in the future, so as to avoid the time consumption of reading code from disk and recompiling. At the same time, it also applies some code optimization patterns to make the code execute faster. So as to speed up the execution of PHP.

  The normal execution process of PHP is as follows

Request request (nginx,apache,cli, etc.) -- > Zend engine reads. php file -- > scans its dictionary and expression -- > parses file -- > creates computer code to be executed (called opcode) - > finally executes opcode -- > response return

The above steps will be executed every time the PHP script is requested. If the PHP source code does not change, the Opcode will not change. Obviously, it is not necessary to regenerate the Opcode every time. Combined with the ubiquitous caching mechanism in the Web, we can cache the Opcode. Isn't it faster to directly access the cached Opcode in the future, The flow chart after Opcode caching is enabled is as follows:

  Opcode cache aims to avoid repeated compilation and reduce CPU and memory overhead.

The following describes the installation of Opcache

Installation:

1,find opcache My extension is php7.1
yum list php71*
2,Install extensions
yum install php71w-opcache.x86_64

to configure:

zend_extension=opcache.so
[opcache]
;open opcache
opcache.enable=1  

;CLI In this environment, PHP Enable OPcache
opcache.enable_cli=1

;OPcache Shared memory storage size,Company MB
opcache.memory_consumption=128  

;PHP A method called string persistence is used( string interning)For example, if you use a string 1000 times in your code“ foobar",stay PHP The internal will only allocate an immutable memory area to store the string when it is first used, and the other 999 uses will directly point to the memory area. This option will raise this feature to a higher level - by default, the immutable memory area will only exist in a single memory area php-fpm If this option is set, it will be used in all processes php-fpm In process sharing. In larger applications, this can effectively save memory and improve application performance.
The value of this option is in megabytes( megabytes)As a unit, if it is set to 16, it means 16 MB,The default is 4 MB
opcache.interned_strings_buffer=8

;This option controls the maximum number of caches in memory PHP File. This option must be set large enough to be larger than all files in your project PHP Total number of files.
The minimum value of the set value range is 200 and the maximum value is PHP 5.5.6 It was 100000, PHP 5.5.6 And then 1000000. That is, between 200 and 1000000.
opcache.max_accelerated_files=4000

;Sets the expiration time of the cache in seconds,If it is 0, it should be checked every time
opcache.revalidate_freq=60

;It literally means "allow faster shutdown". Its function is to provide a faster mechanism to call the destructor in the code at the end of a single request, so as to speed up the process PHP Response speed and PHP The recycling speed of process resources, so that the application can respond to the next request more quickly. Set it to 1 to use this mechanism.
opcache.fast_shutdown=1

;If enabled (set to 1), OPcache Will be opcache.revalidate_freq Set the number of seconds to detect the timestamp of the file( timestamp)Check whether the script is updated.
If this option is disabled (set to 0), opcache.revalidate_freq Will be ignored, PHP The file will never be checked. This means that if you modify your code, update it to the server, and then request the corresponding function of the updated code on the browser, you will not see the effect of the update
 It is strongly recommended that you set it to 0 in the production environment, update the code, and then restart smoothly PHP and web The server.
opcache.validate_timestamps=0 

;open Opcache File Cache(Experimental), By opening this, We can make Opcache hold opcode Cache to external file, For some scripts, There will be a significant performance improvement.
such PHP Will be/tmp Directory Cache some Opcode Binary export file for, Can span PHP Life cycle exists.
opcache.file_cache=/tmp

View phpinfo:

Test results:

The same interface has increased from hundreds of milliseconds to about 50ms

reference material: What Opcode is and how to use Opcache well

Several Tips for PHP7 to achieve the highest performance  

Posted by bfranco on Mon, 20 Sep 2021 09:23:53 -0700