简介
最近realip.cc貌似是被攻击了,就是被批量定时调用,应该也不算是攻击吧,看了一下,感觉支持的请求并发还是比较垃圾的,所以就优化了下
操作
优化之前先测试下目前支持的并发
Server Software: cloudflare
Server Hostname: realip.cc
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-CHACHA20-POLY1305,256,256
Server Temp Key: X25519 253 bits
TLS Server Name: realip.cc
Document Path: /
Document Length: 456 bytes
Concurrency Level: 560
Time taken for tests: 27.503 seconds
Complete requests: 3000
Failed requests: 0
Total transferred: 3334804 bytes
HTML transferred: 1368000 bytes
Requests per second: 109.08 [#/sec] (mean)
Time per request: 5133.980 [ms] (mean)
Time per request: 9.168 [ms] (mean, across all concurrent requests)
Transfer rate: 118.41 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 518 637 263.6 595 3659
Processing: 456 3169 3593.5 1715 16374
Waiting: 456 3165 3592.2 1713 16216
Total: 978 3807 3604.5 2390 18121
Percentage of the requests served within a certain time (ms)
50% 2390
66% 3182
75% 4016
80% 5192
90% 9606
95% 11609
98% 16498
99% 16594
100% 18121 (longest request)
差不多是560个并发就没得玩了
接着做下调整
- 原来是使用flask写的,所以现在直接改成fastapi,并且修改了下ci脚本,拿出来了部分配置文件
- 使用调整uvicorn去启动项目,调整uvicorn的worker直接改成4个,因为我是2core的cpu
- 然后把nginx的进程数目也改为4个,worker_connections改为2048个,后端其实是有两台服务器的,但是因为一台里面是有nginx做负载均衡,所以两台机器的权重也是不一样的
最后看下结果吧
Server Software: cloudflare
Server Hostname: realip.cc
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-CHACHA20-POLY1305,256,256
Server Temp Key: X25519 253 bits
TLS Server Name: realip.cc
Document Path: /
Document Length: 546 bytes
Concurrency Level: 1000
Time taken for tests: 41.837 seconds
Complete requests: 4000
Failed requests: 25
(Connect: 0, Receive: 0, Length: 25, Exceptions: 0)
Total transferred: 4776226 bytes
HTML transferred: 2170350 bytes
Requests per second: 95.61 [#/sec] (mean)
Time per request: 10459.162 [ms] (mean)
Time per request: 10.459 [ms] (mean, across all concurrent requests)
Transfer rate: 111.49 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 650 232.0 595 3639
Processing: 413 6575 5012.4 5276 29970
Waiting: 413 6554 5010.7 5271 29970
Total: 934 7225 5045.7 5912 30730
Percentage of the requests served within a certain time (ms)
50% 5912
66% 7564
75% 9275
80% 10801
90% 14706
95% 16692
98% 20413
99% 24667
100% 30730 (longest request)
虽然有错误请求,但是在情况好一点的状态下1000并发是没有什么大的问题的,几乎提升一倍的性能吧,fastapi还是很优秀的,之后看情况把代码扔出来,之后大家有调用需求的话直接自己部署一个就好了。
欢迎关注我的博客www.bboy.app
Have Fun