Dust8 的博客

读书百遍其义自见

0%

django与压测和容量预估

原理:每天80%的访问集中在20%的时间里,这20%时间叫做峰值时间
公式:( 总PV数 * 80% ) / ( 每天秒数 * 20% ) = 峰值时间每秒请求数(QPS)    

现有的 qps 是可以测试出来, 每天秒数是已知的, 那么能支撑的总 pv 数是可以算出来的.
现有 pv / 现有用户数 = 总的 pv / 总的用户数, 这样就可以算出能支撑的最大用户数.

计算现有 qps

Locust 是一个压测工具. 用 python 写的.
下面代码是测试一个接口, 在该文件目录运行 locust 就启动了, 在打开 http://localhost:8089 设置模拟用户数, 请求速度和请求地址.

1
2
3
4
5
6
7
8
9
10
11
# locustfile.py
import time
from locust import HttpUser, task


class QuickstartUser(HttpUser):
@task
def hello_world(self):
self.client.get(
"/classsellinfo/?page=1&page_size=10&ordering=-created&class_price__gt=0.1"
)

在 4 核 16G 的电脑上用 wsl2 + gunicorn 8(建议是(2*CPU)+1) 个 worker 上大概是 150 rps. 这个接口访问了本地 mysql, 并且无缓存设置.

统计现有pv

GoAccess 是一个开源的实时 web 日志分析器, 支持命令行和浏览器交互.

  • 分析用户的使用情况
  • 统计当前用户的 pv 来预估后期更多用户的 pv
  • 查看请求多的接口可以优先优化

分析 nginx 单个日志文件

分析完后打开 report.html 就可以看到统计情况

1
goaccess access.log -o report.html --log-format=COMBINED

分析 nginx 多个压缩日志文件

1
zcat -f access.log* | goaccess -a -o report.html --log-format=COMBINED

参考链接