Home » Code » Laravel性能初探

Laravel性能初探

硬件:2核CPU +8G 内存:

[root@VM_0_9_centos laravel]# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 79
model name	: Intel(R) Xeon(R) CPU E5-26xx v4
stepping	: 1
microcode	: 0x1
cpu MHz		: 2394.446
cache size	: 4096 KB
physical id	: 0
siblings	: 2
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch bmi1 avx2 bmi2 rdseed adx xsaveopt
bogomips	: 4788.89
clflush size	: 64
cache_alignment	: 64
address sizes	: 40 bits physical, 48 bits virtual
power management:

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 79
model name	: Intel(R) Xeon(R) CPU E5-26xx v4
stepping	: 1
microcode	: 0x1
cpu MHz		: 2394.446
cache size	: 4096 KB
physical id	: 0
siblings	: 2
core id		: 1
cpu cores	: 2
apicid		: 1
initial apicid	: 1
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch bmi1 avx2 bmi2 rdseed adx xsaveopt
bogomips	: 4788.89
clflush size	: 64
cache_alignment	: 64
address sizes	: 40 bits physical, 48 bits virtual
power management:

[root@VM_0_9_centos laravel]# free -m
              total        used        free      shared  buff/cache   available
Mem:           7823        2608         317           0        4896        4881
Swap:             0           0           0

软件:CentOS 7.6 + PHP 7.3.3 + Nginx 1.12.2 + Mysql 8.0.15 + Laravel 5.8.4

[root@VM_0_9_centos laravel]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

[root@VM_0_9_centos laravel]# php -v
PHP 7.3.3 (cli) (built: Mar  5 2019 13:50:38) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.3, Copyright (c) 1998-2018 Zend Technologies

[root@VM_0_9_centos laravel]# nginx -v
nginx version: nginx/1.12.2

[root@VM_0_9_centos laravel]# mysql --version
mysql  Ver 8.0.15 for Linux on x86_64 (MySQL Community Server - GPL)

[root@VM_0_9_centos laravel]# php artisan -V
Laravel Framework 5.8.4

nginx 日志中添加 $request_time 请求总耗时以及 $upstream_response_time 后端响应耗时两个字段。

执行 artisan make:auth 把登录注册功能添加进去,登录,访问 /home 个人主页。注意:默认 Laravel 的 sessionid 每次请求后都会变,这是因为默认进行了加密处理,需要把 App\Http\Middleware\EncryptCookies 这个中间件去掉(参见这里)才能进行测试。

无并发状态下自己刷新网页,单个请求大概耗时 80ms。

163.125.177.139 - - [17/Mar/2019:17:40:35 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.079 0.079
163.125.177.139 - - [17/Mar/2019:17:40:35 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.073 0.073
163.125.177.139 - - [17/Mar/2019:17:40:36 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.084 0.084
163.125.177.139 - - [17/Mar/2019:17:40:37 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.072 0.072
163.125.177.139 - - [17/Mar/2019:17:40:37 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.080 0.080
163.125.177.139 - - [17/Mar/2019:17:40:38 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.084 0.084
163.125.177.139 - - [17/Mar/2019:17:40:38 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.074 0.074
163.125.177.139 - - [17/Mar/2019:17:40:39 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.083 0.083
163.125.177.139 - - [17/Mar/2019:17:40:39 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.076 0.076
163.125.177.139 - - [17/Mar/2019:17:40:40 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.074 0.074
163.125.177.139 - - [17/Mar/2019:17:40:41 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.083 0.083
163.125.177.139 - - [17/Mar/2019:17:40:42 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.084 0.084
163.125.177.139 - - [17/Mar/2019:17:40:43 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.076 0.076
163.125.177.139 - - [17/Mar/2019:17:40:45 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.079 0.079
163.125.177.139 - - [17/Mar/2019:17:40:49 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.074 0.074
163.125.177.139 - - [17/Mar/2019:17:41:11 +0800] "GET /home HTTP/1.1" 200 3362 "http://xxx.xxx.xxx.xxx/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 0.075 0.075

使用 ab 进行压力测试,20个并发,共进行2000次请求,结果如下:


//ab 结果
[root@iZuf6jcwx7sfb1h6ayjq99Z ~]# ab -c 20 -n 2000 -s 60 -rk -H "Cookie: phpsessionid=72wXuhltiBxICwZQtC4aq8cXNhWqW4Aigrn3lf0z" http://xxx.xxx.xxx.xxx/home
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking xxx.xxx.xxx.xxx (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests

Server Software: nginx/1.12.2
Server Hostname: xxx.xxx.xxx.xxx
Server Port: 80

Document Path: /home
Document Length: 3350 bytes

Concurrency Level: 20
Time taken for tests: 82.460 seconds
Complete requests: 2000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 0
Total transferred: 7792000 bytes
HTML transferred: 6700000 bytes
Requests per second: 24.25 [#/sec] (mean)
Time per request: 824.598 [ms] (mean)
Time per request: 41.230 [ms] (mean, across all concurrent requests)
Transfer rate: 92.28 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 28 31 1.5 31 36
Processing: 121 790 227.8 811 1516
Waiting: 121 790 227.9 811 1516
Total: 153 821 227.9 843 1549

Percentage of the requests served within a certain time (ms)
50% 843
66% 934
75% 985
80% 1013
90% 1103
95% 1159
98% 1253
99% 1316
100% 1549 (longest request)

// CPU + Memory + Load 等信息
Time              ---cpu-- ---mem-- ---tcp-- -----traffic---- --vda--- --vda1-- --sr0---  ---load- 
Time                util     util   retran    bytin  bytout     util     util     util     load1   
17/03/19-18:03:51   0.90    40.49     0.00   174.00  285.00     0.08     0.08     0.00      0.00   
17/03/19-18:03:56   0.70    40.49     0.00   230.00  413.00     0.06     0.06     0.00      0.00   
17/03/19-18:04:01   1.00    40.49     0.00   312.00  376.00     0.06     0.06     0.00      0.00   
17/03/19-18:04:06   1.61    40.49     0.00   327.00  587.00     0.10     0.10     0.00      0.00   
17/03/19-18:04:11   1.01    40.50     0.00   377.00  550.00     0.06     0.06     0.00      0.00   
17/03/19-18:04:16   1.21    40.48     0.00   451.00  915.00     0.12     0.12     0.00      0.00   
17/03/19-18:04:21   1.00    40.48     0.00   176.00  238.00     0.20     0.20     0.00      0.00   
17/03/19-18:04:26   0.90    40.48     0.00   230.00  432.00     0.06     0.06     0.00      0.00   
17/03/19-18:04:31   1.00    40.49     0.00   137.00  236.00     0.06     0.06     0.00      0.00   
17/03/19-18:04:36  25.53    40.48     0.00     4.1K   20.2K     0.06     0.06     0.00      1.04   
17/03/19-18:04:41 100.00    40.52     0.03    14.8K  117.6K     0.34     0.34     0.00      2.48   
17/03/19-18:04:46 100.00    40.51     0.03    14.5K  115.3K     0.18     0.18     0.00      3.88   
17/03/19-18:04:51 100.00    40.51     0.03    14.1K  114.1K     0.20     0.20     0.00      5.26   
17/03/19-18:04:56 100.00    40.52     0.03    15.0K  114.3K     0.20     0.20     0.00      6.44   
17/03/19-18:05:01 100.00    40.52     0.05    14.7K  117.3K     0.24     0.24     0.00      7.60   
17/03/19-18:05:06 100.00    40.53     0.03    13.6K  106.0K     0.20     0.20     0.00      8.83   
17/03/19-18:05:11 100.00    40.54     0.00    13.0K  101.9K     0.24     0.24     0.00      9.97   
17/03/19-18:05:16 100.00    40.52     0.00    13.6K  104.2K     0.10     0.10     0.00     10.61   
17/03/19-18:05:21 100.00    40.54     0.08    14.1K  114.7K     0.20     0.20     0.00     11.36   
Time              ---cpu-- ---mem-- ---tcp-- -----traffic---- --vda--- --vda1-- --sr0---  ---load- 
Time                util     util   retran    bytin  bytout     util     util     util     load1   
17/03/19-18:05:26 100.00    40.54     0.03    13.9K  106.8K     0.18     0.18     0.00     12.06   
17/03/19-18:05:31 100.00    40.54     0.03    13.8K  110.0K     0.16     0.16     0.00     12.69   
17/03/19-18:05:36 100.00    40.54     0.05    14.7K  115.5K     0.20     0.20     0.00     13.28   
17/03/19-18:05:41 100.00    40.55     0.05    14.7K  115.1K     0.26     0.26     0.00     13.66   
17/03/19-18:05:46 100.00    40.55     0.00    15.0K  119.2K     0.18     0.18     0.00     14.08   
17/03/19-18:05:51 100.00    40.58     0.05    14.7K  117.8K     0.24     0.24     0.00     14.40   
17/03/19-18:05:56 100.00    40.55     0.03    14.5K  111.2K     0.28     0.28     0.00     14.77   
17/03/19-18:06:01  22.49    40.56     0.12     3.1K   33.0K     0.12     0.12     0.00     13.58   
17/03/19-18:06:06   0.81    40.56     0.00   343.00  609.00     0.12     0.12     0.00     12.50   
17/03/19-18:06:11   1.10    40.57     0.00   331.00  527.00     0.06     0.06     0.00     11.49   
17/03/19-18:06:16   1.11    40.56     0.00   502.00  936.00     0.06     0.06     0.00     10.57   
17/03/19-18:06:21   1.01    40.56     0.00   137.00  203.00     0.00     0.00     0.00      9.73   
17/03/19-18:06:26   0.80    40.56     0.00   275.00  478.00     0.08     0.08     0.00      8.95   
17/03/19-18:06:31   0.90    40.56     0.00   120.00  184.00     0.24     0.24     0.00      8.23   
17/03/19-18:06:36   0.91    40.56     0.00   275.00  481.00     0.08     0.08     0.00      7.57   
17/03/19-18:06:41   0.81    40.56     0.00   240.00  432.00     0.00     0.00     0.00      6.97   
17/03/19-18:06:46   0.71    40.56     0.00   290.00  440.00     0.08     0.08     0.00      6.41   
17/03/19-18:06:51   0.81    40.55     0.00   137.00  236.00     0.08     0.08     0.00      5.90   
17/03/19-18:06:56   0.91    40.55     0.00   275.00  433.00     0.08     0.08     0.00      5.42  

// Mysql 查询速度
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 46.73ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 57.85ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 77.62ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 75.98ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 44.18ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 35.17ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 29.3ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 36.6ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 51.52ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 55.35ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 85.48ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 33.26ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 32.59ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 56.72ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 56.15ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 61.67ms
2019-03-17 10:05:53, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 70.16ms
2019-03-17 10:05:54, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 27.86ms
2019-03-17 10:05:54, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 78.63ms
2019-03-17 10:05:54, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 101.93ms
2019-03-17 10:05:54, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 124.18ms
2019-03-17 10:05:54, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 36.52ms
2019-03-17 10:05:54, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 26.16ms
2019-03-17 10:05:54, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 69.16ms
2019-03-17 10:05:54, sql: select * from `users` where `id` = ? limit 1, bindngs: [1], timeuse: 11.01ms

其中,CPU/Mem/load等信息使用的是淘宝的 tsar 来监控,具体使用可参见(这里)。对于 ab 的结果,可以参考这里

结论:20并发完成2000个请求,耗时82s,用户角度看每个请求耗时824ms,QPS 24。CPU使用100%,load1 负载升至14,内存没有上升维持40%,php-fpm进程涨到了52个,同时 mysql 简单查询耗时50ms。

对于专业测试来说,这个数据是不可以接受的,这是网上的一个指标参考:

CPU、load1 对于双核单机来说,几乎是不可能达到表中的指标的,响应时间100 ms,这个可以 x10,1秒内响应其实用户感知不大。由于测试结果跟机器配置与指标高低紧密相关,因此,说 Laravel 单机抗不住多少并发是很笼统的。

日常开发中经常使用 Laravel-admin 这个后台框架,安装上后,无并发单次访问,响应时间是150 ms左右。以20并发访问后台首页。

Concurrency Level:      20
Time taken for tests:   120.871 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      24278000 bytes
HTML transferred:       23722000 bytes
Requests per second:    8.27 [#/sec] (mean)
Time per request:       2417.429 [ms] (mean)
Time per request:       120.871 [ms] (mean, across all concurrent requests)
Transfer rate:          196.15 [Kbytes/sec] received

这时,负载上不去,CPU也只到60%左右,原因不明。平均每个请求耗时2417 ms,QPS 8。把并发降到10个时,平均响应时间降到可以接受的 1031 ms,即使是10并发,每天也将近百万 PV 了。

对于API开发的话,其接口的速度又是如何呢,按装 passport 包同样进行一翻测试(注意要把 throttle:60,1 这个频率限制中间件去掉)。无并发单次访问,响应时间120 ms。

20并发测试,CPU满100%,结果如下:

Concurrency Level:      20
Time taken for tests:   46.529 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      374000 bytes
HTML transferred:       148000 bytes
Requests per second:    21.49 [#/sec] (mean)
Time per request:       930.582 [ms] (mean)
Time per request:       46.529 [ms] (mean, across all concurrent requests)
Transfer rate:          7.85 [Kbytes/sec] received

平均响应时间 930ms,QPS 21。

有时候为了调试方便,还会安装官方推出的调试工具包telescope,安装上后再看对性能的一点影响。

Concurrency Level:      20
Time taken for tests:   55.901 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      3896000 bytes
HTML transferred:       3350000 bytes
Requests per second:    17.89 [#/sec] (mean)
Time per request:       1118.025 [ms] (mean)
Time per request:       55.901 [ms] (mean, across all concurrent requests)
Transfer rate:          68.06 [Kbytes/sec] received

综上,总结如下:

参考链接:
https://www.cnblogs.com/gumuzi/p/5617232.html
http://blog.chinaunix.net/uid-29431701-id-4070102.html
https://www.hi-linux.com/posts/5198.html
https://juejin.im/post/5c8b92aa6fb9a049fb448e76

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.