用ab压测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。有时间再测测。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章