Home > PHP Framework > Laravel > Make your Laravel application run faster! (Utilizing PHP OPcache)

Make your Laravel application run faster! (Utilizing PHP OPcache)

藏色散人
Release: 2020-04-22 13:31:58
forward
2732 people have browsed it

What is Opcache

Every time a PHP script is executed, the script needs to be compiled into bytecode, and OPcache can cache the bytecode, so, The next time the same script is requested, the script does not need to be recompiled, which greatly saves the execution time of the script, allowing the application to run faster and also saving server overhead.

Speak with numbers

Of course we want to know what kind of optimization has been done. Although the performance improvement is highly dependent on the configuration of the application and server, we can run Have a general understanding of benchmarks.

For this purpose, I specially prepared a very low configuration machine: 1 core CPU, 1G memory to run the Apache benchmark test. What I request is the default welcome page of Laravel 5.4, allowing 10 concurrent requests to continue accessing for 1 minute. The following are the benchmark results of turning off OPcache:

OPcache disabled: 10.18 requests per second
Copy after login

For such a low-configuration server, this is not Not bad, but we can do better. The benchmark test results with OPcache turned on are as follows (using the default OPcache configuration):

Enabled with default values: 34.52 requests per second
Copy after login

The gap is still huge! Next, we will optimize the OPcache configuration, and the benchmark test will perform better:

Enabled with optimized values: 42.53 requests per second
Copy after login

Have you been able to use this?

Sounds awesome, but how to use it

First, we need to ensure that OPcache is installed on the server. Starting from PHP 5.5, OPcache has It has become part of the core of PHP, so for Laravel developers, there is basically no need to manually install this extension.

Of course, if you are not sure, you can confirm by looking at phpinfo():

<?php
phpinfo();
Copy after login

This script will display all PHP installed extensions. Search for "OPcache" on the page. If found, it proves that it has been installed. If not, you need to install it yourself.

Next, we need to enable OPcache in the PHP configuration file (the default is closed):

opcache.enable=1
Copy after login

Next we continue to make some optimization configurations for OPcache:

opcache.memory_consumption=512
Copy after login

This configuration indicates the memory space (unit: MB) you want to allocate to OPcache. Just set a value greater than 64.

opcache.interned_strings_buffer=64
Copy after login

This configuration indicates the space (unit: MB) you want to allocate to the actual string. Just set a value greater than 16.

opcache.max_accelerated_files=32531
Copy after login

This configuration indicates how many scripts can be cached. Set this value as close as possible to (or greater than) the number of scripts contained in the project.

opcache.validate_timestamps=0
Copy after login

Change the configuration value for revalidation scripts. If set to 0 (best performance), you need to manually clear the OPcache after each PHP code change. If you don't want manual purging, you can set it to 1 and configure the revalidation interval via opcache.revalidate_freq, which may cost some performance since changes need to be checked every x seconds.

opcache.save_comments=1
Copy after login

This configuration will leave comments in the script. I recommend turning this option on because some libraries depend on this configuration and I can't find any benefit of turning it off.

opcache.fast_shutdown=0
Copy after login

Fast shutdown will provide a faster mechanism for clearing memory, however, in my benchmark tests, it is slower. This may bring some performance improvements to the application, but you need to try it yourself.

So, the final configuration optimization looks like this:

opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=32531
opcache.validate_timestamps=0
opcache.save_comments=1
opcache.fast_shutdown=0
Copy after login

You can experiment with these configuration values. The specific configuration values ​​depend on your application size and server configuration.

Finally, save this configuration file and restart the web server, your application will definitely become faster.

Preparing Laravel Application

As mentioned earlier, if opcache.validate_timestamps is set to 0, we need to manually clear the OPcache after each modification of the PHP code. For this purpose, I created an extension package to provide corresponding Artisan commands to handle OPcache cleanup: https://github.com/appstract/laravel-opcache.

After installing the extension, just execute the following command to clean up the OPcache:

php artisan opcache:clear
Copy after login

In addition, the extension package also provides some other useful tools, which you can use in the project's Seen on GitHub page .

This article is reprinted, original address: https://xueyuanjun.com/post/7326

The above is the detailed content of Make your Laravel application run faster! (Utilizing PHP OPcache). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:xueyuanjun.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template