Home  >  Article  >  Backend Development  >  What is OPCache? How to use OPCache to improve PHP performance?

What is OPCache? How to use OPCache to improve PHP performance?

青灯夜游
青灯夜游forward
2021-05-07 18:47:226214browse

This article takes you through OPCache and introduces in detail how to use OPCache to improve the performance of PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What is OPCache? How to use OPCache to improve PHP performance?

For an interpreted language like PHP, all the code will be loaded and parsed every time it is run. The advantage of this is that the code can be processed at any time. Hot update modifications because we don't need to compile. But this will also bring about a problem, that is, it cannot handle excessive traffic. After all, each time it is loaded, parsed and released, it will increase the burden on the CPU. Usually an 8-core 16G server can reach a CPU utilization rate of more than 60% at about 2000 or 3000 concurrencies. And if you are using a large framework like Laravel, the efficiency will be even lower. At this time, we usually increase the number of servers for load balancing to reduce server pressure. However, the cost of doing so will increase significantly. So, are there any optimization solutions?

In an article on the optimization of PHP7 by Brother Niao on his blog, the first suggestion is to enable OPcache. Of course, another option is to use Swoole. We will talk about Swoole in the future. Today, we will learn OPcache first.

What is OPcache

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

This is an introduction to OPcache in the PHP documentation. In other words, OPcache saves the steps of loading and parsing each time, and caches the script bytecode after the first parsing and compilation into the system. in shared memory. In fact, this is similar to an incomplete compilation.

Languages ​​similar to Java must be packaged and compiled before they can be run online, such as packaging into a jar package. C or C# can be packaged into a .dll or .exe. These packaged files are compiled files. After running them, they will generally remain running, that is, they will become a resident process, and their code will enter the memory. When the program is running, there is no need to interpret or compile it, so the speed is naturally much faster. OPcache also plays a similar role. However, it is not a complete compilation process. We still rely on PHP-FPM to run the script. However, after turning on OPcache, PHP-FPM will first search the memory to see whether there is already relevant cached bytecode. If it is in the memory, it will be accessed directly. If not, it will be interpreted and compiled again and cached. In addition, OPcache is for files, that is to say, if a file is newly added, it will be cached only if it has been run. If it has not been run, it is not in the current shared memory.

Recommended learning: "PHP Video Tutorial"

Install Opcache

OPcache is already an official extension of PHP and is installed with it The package is released together, so we can use --enable-opcache to enable the extension when compiling and installing PHP. It is already the default extension. You can also use the files in the installation package to install OPcache on systems where OPcache is not installed.

cd php-7.4.4/ext/opcache/
phpize
./configure
make && make install

It should be noted that OPcache and Xdebug should not be used together in a production environment. Xdebug itself is not recommended for use in a production environment. 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 should be noted that the OPcache extension is a Zend extension package, so what we need to open is the Zend extension.

zend_extension=opcache.so

Also, you need to enable it.

opcache.enable=1

When OPcache is turned on and we update the code, we will find that the code we just updated is not our latest code. This is because the code has been cached, just like Java, and we need to restart the service. So what is restarting in PHP? 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 -USR2

Thanks for the correction from Zhihu. Restarting PHP-FPM is not the best solution. You should use opcache_reset() to restart manually, or configure opcache.validate_timestamps opcache.revalidate_freq automatically through the php.ini file. Interval compilation, or directly recompile the modified file through opcache_compile_file()

ab Test effect

The content we test is part of the test environment The PHP version used on this 2-core 4G server is PHP7.4. The normal Nginx and PHP configurations and ulimit are also opened to the maximum. The code simply outputs a line of text, but we are using a simple mvc framework, which means that when this code is run, it will load at least several files instead of just one file.

首先我们来看未开启 OPcache 的情况。

What is OPCache? How to use OPCache to improve PHP performance?

接下来是开启了 OPcache 的情况。

What is OPCache? How to use OPCache to improve PHP performance?

很明显,性能有了很大的提高。不仅速度快了很多,吞吐率也是直接上升了几倍。当然,这只是非常简单的一个测试,不过总体看来,确实对单机的性能提升有很大的帮助。最最主要的是,同样的并发情况下,CPU 资源也比未开启的状态下低了70%。

配置参考

在 PHP 的官方文档中,已经为我们给出了一套默认的 OPcache 在 php.ini 中的配置。经过测试,基本没什么问题,当然,现在还没有在生产环境中使用过,还需要进行更多的测试。不过文档中指出,这套配置是可以直接运用到线上的,不过需要注意的是某些使用了注解之类功能的高级框架可能需要注意某些参数。

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

具体的配置说明以及其他的一些配置选项我们可以参考官方文档进行详细的了解。

总结

既然是我们的 PHP 大神鸟哥推荐的,而且也是官方推荐的扩展,我觉得在正式生产环境中使用不会有太大问题。另外,官方也给出了一套可以直接运用于线上生产环境的配置参数,也方便我们直接在线上进行测试。目前在生产环境中,我们只使用了一台服务器来进行测试,并且给它多分配了一些负载过来,从目前的情况来看,这一台机器的运行效率比其他几台的高很多。因为它一方面处理了更多的请求,另一方面它的 CPU 资源占用率还没有其他几台机器高。同时,OPcache 也不需要我们去了解更多的进程协程之类的知识,不像 Swoole 一样的会带来更高的学习成本。所以综上所述,在测试完备的情况下,OPcache 绝对是我们最优先考虑的单机优化方案。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of What is OPCache? How to use OPCache to improve PHP performance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete