Which of PHP7 opens OPcache and Swoole to improve? Performance improvement comparison

For this test, we use the Laravel framework. As one of the most popular PHP frameworks, laravel is widely recognized and welcomed. At the same time, due to the high level of integration, a large number of files are loaded in each run, and a large number of closures and magic methods are used, resulting in a heavy laravel framework and extremely poor concurrent performance. Both OPcache and Swoole are php extensions. This time it aims to compare the acceleration effects of Laravel applications after the two extensions are enabled.

Preliminary preparation

The host used for the test is a virtual machine, which is configured in a dual-core 4GB personal computer. The virtual machine system is linux, the http server uses nginx, and the lnmp script is used to install nginx, mysql, and php. The Laravel framework is version 7.X.

  • Configure the site, configure the virtual host in the nginx server block

server{ listen 80; root “/vagrant/www/laravel7/public”; server_name test.laravel.com; index index.html index.php; location / { try_files $uri u r i / / i n d e x . p h p ? uri/ /index.php? uri//index.php?args; } location ~ \.php$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED d o c u m e n t _ r o o t document\_root document_rootfastcgi_path_info; fastcgi_param SCRIPT_FILENAME d o c u m e n t _ r o o t document\_root document_rootfastcgi_script_name; include fastcgi_params; } }

编辑/etc/hosts文件,在新行添加127.0.0.1  test.laravel.com
  • Open the project, create a new controller TestController, and create a new test method in it:
<?php
namespace App\Http\Controllers;
 
 
 
class TestController extends Controller
{
    public function test()
    {
    	return 123;
    }
 
    
}
  • Register a route in routes/api.php:
Route::get('test', 'TestController@test');

In the app/Http/Kernel file, turn off the frequency limit middleware throttle.

Do not open opcache and laravel

Modify the php-fpm.conf file, modify the pm and pm.max_children configuration, set pm to static and pm.max_children to 50 to obtain better concurrency performance.

[www] listen = /tmp/php-cgi.sock listen.backlog = -1 listen.allowed_clients = 127.0.0.1 listen.owner = www listen.group = www listen.mode = 0666 user = www group = www pm = static pm.max_children = 50 pm.start_servers = 10 pm.min_spare_servers = 10 pm.max_spare_servers = 20 request_terminate_timeout = 100 request_slowlog_timeout = 0 slowlog = var/log/slow.log

  • Use ab pressure test after restarting fpm: ab -n 1000 -c 100 http://test.laravel.com/api/test
Server Software:        nginx
Server Hostname:        test.laravel.com
Server Port:            80
 
Document Path:          /api/test
Document Length:        3 bytes
 
Concurrency Level:      100
Time taken for tests:   148.306 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      253000 bytes
HTML transferred:       3000 bytes
Requests per second:    6.74 [#/sec] (mean)
Time per request:       14830.553 [ms] (mean)
Time per request:       148.306 [ms] (mean, across all concurrent requests)
Transfer rate:          1.67 [Kbytes/sec] received

The concurrency at this time is about 7 qps

Open OPcache

Open opcache in the configuration file php.ini file

zend_extension=“opcache.so” opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 opcache.interned_strings_buffer=8

  • After restarting fpm, use ab to test: ab -n 1000 -c 100 http://test.laravel.com/api/test
Server Software:        nginx
Server Hostname:        test.laravel.com
Server Port:            80
 
Document Path:          /api/test
Document Length:        4 bytes
 
Concurrency Level:      100
Time taken for tests:   11.006 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      254000 bytes
HTML transferred:       4000 bytes
Requests per second:    90.86 [#/sec] (mean)
Time per request:       1100.590 [ms] (mean)
Time per request:       11.006 [ms] (mean, across all concurrent requests)
Transfer rate:          22.54 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   4.3      0      16
Processing:   409 1069 152.0   1092    1414
Waiting:      408 1069 152.0   1092    1414
Total:        424 1070 149.6   1092    1414
 
Percentage of the requests served within a certain time (ms)
  50%   1092
  66%   1126
  75%   1149
  80%   1162
  90%   1203
  95%   1242
  98%   1280
  99%   1309
 100%   1414 (longest request)

At this time, it has reached 90qps, and the performance is more than 10 times that when it is not turned on! .

Use swoole acceleration package

Open source laravel-swoole acceleration package

Run the composer command to install in the project directory; configure it in the nginx configuration file to forward the request to the port monitored by swoole.

Pressure test with ab: ab -n 1000 -c 100 http://test.laravel.com/api/test

Server Software:        nginx
Server Hostname:        test.laravel.com
Server Port:            80
 
Document Path:          /api/test
Document Length:        4 bytes
 
Concurrency Level:      100
Time taken for tests:   1.158 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      225000 bytes
HTML transferred:       4000 bytes
Requests per second:    863.46 [#/sec] (mean)
Time per request:       115.813 [ms] (mean)
Time per request:       1.158 [ms] (mean, across all concurrent requests)
Transfer rate:          189.72 [Kbytes/sec] received

Take off at speed! Achieved 800qps!

That is more than a hundred times?

to sum up

Of course, this is only a relatively simple test, but in general, the improvement of the performance of php scripts by opcache extension and swoole extension is still very obvious.

Yae Sakura: The Growth Road of PHP Internet Architect * The Ultimate Guide to "Design Patterns"

PHP Internet Architect 50K Growth Guide + Industry Problem Solving Guide (Continuous Update)

Interview with 10 companies, get 9 offers, PHP interview questions in 2020

★If you like my article and want to communicate and learn with more senior developers, get more technical consultation and guidance related to interviews with big companies, welcome to join our group, password: phpzh

The latest PHP advanced tutorial in 2020, full series!

file

Guess you like

Origin blog.csdn.net/weixin_43814458/article/details/108734139