PHP7 how to open opcode to build powerful performance PHP tips

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 below with the small series to learn together

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 Opcache
Opcache.enable=1

; CLI Environment, PHP enabled Opcache
Opcache.enable_cli=1

; Opcache shared memory storage size, in megabytes
opcache.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), which, if set to 16, represents 16MB, which is 4MB by default.
Opcache.interned_strings_buffer=8

This option is used to control the maximum number of PHP files that 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

Set the expiration time of the cache (in seconds), which is 0 to check every time
Opcache.revalidate_freq=60

, literally, is "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 if you modify your code and then you update it to the server and then request the updated code on the browser, you will not see the effect of the update.
It is strongly recommended that you set the 0 in the production environment, update the code, and then smooth restart the PHP and Web servers.
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 a significant performance improvement.
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.

Summarize

Articles you may be interested in:

PHP Tips for solving rabbit bunny problems based on recursive algorithm

About exception handling in thinkphp _php instance

PHP7 PHP tips for uploading images based on curl

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.