PHP7開啟OPcache和Swoole哪個提升更大?效能的提升對比

2020-09-23 12:01:11

這次測試咱們採用Laravel框架。laravel作為最熱門的php框架之一,廣受認可與歡迎。同時由於整合度很高,每次執行都載入了大量檔案,加之使用了大量的閉包、魔術方法,導致laravel框架很重,並行效能極差。OPcache和Swoole都是php的擴充套件,這次旨在比較這兩個擴充套件分別開啟後對Laravel應用的加速效果。

前期準備

測試所用的主機為虛擬機器器,虛擬機器器設定在雙核4GB的個人電腦中。虛擬機器器系統為linux,http伺服器採用nginx,用lnmp指令碼安裝nginx、mysql、php。Laravel框架為7.X版本。

  • 設定站點,在nginx的server塊中設定虛擬主機

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
  • 開啟專案,新建控制器TestController,在裡面新建一個test方法:
<?php
namespace App\Http\Controllers;
 
 
 
class TestController extends Controller
{
    public function test()
    {
    	return 123;
    }
 
    
}
  • 在routes/api.php中註冊一個路由:
Route::get('test', 'TestController@test');

在app/Http/Kernel檔案中,關掉頻率限制中介軟體throttle。

不開啟opcache和laravel

修改php-fpm.conf檔案,修改pm和pm.max_children 設定,pm設定為static,pm.max_children設定為50,以獲得較好的並行效能。

[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

  • 重新啟動fpm後用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:        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

此時的並行大約為為 7 qps

開啟OPcache

在組態檔php.ini檔案中開啟opcache

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

  • 重新啟動fpm後,用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:   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)

此時的達到了 90qps,效能是未開啟時的 10 倍以上!。

使用swoole加速包

開源的laravel-swoole加速包

在專案目錄下執行composer命令安裝;在nginx的組態檔中設定,將請求轉發到swoole監聽的埠。

用 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

速度起飛!達到了800qps!

也就是一百多倍?

總結

當然這只是一個比較簡單的測試,但是總的來說opcache擴充套件和swoole擴充套件對php指令碼效能的提升還是很明顯的。

八重櫻:PHP 網際網路架構師成長之路*「設計模式」終極指南

PHP 網際網路架構師 50K 成長指南+行業問題解決總綱(持續更新)

面試10家公司,收穫9個offer,2020年PHP 面試問題

★如果喜歡我的文章,想與更多資深開發者一起交流學習的話,獲取更多大廠面試相關技術諮詢和指導,歡迎加入我們的群啊,暗號:phpzh

2020年最新PHP進階教學,全系列!

file