1、性能优化考虑点
- (a)当前系统结构瓶颈
- 观察指标(top命令)、压力测试
- (b)了解业务模式
- 接口业务类型、系统层次化结构
- (c)性能与安全
2、压测工具ab接口测试
ab不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。
ab是一个httpd自带的很好用的压力测试工具,ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。
可以用来测试apache的负载压力,也可以用来测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力负载性能。
1、安装ab
yum install httpd-tools
2、使用
Usage: ab [options] [http[s]://]hostname[:port]/path ,比如ab -n 2000 -c http://127.0.0.1/
-n 要执行请求数,默认会执行一个请求
-c 一次执行多个请求的数量,默认是一次一个请求。
-t 用于基准测试的最大秒数,使用它在固定的总时间内对服务器进行基准测试。默认情况下,没有时间限制。
-s 超时之前等待的最大秒数。 默认值是30秒。
-b TCP发送/接收缓冲区的大小,以字节为单位。
-B 进行传出连接时要绑定的地址。
-p 包含数据到POST的文件。 文件格式为gid=2&status=1,配合-T使用
-u 包含PUT数据的文件。 还请记住设置-T 。
-T Content-type用于POST / PUT数据的内容类型内容类型标题,例如:'application/x-www-form-urlencoded' 默认是'text/plain'
-v verbosity 要打印多少个疑难解答信息,设置详细级别 - 4和以上打印标题信息,3和以上打印响应代码(404,200等),2和以上打印警告和信息。
-w 在HTML表格中打印结果。
-i 使用HEAD代替GET。
-x 用作<table>的属性的字符串。 属性被插入<table here>。
-y 用作<tr>的属性的字符串。
-z 用作<td>的属性的字符串。
-C 将cookie添加到请求。 参数通常采用名称=值对的形式。 这个字段是可重复的。
-H attribute 例如 ‘Accept-Encoding: gzip’ 插入所有普通标题行之后。(重复)
-A 添加基本的WWW认证,该属性是一个冒号分隔的用户名和密码,auth-username:password
-P 添加基本代理验证,属性是一个冒号分隔的用户名和密码,proxy-auth-username:password
-X 使用代理服务器和端口号。
-V 打印版本号并退出。
-k 使用HTTP KeepAlive功能。
-d 不要显示百分点服务表。
-S 不要显示信心估计和警告。
-q 做超过150个请求时不要显示进度。
-g 将收集的数据输出到gnuplot格式文件。
-e 输出提供百分比的CSV文件。
-r 不要退出套接字接收错误。
-h 显示使用情况信息(此消息)。
-Z 密码套件指定SSL / TLS密码套件(请参阅openssl密码)
-f 指定SSL / TLS协议 (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
样例:
- GET请求
- [root@zq conf.d]# ab -n2000 -c 4 https://10.21.144.110/44demo/
- 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 10.21.144.110 (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: diserver ##被测试服务器软件名称
- Server Hostname: 10.21.144.110 #服务器主机名
- Server Port: 443 #服务器端口
- SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256
-
- Document Path: /44demo/ #请求的URL中的路径
- Document Length: 157 bytes #页面的大小
-
- Concurrency Level: 4 #并发数
- Time taken for tests: 2.113 seconds #请求总耗时
- Complete requests: 2000 #总共完成的请求数量
- Failed requests: 0 #失败数
- Write errors: 0
- Non-2xx responses: 2000
- Total transferred: 692000 bytes #测试过程中产生的网络传输总量
- HTML transferred: 314000 bytes #测试过程中产生的HTML传输量
- Requests per second: 946.67 [#/sec] (mean) #表示服务器吞吐量,每秒发多少个请求,总请求数/请求总耗时,2000/2.113
- Time per request: 4.225 [ms] (mean) #客户端访问服务端,单个请求所耗时间
- Time per request: 1.056 [ms] (mean, across all concurrent requests)#单个请求在服务端处理的耗时
- Transfer rate: 319.87 [Kbytes/sec] received #传输速率,可以帮助排除是否存在网络流量过大导致响应时间延长的问题
-
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 2 4 0.8 4 12 #socket链路建立消耗,代表网络状况好
- Processing: 0 0 0.4 0 6 #写入缓冲区消耗+链路消耗+服务器消耗
- Waiting: 0 0 0.4 0 6 #写入缓冲区消耗+链路消耗+服务器消耗+读取数据消耗
- Total: 2 4 0.9 4 12
-
- Percentage of the requests served within a certain time (ms)
- 50% 4
- 66% 4
- 75% 4
- 80% 5
- 90% 5
- 95% 6
- 98% 7
- 99% 8
- 100% 12 (longest request)
-
- POST 请求
- [root@zq conf.d]# cat /home/testzq/post.txt
- vendor=""&application=""&firsttime="1576157443"&lasttime="1576157543"&sortedby=2&reversed=0&skip=1&count=10
-
- [root@zq conf.d]# ab -n 100 -c 2 -p "/home/testzq/post.txt" -T application/json -C "username=admin; role=admin; auth_tkt=5928ba64a4eae02ba17c8a0ee6f0f924477c2f2f4b6855446a6425b4e26498a86dc8b81122fd3f8f3fced95a7858965085f3e23ef4486c1265d765f551cb33be5e35462bYWRtaW4%3D; timestamp=1580549678454" -H Referer:https://10.21.144.110:8443/ https://10.21.144.110:8443/inventory/software/applications
- 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 10.21.144.110 (be patient).....done
-
-
- Server Software: diserver
- Server Hostname: 10.21.144.110
- Server Port: 8443
- SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES256-GCM-SHA384,2048,256
-
- Document Path: /inventory/software/applications
- Document Length: 7095 bytes
-
- Concurrency Level: 2
- Time taken for tests: 0.303 seconds
- Complete requests: 100
- Failed requests: 0
- Write errors: 0
- Non-2xx responses: 100
- Total transferred: 726000 bytes
- Total body sent: 53500
- HTML transferred: 709500 bytes
- Requests per second: 329.97 [#/sec] (mean)
- Time per request: 6.061 [ms] (mean)
- Time per request: 3.031 [ms] (mean, across all concurrent requests)
- Transfer rate: 2339.47 [Kbytes/sec] received
- 172.40 kb/s sent
- 2511.86 kb/s total
-
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 2 3 0.4 2 4
- Processing: 2 3 1.2 3 7
- Waiting: 2 3 1.2 3 6
- Total: 4 6 1.3 6 10
- ERROR: The median and mean for the initial connection time are more than twice the standard
- deviation apart. These results are NOT reliable.
-
- Percentage of the requests served within a certain time (ms)
- 50% 6
- 66% 7
- 75% 7
- 80% 7
- 90% 8
- 95% 8
- 98% 9
- 99% 10
- 100% 10 (longest request)
3、注意点
1. 压力测试工作应该放到产品上线之前,而不是上线以后
2. 测试时尽量跨公网进行,而不是内网
3. 测试时并发应当由小逐渐加大,比如并发100时观察一下网站负载是多少、打开是否流程,并发200时又是多少、网站打开缓慢时并发是多少、网站打不开时并发又是多少
4. 应尽量进行单元测试