Let Bullets Fly~Improve PHP7 Performance with OPcache Extension| Laravel

No matter where I am, I will reply you immediately when I see the email.My Email: echo "YUBzYW1lZ28uY29tCg==" | base64 -d Preface It's 11:30 and the settling time is up.

When PHP is running, there is a process that precompiles the PHP code, generates the byte code, then loads it into memory, and finally executes the compiled byte code fragment on memory by the CPU.We will find that such a process goes through every Time we execute a PHP program, which is not a waste of Time. Yes, it's easy to think about: Why not look at C++ language and compile the source code so that it can be loaded directly into memory sogo?Eh?.Take out your rifle and load OPcache with this bullet.This zend extension has been built in since PHP 5.5.0 came out.

What is OPcache OPcache is a Zend extension in PHP that can greatly improve PHP performance. OPcache improves PHP performance by storing precompiled byte codes of PHP scripts in shared memory. The advantage of storing precompiled byte codes is that it saves the overhead of loading and parsing PHP scripts each time.

Judge whether it has been extended OPcache

➜  ~ php -m | grep OPcache
Zend OPcache
Zend OPcache

If not, it can be turned on in the php.ini configuration /home/samego/service/php7.2/php.ini

➜  ~ echo zend_extension="opcache.so" >> /home/samego/service/php7.2/php.ini

About OPcache configure Next, we need to enable OPcache in the PHP configuration file (turned off by default):

opcache.enable=1

Let's continue with some optimization of OPcache:

opcache.memory_consumption=512

This configuration indicates that you want to allocate memory space (in MB) to OPcache by setting a value greater than 64.

opcache.interned_strings_buffer=64

This configuration indicates that you want to allocate space (in MB) to the actual string by setting a value greater than 16.

opcache.max_accelerated_files=32531

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

opcache.validate_timestamps=0

The reconfiguration value is used to revalidate the script, and if set to 0 (best performance), you need to manually clear the OPcache after each PHP code change.If you don't want to clean up manually, you can set it to 1 and revalidate the interval by configuring opcache.revalidate_freq, which may consume some performance because changes need to be checked every x seconds.

opcache.save_comments=1

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

opcache.fast_shutdown=0

Fast shutdown will give you a faster memory cleanup mechanism, but in my benchmarks, it's slower and may result in some performance improvements for your application, but you need to try it yourself.

So the final configuration optimization is long 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

You can experiment with these configuration values, depending on your application size and server configuration. Learn in the Laravel Community

Laravel OPcache

  • install
➜  ~ composer require appstract/laravel-opcache
  • configure
➜  ~ php artisan vendor:publish --provider="Appstract\Opcache\OpcacheServiceProvider" --tag="config"
  • command
# Clear OPcache:
➜  ~ php artisan opcache:clear

# Show OPcache config:
➜  ~ php artisan opcache:config

# Show OPcache status:
➜  ~ php artisan opcache:status

# Pre-compile your application code:
➜  ~ php artisan opcache:optimize

Waiting Scene Test

Individuals prefer to talk about data Scenario: (1) Request GET interface (2) Test number 10 (3) Concurrency number 100

case non-extension

1000 requests, 32.32 seconds, 30.94 requests per second

Transactions:		        1000 hits
Availability:		      100.00 %
Elapsed time:		       32.32 secs
Data transferred:	        0.97 MB
Response time:		        0.32 secs
Transaction rate:	       30.94 trans/sec
Throughput:		        0.03 MB/sec
Concurrency:		        9.96
Successful transactions:        1000
Failed transactions:	           0
Longest transaction:	        0.44
Shortest transaction:	        0.11

case had extend

1000 requests, 2.94 seconds, 340.14 requests per second

Transactions:		        1000 hits
Availability:		      100.00 %
Elapsed time:		        2.94 secs
Data transferred:	        0.97 MB
Response time:		        0.03 secs
Transaction rate:	      340.14 trans/sec
Throughput:		        0.33 MB/sec
Concurrency:		        9.86
Successful transactions:        1000
Failed transactions:	           0
Longest transaction:	        0.29
Shortest transaction:	        0.01

I'm so happy to see this data.In terms of performance, there's such a stark contrast, let me just say ~OPcache is right (3[] Good night Value comes from technology, technology comes from sharing!

Keywords: Programming PHP Laravel Fragment

Added by badviking on Sun, 07 Jul 2019 20:27:41 +0300