壓測Laravel5.8、ThinkPHP5、Yii2
(1)測試環境搭建
這裏用virtualbox搭建Linux虛擬機,用vagrant管理虛擬機。Web服務器用nginx,可以用lnmp腳本安裝和管理nginx、php、mysql。
(2)配置站點
在nginx配置文件的server塊中配置虛擬主機。
配置laravel5.8項目
server{
listen 80;
root "/vagrant/www/laravel5.8/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;
}
}
配置yii2項目
server{
listen 80;
root "/home/wwwroot/yii2/web";
server_name test.yii2.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;
}
}
配置thinkphp5項目
server{
listen 80;
set $root /vagrant/www/myshop/public;
#root /vagrant/shop/public;
server_name test.tp5.com;
location / {
root $root;
index index.html index.php;
if ( -f $request_filename) {
break;
}
if ( !-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
location ~ .+\.php($|/) {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
include fastcgi_params;
}
}
配置完後,保存,命令行運行service nginx reload使配置生效。
(3)壓測
這裏在每個框架中定義一個路由,都路由到控制器中,控制器中只是簡單echo一個字符串’HELLO’,不做其他處理。然後命令行輸入ab命令壓測:
shell > ab -n 1000 -c 100 url
壓測laravel5.8:
Server Software: nginx
Server Hostname: test.laravel.com
Server Port: 80
Document Path: /ab-test
Document Length: 5 bytes
Concurrency Level: 100
Time taken for tests: 123.103 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 935656 bytes
HTML transferred: 5000 bytes
Requests per second: 8.12 [#/sec] (mean)
Time per request: 12310.314 [ms] (mean)
Time per request: 123.103 [ms] (mean, across all concurrent requests)
Transfer rate: 7.42 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 2.9 0 12
Processing: 4231 11796 1750.7 11587 16269
Waiting: 4231 11796 1750.7 11587 16269
Total: 4242 11797 1749.2 11587 16269
Percentage of the requests served within a certain time (ms)
50% 11587
66% 12028
75% 12579
80% 12943
90% 13874
95% 15068
98% 15316
99% 15785
100% 16269 (longest request)
壓測tp5:
Server Software: nginx
Server Hostname: test.tp5.com
Server Port: 80
Document Path: /ab-test
Document Length: 5 bytes
Concurrency Level: 100
Time taken for tests: 15.166 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 183000 bytes
HTML transferred: 5000 bytes
Requests per second: 65.94 [#/sec] (mean)
Time per request: 1516.571 [ms] (mean)
Time per request: 15.166 [ms] (mean, across all concurrent requests)
Transfer rate: 11.78 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 4.0 0 16
Processing: 296 1449 241.2 1478 1974
Waiting: 296 1449 241.2 1478 1974
Total: 303 1451 238.4 1479 1974
Percentage of the requests served within a certain time (ms)
50% 1479
66% 1520
75% 1554
80% 1573
90% 1645
95% 1686
98% 1724
99% 1756
100% 1974 (longest request)
壓測yii2:
Server Software: nginx
Server Hostname: test.yii2.com
Server Port: 80
Document Path: /index.php?r=test/test
Document Length: 5 bytes
Concurrency Level: 100
Time taken for tests: 27.086 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 303000 bytes
HTML transferred: 5000 bytes
Requests per second: 36.92 [#/sec] (mean)
Time per request: 2708.586 [ms] (mean)
Time per request: 27.086 [ms] (mean, across all concurrent requests)
Transfer rate: 10.92 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 3.5 0 13
Processing: 494 2609 447.8 2704 3230
Waiting: 494 2609 447.8 2704 3230
Total: 506 2610 445.0 2704 3230
Percentage of the requests served within a certain time (ms)
50% 2704
66% 2784
75% 2832
80% 2865
90% 2946
95% 2993
98% 3035
99% 3073
100% 3230 (longest request)
(4)分析和對比
可以看到在併發請求下,laravel框架的性能明顯比tp5和yii2差要差一些。總共發起1000次請求,每次100個併發請求,,三個框架的qps對比:
框架 | qps |
---|---|
Laravel5.8 | 8.12 |
ThinkPHP5 | 65.94 |
Yii2 | 36.92 |
造成這種結果的原因,是每個框架啓動過程中加載的資源不同。如果你研究過laravel的啓動過程,會發現laravel在完成一次請求的過程中加載(require)了大量的類文件,做了很多驗證、過濾、加密操作。這些操作會消耗更多內存,同時也會使完成一次請求的耗時更長。有利有弊,用laravel寫php代碼確實效率很快、代碼優雅,但卻犧牲了相當一部分性能。如果對併發性能有較高要求,建議用tp5做接口,tp5相對laravel更輕量。
(5)用swoole擴展加速laravel?
輪子已經有人造好了,laravel-s。有時間再測測。