PHP7 how to open opcode to create a powerful performance explanation

Source: Internet
Author: User
Tags php source code
This article mainly introduces to you about PHP7 how to open opcode to build a strong performance of the relevant information, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends to study together below

Objective

Bird brother said in the blog, the performance of several tips to improve PHP 7, the first one is to open Opcache:

Remember to enable Zend Opcache because PHP7 is faster than PHP-5.6 enabled even if Opcache is not enabled, so there was a time when the Opcache was not enabled before the test period.

So what is Opcache?

Opcache's previous life was optimizer+, an official PHP company Zend developed a closed-source, but free-to-use PHP optimization accelerator component. optimizer+ the script files generated by the PHP code precompilation Opcode Cached in shared memory for later reuse, thus avoiding the time consuming of the code being compiled again from the disk. It also applies some code optimization patterns to make code execution faster. Thus speeding up the execution of PHP.

The normal execution process for PHP is as follows

Request requests (NGINX,APACHE,CLI, etc.) the-->zend engine reads the. php file and scans its dictionaries and expressions--to parse the file--Create the computer code to execute (called opcode)-- Last execution opcode--> response return

Each time the PHP script will execute the above steps, if the PHP source code does not change, then opcode will not change, obviously there is no need to regenerate opcode every time, combined with the ubiquitous caching mechanism in the web, we can cache opcode, It is not faster to access the cached opcode directly later, and the flowchart after enabling opcode caching is as follows:

The goal of the Opcode cache is to avoid duplication of compilation and reduce CPU and memory overhead.

The installation of Opcache is described below

Installation:

1, find the extension of Opcache, my is php7.1

Yum List php71*

2. Installation extension

Yum Install php71w-opcache.x86_64

Configuration:

Zend_extension=opcache.so[opcache]; open opcacheopcache.enable=1; CLI Environment, PHP enabled Opcacheopcache.enable_cli=1;opcache shared memory storage size, unit mbopcache.memory_consumption=128; PHP uses a technique called string interning to improve performance. For example, if you use the string "Foobar" in the code 1000 times, in PHP, only the first time you use the string to allocate an immutable memory area to store the string, the other 999 times will directly point to the memory area. This option will raise the feature to a level--by default, this immutable memory area will only exist in a single php-fpm process, and if this option is set, it will be shared across all php-fpm processes. In larger applications, this can be very effective in saving memory and improving application performance. The value of this option is in megabytes (megabytes), and if it is set to 16, it is 16MB, and the default is 4mbopcache.interned_strings_buffer=8; This option controls how many PHP files can be cached in memory. This option must be set large enough to be larger than the sum of all the PHP files in your project. Set value range The minimum value is 200, the maximum value before PHP 5.5.6 is 100000,php 5.5.6 and after is 1000000. That means between 200 and 1000000. opcache.max_accelerated_files=4000; Sets the expiration time of the cache (in seconds), which is 0 to check the opcache.revalidate_freq=60 every time; literally, "Allow faster shutdown." Its role is to provide a faster mechanism to invoke the destructor in the code at the end of a single request, speeding up the response of PHP and the retrieval of PHP process resources so that the application can respond more quickly to the next request. You can use this mechanism by setting it to 1. opcache.fast_shutdown=1; If enabled (set to 1), Opcache checks the file's timestamp (timestamp) in the number of seconds opcache.revalidate_freq set to check if the script is updated. If this option is disabled (set to 0), Opcache.revalidate_freq will be ignored and PHP files will never be checked. This means that ifYou modify your code, then you update it to the server, and then request the updated code on the browser to correspond to the function, you will not see the effect of the update is strongly recommended that you set the production environment to 0, after updating the code, and then smooth restart the PHP and Web server. opcache.validate_timestamps=0; open Opcache file Cache (experimental), by opening this, we can let Opcache cache the opcode cache to an external file, for some scripts, There will be significant performance gains. This will allow PHP to cache some opcode binary export files in the/tmp directory, which can exist across the PHP life cycle. opcache.file_cache=/tmp

View Phpinfo:

Test results:

The same interface was lifted from the previous hundreds of milliseconds to about 50ms now.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.