Sanic , Fastapi都是優(yōu)秀的python web異步框架檐蚜,找了半天沒找到靠譜的性能對比測試加缘,只能自己做一個(gè)。
測試內(nèi)容:純get請求霹娄、異步ORM讀
測試工具:WRK
測試代碼:由于兩者代碼非常相似能犯,本文就直接放上fastapi的代碼demo了。
1犬耻、純get請求 壓測:
1.1 :代碼
1.2 : 結(jié)果
1.2.1 Sanic
Running 30s test @ http://0.0.0.0:7006/
4 threads and 1000 connections
Thread Stats? Avg? ? ? Stdev? ? Max? +/- Stdev
Latency? ? 6.26ms? ? 5.01ms? 82.78ms? 85.31%
Req/Sec? ? 9.85k? ? 7.36k? 23.68k? ? 50.08%
Latency Distribution
50%? ? 4.27ms
75%? ? 7.64ms
90%? 13.17ms
99%? 24.40ms
1176945 requests in 30.08s, 126.83MB read
Socket errors: connect 751, read 25, write 0, timeout 0
Requests/sec:? 39131.24
Transfer/sec:? ? ? 4.22MB
1.2.2 Fastapi
Running 30s test @ http://0.0.0.0:7000/index
4 threads and 1000 connections
Thread Stats? Avg? ? ? Stdev? ? Max? +/- Stdev
Latency? ? 52.14ms? 25.04ms 209.52ms? 67.22%
Req/Sec? ? 4.07k? ? 0.88k? ? 6.78k? ? 71.38%
Latency Distribution
50%? 49.73ms
75%? 69.48ms
90%? 84.76ms
99%? 118.72ms
486736 requests in 30.04s, 65.91MB read
Socket errors: connect 0, read 1006, write 2, timeout 0
Requests/sec:? 16202.60
Transfer/sec:? ? ? 2.19MB
2踩晶、異步ORM讀 壓測:
2.1.1 Sanic
Running 30s test @ http://0.0.0.0:7005/testdb
4 threads and 1000 connections
Thread Stats? Avg? ? ? Stdev? ? Max? +/- Stdev
Latency? ? 66.76ms? 57.26ms 558.07ms? 77.71%
Req/Sec? ? 0.94k? 384.55? ? 1.84k? ? 53.17%
Latency Distribution
50%? 44.23ms
75%? 108.09ms
90%? 143.02ms
99%? 247.15ms
111825 requests in 30.07s, 14.40MB read
Socket errors: connect 751, read 108, write 0, timeout 0
Requests/sec:? 3719.34
Transfer/sec:? ? 490.34KB
2.1.2 Fastapi
Running 30s test @ http://0.0.0.0:7000/testdb
4 threads and 1000 connections
Thread Stats? Avg? ? ? Stdev? ? Max? +/- Stdev
Latency? 454.12ms? 483.01ms? 5.60s? ? 86.84%
Req/Sec? 699.22? ? 153.92? ? 1.38k? ? 75.95%
Latency Distribution
50%? 376.73ms
75%? 636.51ms
90%? ? 1.06s
99%? ? 2.17s
82939 requests in 30.10s, 10.69MB read
Socket errors: connect 0, read 659, write 0, timeout 0
Non-2xx or 3xx responses: 379
Requests/sec:? 2755.36
Transfer/sec:? ? 363.74KB
3 結(jié)論
3.1 純性能sanic大約是fastapi的2.5倍,sanic單機(jī)并發(fā)接近 4萬?Requests/sec香追,而fastapi只有1.6萬合瓢。
3.2 使用異步ORM進(jìn)行數(shù)據(jù)庫讀時(shí),瓶頸更多在數(shù)據(jù)庫方面透典,但同樣條件下晴楔,fastapi性能仍然比sanic略低,大約為sanic并發(fā)性能的0.7倍峭咒。
4 感言
python3.6版本以后web的性能真的是可比Golang了(實(shí)測sanic比go的gin框架性能約低一半税弃,但仍處于一個(gè)數(shù)量級),限制服務(wù)器性能的更多時(shí)數(shù)據(jù)庫和帶寬凑队,正常一個(gè)帶數(shù)據(jù)庫讀寫的api接口并發(fā)能到3000已經(jīng)很牛了则果,無論哪個(gè)框架性能都過剩了幔翰。