Home > Backend Development > PHP7 > body text

Tips to improve PHP7 performance using OPcache extension

coldplay.xixi
Release: 2023-02-17 19:46:01
forward
2457 people have browsed it

Tips to improve PHP7 performance using OPcache extension

Recommended (free): PHP7

##No matter where I am, I will reply you immediately when I see the email.My Email: echo "YUBzYW1lZ28uY29tCg==" | base64 -d


PrefaceIt's half past eleven, and it's time to settle.

When PHP is running, there is such a process. The PHP code is first pre-compiled, the bytecode is generated and then loaded into the memory. Finally, the CPU executes the compiled bytes on the memory. code fragment. We will find that when executing a PHP program, we go through such a process every time. This is not a waste of time. Yes, it is easy to think: Why not follow the C language and compile the source code into a file that can be directly loaded into the memory so Where is brother? Uh-huh?. Get out your rifle and load this bullet

OPcache. Since PHP5.5.0 came out, this zend extension has been built-in.


What is OPcache
OPcache is a Zend extension in PHP that can greatly improve the performance of PHP. 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.


Judge whether it has been extended OPcache

➜  ~ php -m | grep OPcache
Zend OPcache
Zend OPcache
Copy after login
If it is not enabled, you can enable it in the php.ini configuration

/home /samego/service/php7.2/php.ini

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

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

opcache.enable=1
Copy after login
Let’s 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, set A value greater than 64 will do.

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 to clean up memory, however, in my benchmark tests, it was slower. Maybe this will 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. Learning from the Laravel Community


Laravel OPcache

  • install

    ➜  ~ composer require appstract/laravel-opcache
    Copy after login
  • configure

    ➜  ~ php artisan vendor:publish --provider="Appstract\Opcache\OpcacheServiceProvider" --tag="config"
    Copy after login
  • 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
Copy after login

Scenario test to wait and see

Personally, I prefer the data-speaking

scenario: (1) Requesting the GET interface (2) The number of tests is 10 (3) The number of concurrency is 100

case non-extension

1000 requests, took 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
Copy after login

case had extend

1000 The request took 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
Copy after login

I was very happy to see this set of data, extremely happy. In terms of performance, there is such a sharp contrast that I won’t say anything ~OPcache is right                    

The above is the detailed content of Tips to improve PHP7 performance using OPcache extension. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!