基於Flask的網關:Flask,Uwsgi,Gevent,Gunicorn(gevent),Tornado,Twistedpython
import os
import sysreact
from flask import Flaskweb
app = Flask(name)
port = 5000
debug = Truejson
def hello() :
return "Congraduation!!! It Works!!!"flask
@app.route('/')
def index() :
return hello()網絡
def flask(p = 9090) :
print("flask is running on: localhost:%d", p)
app.run('0.0.0.0', port = p, debug = debug)併發
def uwsgi(p = 9191) :
print("uwsgi is running on: localhost:%d", p)
# os.system("uwsgi uwsgi.ini")
os.system("uwsgi --http :" + str(p) + " --wsgi-file index.py --callable app --processes 4 --threads 10 --master --max-request 100000 --stats :9191")app
from gevent.pywsgi import WSGIServer框架
def gevent(p = 9292) :
print("gevent is running on: localhost:%d", p)
http_server = WSGIServer(('', p), app)
http_server.serve_forever()async
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import define, options
def gunicorn(p = 9393) :
print("gunicorn is running on: localhost:%d", p)
# os.system("gunicorn -c gunicorn.py index:app")
os.system("gunicorn -b0.0.0.0:" + str(p) + " -w4 -k gevent index:app")
def tornado(p = 9494) :
print("tornado is running on: localhost:%d", p)
define('port', type = int, default = p)
define('mode', default = 'debug')
options.parse_command_line()
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(options.port)
IOLoop.instance().start()
from twisted.web import server, resource
from twisted.internet import reactor, endpoints
class Counter(resource.Resource) :
isLeaf = True
numberRequests = 0
def render_GET(self, request) :
request.setHeader(b"content-type", b"text/plain")
content = index()
return content.encode("ascii")
def twisted(p = 9595) :
print("twisted is running on: localhost:%d", p)
endpoints.serverFromString(reactor, "tcp:" + str(p)).listen(server.Site(Counter()))
reactor.run()
import falcon
class ThingsResource(object) :
def on_get(self, req, resp) :
resp.body = ('hello falcon')
appfalcon = falcon.API()
appfalcon.add_route('/', ThingsResource())
def falcon(p = 9696) :
print("falcon is running on: localhost:%d", p)
os.system("gunicorn -b0.0.0.0:" + str(p) + " -w4 -k gevent index:appfalcon")
from sanic import Sanic
from sanic.response import json, text
appsanic = Sanic()
@appsanic.route('/')
async def sanicindex(request) :
return text(hello())
def sanic(p = 9797) :
print("sanic is running on: localhost:%d", p)
appsanic.run(host = '0.0.0.0', port = p, workers = 4, debug = debug)
if sys.version_info < (3, 7) :
from vibora import Vibora, logging
from vibora.responses import Response
appvibora = Vibora() @appvibora.route('/') async def viboraindex() : return Response(bytes(index(), 'utf8')) def log_handler(msg, level) : # Redirecting the msg and level to logging library.
getattr(logging, level)(msg)
print(f'Msg: {msg} / Level: {level}')
def vibora(p = 9898) : print("vibora is running on: localhost:%d", p) appvibora.run(host = '0.0.0.0', port = p, debug = debug)
else :
def vibora(p = 9898) :
print("vibora is not running : python is not 3.6")
if name == 'main' :
if len(sys.argv) > 1 :
if sys.argv[1] == '1' : flask(port)
if sys.argv[1] == '2' : uwsgi(port)
if sys.argv[1] == '3' : gevent(port)
if sys.argv[1] == '4' : gunicorn(port)
if sys.argv[1] == '5' : tornado(port)
if sys.argv[1] == '6' : twisted(port)
if sys.argv[1] == '7' : falcon(port)
if sys.argv[1] == '8' : sanic(port)
if sys.argv[1] == '9' : vibora(port)
else :
flask(port)
Flask
Uwsgi
Gunicorn(gevent)
Tornado
Twisted
Gevent
非Flask Web框架:Falcon,Sanic,Vibora
Falcon
Sanic
Vibora
結論
一、在MAC環境下,用siege測試,相同的併發和請求數,Sanic和Uwsgi的表現遙遙領先,測試幾十遍,sanic的QPS驚人的能夠彪到7K以上,還要優化空間,Uwsgi參數優化後最大也能夠到4K以上,其餘框架表現平平,其中網絡極力推薦的Tornado和Gunicorn(gevent)也沒有Sanic出色。
二、在Mac和其餘Linux環境中,用wrk測試,Vibora的表現豔壓羣芳,無與倫比。
Mac OS 10.14 + Python3.6.7
wrk -t100 -c1000 -d10s http://localhost:5000/
Index
Frameworks
Requests/Sec
Version
1 Flask
1031.42
1.0.2
2 uWSGI
801.69
2.0.17.1
3 Gevent
2220.93
1.3.7
4 Gunicorn(gevent)
3666.68
19.9.0
5 Tornado
1539.63
5.1.1
6 Twisted
4006.71
18.9.0
7 Falcon
4697.89
1.4.1
8 Sanic
3068.29
0.8.3
9 Vibora
15316.90
0.0.6
siege -c100 -r10000 -t10s http://localhost:5000/
Index
Frameworks
Requests/Sec
Version
1 Flask
998.17
1.0.2
2 uWSGI
854.09
2.0.17.1
3 Gevent
1650.11
1.3.7
4 Gunicorn(gevent)
1671.60
19.9.0
5 Tornado
1137.27
5.1.1
6 Twisted
1765.65
18.9.0
7 Falcon
1640.38
1.4.1
8 Sanic
1697.91
0.8.3
9 Vibora
2225.55
0.0.6
Ubuntu18 + Python3.6.7
wrk -t100 -c1000 -d10s http://localhost:5000/
Index
Frameworks
Requests/Sec
Version
1 Flask
348.32
1.0.2
2 uWSGI
727.01
2.0.17.1
3 Gevent
868.29
1.3.7
4 Gunicorn(gevent)
2300.19
19.9.0
5 Tornado
693.61
5.1.1
6 Twisted
2059.27
18.9.0
7 Falcon
3041.84
1.4.1
8 Sanic
1158.95
0.8.3
9 Vibora
4477.26
0.0.6
siege -c100 -r10000 -t10s http://localhost:5000/
Index
Frameworks
Requests/Sec
Version
1 Flask
358.53
1.0.2
2 uWSGI
899.25
2.0.17.1
3 Gevent
598.06
1.3.7
4 Gunicorn(gevent)
1112.11
19.9.0
5 Tornado
503.71
5.1.1
6 Twisted
972.32
18.9.0
7 Falcon
1143.52
1.4.1
8 Sanic
807.30
0.8.3
9 Vibora
767.14
0.0.6
Debian9 + Python3.6.7
wrk -t100 -c1000 -d10s http://localhost:5000/
Index
Frameworks
Requests/Sec
Version
1 Flask
347.30
1.0.2
2 uWSGI
462.69
2.0.17.1
3 Gevent
904.53
1.3.7
4 Gunicorn(gevent)
2362.21
19.9.0
5 Tornado
736.68
5.1.1
6 Twisted
2135.74
18.9.0
7 Falcon
3028.23
1.4.1
8 Sanic
1208.67
0.8.3
9 Vibora
4888.87
0.0.6
siege -c100 -r10000 -t10s http://localhost:5000/
Index
Frameworks
Requests/Sec
Version
1 Flask
419.40
1.0.2
2 uWSGI
1074.95
2.0.17.1
3 Gevent
660.53
1.3.7
4 Gunicorn(gevent)
1093.92
19.9.0
5 Tornado
572.77
5.1.1
6 Twisted
1041.45
18.9.0
7 Falcon
1181.62
1.4.1
8 Sanic
908.83
0.8.3
9 Vibora
756.53
0.0.6
Centos7.5 + Python3.6.7
wrk -t100 -c1000 -d10s http://localhost:5000/
Index
Frameworks
Requests/Sec
Version
1 Flask
337.44
1.0.2
2 uWSGI
756.72
2.0.17.1
3 Gevent
817.57
1.3.7
4 Gunicorn(gevent)
1952.39
19.9.0
5 Tornado
587.53
5.1.1
6 Twisted
1820.75
18.9.0
7 Falcon
2655.53
1.4.1
8 Sanic
1081.31
0.8.3
9 Vibora
4011.29
0.0.6
siege -c100 -r10000 -t10s http://localhost:5000/
Index
Frameworks
Requests/Sec
Version
1 Flask
336.62
1.0.2
2 uWSGI
918.54
2.0.17.1
3 Gevent
450.48
1.3.7
4 Gunicorn(gevent)
1294.98
19.9.0
5 Tornado
410.35
5.1.1
6 Twisted
949.29
18.9.0
7 Falcon
884.20
1.4.1
8 Sanic
665.61
0.8.3
9 Vibora
956.35
0.0.6
圖表
地址:https://datastudio.google.com/open/1YCVHX0qyoGx2lZaqzQA979c28G6Fxr1-
綜合下來,Vibora, Falcon, Gunicorn 總體表現不錯,進入三強。 尷尬的是:儘管Sanic在各平臺表現也很好,可是總體下來沒有進入三強,很惋惜。。。想哭😢 第一天測試sanic秒殺全場,次日vibora秒殺sanic,我只好換各類姿式,最後證實仍是vibora牛B一點。。。 Index Frameworks Requests/Sec Version 1 Vibora 15316.90 2.0.17.1 2 Falcon 4697.89 0.8.3 3 Gunicorn 3666.68 0.0.6 備註 使用mac壓力測試推薦siege或者wrk,ab有時候會出現超時或退出,不是很穩定。 Debian總體性能稍佔優點,Ubuntu是基於Debian,建議之後微服務os選用debian。