OpcachePHP-PHP source code

Source: Internet
Author: User
OPcache saves pre-compiled PHP scripts to the shared memory to improve PHP performance. The advantage of storing pre-compiled bytecode is that it saves the overhead of loading and parsing PHP scripts each time. PHP5.5.0 and later versions have bound OPcache extensions, next let's take a look at the new opcachePHP bytecode cache extension details OPcache improves PHP performance by storing pre-compiled PHP bytecode in the shared memory, the advantage of storing pre-compiled bytecode is that it saves the overhead of loading and parsing PHP scripts each time. The OPcache extension has been bound to PHP 5.5.0 and later versions. Let's take a look at the new opcache PHP bytecode cache extension details.

Script ec (2); script

The bytecode cache component Zend Optimizer + is now named Zend opcache. In addition, php 5.5 will be integrated into official php components, so there is no need to install other APC and eAccelerator ..

Both APC and Opcache are bytecode caches, that is, when PHP is compiled, it first converts the php code to bytecode, And the bytecode is then executed.

During the second PHP File Execution, it will still be converted to bytecode again. However, in many cases, the file content is almost the same, such as static HTML files. The generated content will not change for a long time, user access requests are directly sent to the client browser by the server. It does not need to be parsed and constructed by PHP.

The bytecode data in the memory can be directly cached for secondary compilation. In this way, the program will be faster and the cpu consumption will be reduced.

For more information, see the two articles.

New Generation PHP acceleration plug-in Zend Opcache

Performance Comparison of php zend opcache VS apc

I mainly used to test the default index. php homepage of phpcmsV9.5.4. There is no data content, but there is an SQL query operation.

Apache2.2.6 32Bit server, PHP 5.4.26 ts, mysql 5.6.16 64Bit

AB Version This is apacheloud, Version 2.3 <$ Revision: 655654 $>

Requests: 1000

Concurrency: 10

Opcache not loaded

First test


Throughput 38.46 rps, 26.001 s elapsed, 260.015 MS per request

Second test

Throughput increased by 40.73 rps, 24.554 s elapsed, 245.544 MS per request

Load opcache for testing

Php_opcache-7.0.3-5.4-ts-vc9-x86 for opcache

Opcache configuration information

1 opcache.memory_consumption=128

2 opcache.interned_strings_buffer=8

3 opcache.max_accelerated_files=4000

4 opcache.revalidate_freq=60

5 opcache.fast_shutdown=1

6 opcache.enable_cli=1

Cache status after opcache is configured for the first time

Cache hits 1

Cache misses miss count 1

Memory usage 225.2Kb

Opcache first test

Throughput increased to 49.11rps, total time consumption of 20.361 s, each request time consumption decreased to 203.612 MS

You can view the hit quantity of opcache in phpinfo.

The number of hits has reached 43957, and the memory usage is 2.31 Mb.

Opcache second test

Throughput increased to 47.87rps, total time consumption of 20.888 s, each request time consumption decreased to 208.882 MS

The two stress tests after opcache do not change much. Each request takes less than 1 ms, and the throughput is also 1 ~ 2 rps

However, when opcache is not enabled, the total time consumption is reduced by 4 ~ 6 s, each request consumes 40 ~ less time ~ 60 ms. The throughput has also increased by 8%.

This is an effective performance improvement without modifying a line of code.

PHP files are compiled into bytecode for memory caching. If the code and content variables are fixed in the production environment

The cached content can be returned quickly, and the user will get a quick response.

However, we recommend that you do not enable opcache for local development. Otherwise, the latest value will not be available.

For example:

Header ('content-type: text/html; charset = UTF-8 ');

$ Str = 'abc ';
Echo $ str; // output abc
?>


Assign $ str a new value.

Header ('content-type: text/html; charset = UTF-8 ');

$ Str = 'new abc ';
Echo $ str; // The output is abc.
?>

This is because $ str has been compiled as a bytecode. When accessed again, the corresponding cache can still be found in the memory, and no re-compilation is performed. The returned value is still the original value abc.

However, opcache has a parameter that can be used to set the cache duration.


Opcache. force_restart_timeout (default "180 ")

The default time is 180 seconds. We recommend that you do not enable opcache locally.

Note: an official Note is provided. If opcache is to be loaded with xdebug at the same time, opcache must be loaded before xdebug.

But I tested it locally. xdebug loads opcache first, and then starts normally. xdebug and opcache are loaded successfully, and the opcache cache is also normal.

But install and load according to official suggestions. Otherwise, a strange error may occur.

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.