Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OPCache to improve performance in PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use OPCache to improve performance in PHP. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Using OPCache to improve the performance of PHP

For an interpretive language like PHP, all the code is loaded and parsed every time it is run, and the advantage is that the code can be hot updated and modified at any time, because we don't need to compile. But this also brings a problem, that is, it can not carry too much traffic. After all, every time you load and parse and release, it will increase the burden on CPU. Usually, an 8-core 16G server can achieve a CPU utilization rate of more than 60% when the concurrency is about 2 or 3000. And if you use a large framework like Laravel, it will be even less efficient. At this time, we usually do load balancing by increasing the number of servers, so as to reduce the pressure on the server. However, the cost of doing so will be much higher. So, is there any optimization plan?

In an article on his blog about the optimization of PHP7, the first piece of advice is to turn on OPcache. Of course, another option is to use Swoole. We will talk about Swoole in the future. Today, we will first learn about OPcache.

What is OPcache?

OPcache improves the performance of PHP by storing the precompiled bytecode of PHP scripts in shared memory. The advantage of storing precompiled bytecode is that it saves the overhead of loading and parsing PHP scripts each time.

This is an introduction to OPcache in the PHP documentation, that is, OPcache saves the steps of loading and parsing each time, caching the first parsed compiled script bytecode into the system's shared memory. In fact, this is similar to an incomplete compilation.

Languages like Java are packaged and compiled before they can be run online, such as a jar package. C++ or C# can be packaged into a .dll or .exe. These packaged files are compiled files, which usually remain running after they are run, that is, they become a resident process, and their code goes into memory. When the program is running, it does not need to be interpreted or compiled, and the natural speed is much faster. OPcache also plays a similar role. It is just that it is not a complete compilation process, we still rely on PHP-FPM to run the script, but after opening OPcache, PHP-FPM will first find out from memory whether there is a relevant cached bytecode in memory, and if so, it will directly take it, and if not, it will explain it again and cache it after compilation. In addition, OPcache is for files, that is, if a file is newly added, it will be cached only if it has been run, and if it has not been run, it is not in the current shared memory.

Install Opcache

OPcache is already the official extension of PHP and is released with the installation package, so we can use-- enable-opcache to open the extension when compiling and installing PHP, which is already the default extension. You can also use the files in the installation package to install on a system that does not have OPcache installed.

Cd php-7.4.4/ext/opcache/phpize./configuremake & & make install

It is important to note that OPcache and Xdebug should not be used together in a production environment. Xdebug itself is not recommended for use in a production environment, and if you must use it at the same time, you need to load OPcache first and then load Xdebug.

After the extension is installed, open the extension in the php.ini file. It is important to note that the OPcache extension is the Zend expansion pack, so what we need to open is the Zend extension.

Zend_extension=opcache.so

In addition, you need to enable it.

Opcache.enable=1

When OPcache is enabled, we will update the code and we will find that the code that has just been updated is not our latest code. This is because the code has been cached, and just like Java, we need to restart the service. So what is the restart on the PHP side? Of course, just restart our PHP-FPM, just use the kill-USR2 command to restart the main process. A quick restart command is also given here.

Ps-ef | grep "php-fpm: master" | grep-v grep | cut-c 9-15 | xargs kill-USR2ab test effect

The content of our test is a 2-core 4G server in the test environment, using PHP7.4 version of PHP, normal Nginx and PHP configuration, ulimit is also turned on to the maximum. The code simply outputs a line of text, but we use a simple mvc framework, which means that the code runs with at least a few files, rather than a simple one.

First of all, let's take a look at the situation where OPcache is not enabled.

Next is the case where OPcache is turned on.

Obviously, the performance has been greatly improved. Not only is the speed much faster, but the throughput has also increased several times directly. Of course, this is just a very simple test, but overall, it does help a lot to improve the performance of a single machine. Most importantly, CPU resources are 70% lower in the same concurrency than when they are not turned on.

Configuration referenc

In the official documentation of PHP, we have been given a set of default configuration of OPcache in php.ini. After testing, there is basically no problem, of course, it has not been used in the production environment, and more tests are needed. However, the documentation points out that this configuration can be applied directly online, but it is important to note that some advanced frameworks that use features such as annotations may need to pay attention to certain parameters.

Opcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_files=4000opcache.revalidate_freq=60opcache.fast_shutdown=1opcache.enable_cli=1

After reading the above, do you have any further understanding of how to use OPCache to improve performance in PHP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report