Explain in detail the performance improvement comparison between php7 enabling opcache and swoole

Time:2022-7-29
catalogue
  • preparation in advance
  • Do not enable opcache and laravel
  • Open opcache
  • Using the swoole acceleration package
  • summary

preparation in advance

The host used in 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 adopts nginx, and the LNMP script is used to install nginx, MySQL and PHP. The laravel framework is version 7.x.

Configure the site and configure the virtual host in the server block of nginx

server{ listen 80; root “/vagrant/www/laravel7/public”; server_name test.laravel.com; index index.html index.php; location / { try_files $uri $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 $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

Edit the /etc/hosts file and add 127.0.0.1 test.laravel.com in the new line

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', '[email protected]');

In the app/http/kernel file, turn off the frequency limiting middleware throttle.

Do not enable opcache and laravel

Modify php-fpm.conf file, and modify PM and pm max_ Children configuration, PM set to static, pm.max_ Children is set to 50 for 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

Restart FPM and use AB pressure measurement: 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

At this time, the concurrency is about 7 QPS

Open opcache

Open opcache in the configuration file php.ini

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 pressure measurement: 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, the performance reaches 90qps, which is more than 10 times that when it is not turned on!.

Using the swoole acceleration package

Open source laravel spool acceleration package

Run the composer command under the project directory to install; Configure in the configuration file of nginx, and forward the request to the port monitored by spool.

Pressure measurement 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! 800qps!

That is more than 100 times?

summary

Of course, this is only a relatively simple test, but in general, the opcache extension and the spool extension have significantly improved the performance of PHP scripts.

The above is a detailed explanation of the performance improvement and comparison between php7 and swoole. For more information about the performance improvement and comparison between php7 and swoole, please pay attention to other relevant articles of developeppaer!

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]