性能测试概述
- 我们的项目不仅仅要考虑功能同时需要关注性能
- 需要考虑并发访问时服务器会出现什么样的输出
关于性能测试需要考虑几点
- 吞吐率(Requests per second)
- 反映了服务器的并发处理能力
- 比如在50个并发下,服务器每秒钟能处理多少请求
- 并发连接数(The number of concurrent connections)
- 表示服务器在同一时间能够承受多少并发的请求
- 比如服务器可以承受300个并发,当在第301个用户访问时,就会失败
- 并发用户数(The number of concurrent users, Concurrency Level)
- 和并发连接数类似
- 一个用户可能产生多个连接
- 用户平均请求等待时间(Time per request)
- 这个关于用户体验的问题
- 服务器平均请求等待时间(Time per request: across all concurrent requests)
- 处理完所有请求花费的时间除以总共请求数
专业的测试工具
- 模拟多人访问项目,就需要使用专业的工具进行如:Load Runner、Jmeter等,作为开发者推荐使用ab工具
- ab工具
- apache 提供的一个压力测试工具
- 先update一下安装源 $
apt update
- 安装 $
apt install apache2-utils
- 使用 $
ab -c 20 -n 10000 http://exampl.com/
-c 20
表示希望模拟20个用户请求,-c表示多少个并发数-n 10000
表示请求10000次,-n表示请求多少次
- 开始进行测试,请求完成后输出测试报告
# 重点关注以下信息 Concurrency Level: 20 Requests per second: 601.94 [#/sec] (mean) # 这里表示平均每秒钟可以处理多少请求 值越大越好 Time per request: 33.226[ms] (mean) # 这里表示用户平均等待时间 值越小越好 Time per reauest: 1.661[ms] (mean, across all concurrent requests) # 这里是服务器平均请求的处理时间 值越小越好 Transfer rate: 114.04 [Kbytes/sec] received # 这里是使用的网络流量 # ... Percentage of the requests served within a certain time (ms) 50% 12 # 表示50%的请求在12ms以内完成 66% 12 75% 13 80% 13 90% 14 95% 15 98% 17 99% 20 100% 1047 (longest request)
- 我们可以通过ab工具测试两种服务的性能进行对比:
- 如:uWSGI 和 django自带的runserver来对比测试
- 发现uWSGI的性能几乎是runserver的2倍