H5直播站点运维笔记四 压测篇

H5直播站点运维笔记四 压测篇

  • 压测篇
    • 一、测试环境
      • 1.CPU
      • 2.内存
      • 3.操作系统
      • 4.WEB环境
    • 二、优化前压测
    • 三、开启opcache优化
    • 四、laravel 缓存配置/路由/类映射加载优化
    • 五、总结
      • 1.并发处理性能提升
      • 2.高并发进阶思路
        • (1)业务代码优化
        • (2)数据库优化
        • (3)加速缓存
  • 服务器篇
    • ...详见运维笔记 《服务器篇》
  • Laravel 框架篇
    • ...详见运维笔记 《Laravel 框架篇》
  • 数据库篇
    • ...详见运维笔记 《数据库篇》

压测篇

服务器采购腾讯云S2系列,Intel Xeon E5-2680 v4(2.4 GHz),4核16G,内网带宽1.5Gbps,公网带宽5Mbps。

一、测试环境

1.CPU

processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 79
model name : Intel® Xeon® CPU E5-26xx v4
stepping : 1
microcode : 1
cpu MHz : 2399.996
cache size : 4096 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
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 unfair_spinlock 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 xsaveopt pti retpoline bmi1 avx2 bmi2 rdseed adx
bogomips : 4799.99
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual

2.内存

MemTotal: 16331852 kB

3.操作系统

CentOS release 6.10 (Final)
Linux VM_0_15_centos 2.6.32-754.27.1.el6.x86_64 #1 SMP Tue Jan 28 14:11:45 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

4.WEB环境

  • Nginx
    nginx/1.16.1
  • PHP
    PHP 7.3.15 (cli) (built: Mar 2 2020 17:53:06) ( NTS )
    PHP 7.3.15 (fpm-fcgi) (built: Mar 2 2020 17:53:08)
  • Laravel
    Laravel Framework 6.18.1
  • Mysql
    Mysql 5.7.1.7

二、优化前压测

Abs -n 5000 -c 500 https://api.mydomain.cn/api/live/live_history

Php-fpm 进程监控

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND   
27304 www       20   0  783m 567m 8548 R 99.4  3.6   5139:39 php-fpm 

Postman 同时请求https://api.mydomain.cn/api/live/live_history,返回结果

Time:36.76s  size:4.98KB

Abs 返回结果

d:\Vue.js>abs -n 5000 -c 500 https://api.mydomain.cn/api/live/live_history
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking api.mydomain.cn (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software:        nginx/1.16.1
Server Hostname:        api.mydomain.cn
Server Port:            443
SSL/TLS Protocol:       TLSv1/SSLv3,AES256-SHA,2048,256
Document Path:          /api/live/live_history
Document Length:        630 bytes
Concurrency Level:      500
Time taken for tests:   375.114 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Non-2xx responses:      5000
Total transferred:      5790000 bytes
HTML transferred:       3150000 bytes
Requests per second:    13.33 [#/sec] (mean)
Time per request:       37511.384 [ms] (mean)
Time per request:       75.023 [ms] (mean, across all concurrent requests)
Transfer rate:          15.07 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       21  330 973.7     34    5480
Processing:   540 35141 6388.9  36859   37954
Waiting:      539 35140 6388.8  36859   37954
Total:       5792 35471 5427.8  36899   38046
Percentage of the requests served within a certain time (ms)
  50%  36899
  66%  37218
  75%  37329
  80%  37416
  90%  37507
  95%  37585
  98%  37712
  99%  37881
 100%  38046 (longest request)

三、开启opcache优化

Abs -n 5000 -c 500 https://api.mydomain.cn/api/live/live_history

Php-fpm进程监控

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                     
18281 www       20   0  252m  24m  16m R 93.4  0.2   0:47.95 php-fpm   

Postman 同时请求结果

Time:4.41s  size:4.98KB

Abs返回结果

d:\Vue.js>abs -n 5000 -c 500 https://api.mydomain.cn/api/live/live_history
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking api.mydomain.cn (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software:        nginx/1.16.1
Server Hostname:        api.mydomain.cn
Server Port:            443
SSL/TLS Protocol:       TLSv1/SSLv3,AES256-SHA,2048,256
Document Path:          /api/live/live_history
Document Length:        630 bytes
Concurrency Level:      500
Time taken for tests:   64.823 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Non-2xx responses:      5000
Total transferred:      5790000 bytes
HTML transferred:       3150000 bytes
Requests per second:    77.13 [#/sec] (mean)
Time per request:       6482.258 [ms] (mean)
Time per request:       12.965 [ms] (mean, across all concurrent requests)
Transfer rate:          87.23 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       31  754 1078.9    482    6549
Processing:   491 5139 872.1   5295    6037
Waiting:      491 5067 870.2   5244    6014
Total:       4787 5893 386.0   5852    7456
Percentage of the requests served within a certain time (ms)
  50%   5852
  66%   5961
  75%   6011
  80%   6043
  90%   6231
  95%   6699
  98%   7077
  99%   7235
 100%   7456 (longest request)

四、laravel 缓存配置/路由/类映射加载优化

Abs -n 5000 -c 500 https://api.mydomain.cn/api/live/live_history

Php-fpm进程监控

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                     
19312 www       20   0  254m  27m  17m S 79.1  0.2   1:50.32 php-fpm   

Postman 同时请求返回结果

Time:1.033s  size:4.98KB

Abs返回结果

d:\Vue.js>abs -n 5000 -c 500 https://api.mydomain.cn/api/live/live_history
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking api.mydomain.cn (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software:        nginx/1.16.1
Server Hostname:        api.mydomain.cn
Server Port:            443
SSL/TLS Protocol:       TLSv1/SSLv3,AES256-SHA,2048,256
Document Path:          /api/live/live_history
Document Length:        630 bytes
Concurrency Level:      500
Time taken for tests:   57.450 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Non-2xx responses:      5000
Total transferred:      5790000 bytes
HTML transferred:       3150000 bytes
Requests per second:    87.03 [#/sec] (mean)
Time per request:       5745.002 [ms] (mean)
Time per request:       11.490 [ms] (mean, across all concurrent requests)
Transfer rate:          98.42 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       31 2897 857.3   2992    6368
Processing:   404 2406 812.3   2349    5342
Waiting:      336 1965 722.0   1880    5256
Total:       2203 5303 762.1   5358    7047
Percentage of the requests served within a certain time (ms)
  50%   5358
  66%   5407
  75%   5457
  80%   5514
  90%   5910
  95%   6680
  98%   6862
  99%   6881
 100%   7047 (longest request)

五、总结

1.并发处理性能提升

本次压测使用abs工具,模拟500用户并发请求5000次,站点的吞吐率(Requests per second)未优化前13.33/秒,优化后提升到87.03/秒,站点并发处理性能明显提升了近6.5倍。

  • 未优化
Requests per second:    13.33 [#/sec] (mean)
Time per request:       37511.384 [ms] (mean)
Time per request:       75.023 [ms] (mean, across all concurrent requests)
  • 优化后
Requests per second:    87.03 [#/sec] (mean)
Time per request:       5745.002 [ms] (mean)
Time per request:       11.490 [ms] (mean, across all concurrent requests)

2.高并发进阶思路

(1)业务代码优化

加强自身代码的流程结构优化和业务逻辑合理化,比任何软硬件加速解决方案更重要,是开发与运维的根本。

  • 设置业务引导分流机制,有效减少用户重复或没必要的请求;
  • 数据分时处理,根据分轻重缓急情况,分级为同步处理与异步处理,减少不必要的并发请求;
  • 动静结合,合理化分类存放数据,减少没必要的数据库访问,静态数据使用CDN加速,减轻站点服务器的压力;

(2)数据库优化

  • 中小型站点高并发需求不建议选购云服务器自行安装数据库,购买与运维成本都太高,直接选购Mysql数据库云服务器;
  • 使用独立数据库服务器,根据站点服务器的最大吞吐率,选购相应并发吞吐率的第三方数据库服务器;
  • 使用独立缓存服务器,根据站点服务器的最大吞吐率,选购相应并发吞吐率的第三方nosql数据库服务器用于缓存数据;

(3)加速缓存

  • CDN存放静态数据,减少用户访问本站点的静态数据访问;
  • 内存缓存加速,使用Redis、Memcache缓存数据,减少Mysql数据库的重复访问压力;

高并优化具体解决方案另起章节。

本章完


服务器篇

…详见运维笔记 《服务器篇》

Laravel 框架篇

…详见运维笔记 《Laravel 框架篇》

数据库篇

…详见运维笔记 《数据库篇》

你可能感兴趣的:(服务器,后端,centos,nginx,压力测试)