flask与sanic框架对比
我在服务器端运行web服务,在本地Windows环境下利用ab对web服务进行压力测试。分析可以分作两个部分:
- flask+gunicorn与sanic的对比;
- flask+gunicorn与sanic+uvicorn的对比;
主要的参考指标包括:
- Time taken for tests,即处理请求所花费的总时间;
- Requests per second,即吞吐量;
- user time per request,即用户每请求所花费的时间;
- Time per request,每请求平均花费时间;
下面利用图标的形式对所得数据进行一个解析:
flask+gunicorn对比sanic
表中(n,c)代表(总请求数,并发数)
测试框架 | (n,c) | Time taken for tests | Requests per second | user Time per request | Time per request |
---|---|---|---|---|---|
flask+gunicorn -w 8 | (100 ,10) | 0.031 | 3227.58 | 3.098 | 0.31 |
flask+gunicorn -w 8 | (100 ,50) | 0.016 | 6401.64 | 7.811 | 0.156 |
flask+gunicorn -w 8 | (100 ,100) | 0.03 | 3278.69 | 30.5 | 0.305 |
flask+gunicorn -w 8 | (500 ,50) | 0.125 | 4007.31 | 12.477 | 0.25 |
flask+gunicorn -w 8 | (500 ,100) | 0.109 | 4572.52 | 21.87 | 0.219 |
flask+gunicorn -w 8 | (1000 ,200) | 0.229 | 4362.03 | 45.85 | 0.229 |
sanic | (100 ,10) | 0.068 | 1468.28 | 6.811 | 0.681 |
sanic | (100 ,50) | 0.059 | 1681.55 | 29.735 | 0.595 |
sanic | (100 ,100) | 0.058 | 1732.50 | 57.72 | 0.577 |
sanic | (500 ,50) | 0.292 | 1771.28 | 29.128 | 0.584 |
sanic | (500 ,100) | 0.314 | 1592.13 | 62.809 | 0.628 |
sanic | (1000 ,200) | 0.6 | 1667.02 | 119.975 | 0.6 |
总响应时间
对总响应时间可以看出,sanic的总响应时间明显长于flask+gunicorn
吞吐量
图片表明sanic的吞吐量明显的低于flask配合gunicorn;
用户每请求所花费的时间
sanic用户每请求所花费的时间明显长于flask+gunicorn
每请求平均花费时间
sanic所花费的时间明显要多
flask+gunicorn对比sanic+uvicorn
测试框架 | (n,c) | Time taken for tests | Requests per second | user Time per request | Time per request |
---|---|---|---|---|---|
sanic+uvicorn -w 8 | (100 ,100) | 0.035 | 2869.52 | 34.869 | 0.348 |
sanic+uvicorn -w 8 | (1000 ,100) | 0.252 | 3970.54 | 25.186 | 0.252 |
sanic+uvicorn -w 8 | (10000 ,100) | 3.432 | 2913.64 | 34.321 | 0.343 |
sanic+uvicorn -w 8 | (10000 ,500) | 5.544 | 1803.77 | 277.197 | 0.554 |
sanic+uvicorn -w 8 | (10000 ,1000) | 5.425 | 1843.44 | 542.465 | 0.542 |
flask+gunicorn -w 8 | (100 ,100) | 0.03 | 3278.69 | 30.5 | 0.305 |
flask+gunicorn -w 8 | (1000 ,100) | 0.221 | 4526.18 | 22.094 | 0.221 |
flask+gunicorn -w 8 | (10000 ,100) | 2.163 | 4623.68 | 21.628 | 0.216 |
flask+gunicorn -w 8 | (10000 ,500) | 2.375 | 4210.55 | 118.749 | 0.237 |
flask+gunicorn -w 8 | (10000 ,1000) | 2.461 | 4063.54 | 246.091 | 0.246 |
总响应时间
对总响应时间可以看出,sanic+uvicorn的总响应时间明显长于flask+gunicorn
吞吐量
图片表明sanic+uvicorn的吞吐量明显的低于flask配合gunicorn;
用户每请求所花费的时间
sanic+uvicorn用户每请求所花费的时间明显长于flask+gunicorn
每请求平均花费时间
sanic+uvicorn所花费的时间明显要多
结论
通过上面的分析我们看到无论从哪一个方面出发,flask+gunicorn的搭配的在这个测试案例中的结果还是优于sanic的。而且,通过调查,发现将sanic当作web接口框架的企业寥寥无几。从稳妥的角度出发,建议还是继续沿用flask+gunicorn的框架搭建web接口以达到预期目的。