当前位置:   article > 正文

python twisted和flask_Flask性能优化对比

twisted flask

基于Flask的网关:Flask,Uwsgi,Gevent,Gunicorn(gevent),Tornado,Twisted

!/usr/bin/python

-- coding:utf-8 --

##################################################

美颜Feed在线推荐系统

作者:志恩

时间:2018-10-17

网关性能对比 python index.py [1,2,3,4,5,6,7,8,9]

##################################################

import os

import sys

from flask import Flask

app = Flask(name)

port = 5000

debug = True

def hello() :

return "Congraduation!!! It Works!!!"

@app.route('/')

def index() :

return hello()

flask

def flask(p = 9090) :

print("flask is running on: localhost:%d", p)

app.run('0.0.0.0', port = p, debug = debug)

uwsgi

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")

gevent

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()

gunicorn

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")

tornado

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()

twisted

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()

falcon

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")

sanic

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)

vibora 暂不支持python3.7,可以在3.6运行

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

结论

1、在MAC环境下,用siege测试,相同的并发和请求数,Sanic和Uwsgi的表现遥遥领先,测试几十遍,sanic的QPS惊人的可以彪到7K以上,还要优化空间,Uwsgi参数优化后最大也可以到4K以上,其他框架表现平平,其中网络极力推荐的Tornado和Gunicorn(gevent)也没有Sanic出色。

2、在Mac和其他Linux环境中,用wrk测试,Vibora的表现艳压群芳,无与伦比。

Mac OS 10.14 + Python3.6.7

wrk -t100 -c1000 -d10s http://localhost:5000/

Index

Frameworks

Requests/Sec

Version

1Flask

1031.42

1.0.2

2uWSGI

801.69

2.0.17.1

3Gevent

2220.93

1.3.7

4Gunicorn(gevent)

3666.68

19.9.0

5Tornado

1539.63

5.1.1

6Twisted

4006.71

18.9.0

7Falcon

4697.89

1.4.1

8Sanic

3068.29

0.8.3

9Vibora

15316.90

0.0.6

siege -c100 -r10000 -t10s http://localhost:5000/

Index

Frameworks

Requests/Sec

Version

1Flask

998.17

1.0.2

2uWSGI

854.09

2.0.17.1

3Gevent

1650.11

1.3.7

4Gunicorn(gevent)

1671.60

19.9.0

5Tornado

1137.27

5.1.1

6Twisted

1765.65

18.9.0

7Falcon

1640.38

1.4.1

8Sanic

1697.91

0.8.3

9Vibora

2225.55

0.0.6

Ubuntu18 + Python3.6.7

wrk -t100 -c1000 -d10s http://localhost:5000/

Index

Frameworks

Requests/Sec

Version

1Flask

348.32

1.0.2

2uWSGI

727.01

2.0.17.1

3Gevent

868.29

1.3.7

4Gunicorn(gevent)

2300.19

19.9.0

5Tornado

693.61

5.1.1

6Twisted

2059.27

18.9.0

7Falcon

3041.84

1.4.1

8Sanic

1158.95

0.8.3

9Vibora

4477.26

0.0.6

siege -c100 -r10000 -t10s http://localhost:5000/

Index

Frameworks

Requests/Sec

Version

1Flask

358.53

1.0.2

2uWSGI

899.25

2.0.17.1

3Gevent

598.06

1.3.7

4Gunicorn(gevent)

1112.11

19.9.0

5Tornado

503.71

5.1.1

6Twisted

972.32

18.9.0

7Falcon

1143.52

1.4.1

8Sanic

807.30

0.8.3

9Vibora

767.14

0.0.6

Debian9 + Python3.6.7

wrk -t100 -c1000 -d10s http://localhost:5000/

Index

Frameworks

Requests/Sec

Version

1Flask

347.30

1.0.2

2uWSGI

462.69

2.0.17.1

3Gevent

904.53

1.3.7

4Gunicorn(gevent)

2362.21

19.9.0

5Tornado

736.68

5.1.1

6Twisted

2135.74

18.9.0

7Falcon

3028.23

1.4.1

8Sanic

1208.67

0.8.3

9Vibora

4888.87

0.0.6

siege -c100 -r10000 -t10s http://localhost:5000/

Index

Frameworks

Requests/Sec

Version

1Flask

419.40

1.0.2

2uWSGI

1074.95

2.0.17.1

3Gevent

660.53

1.3.7

4Gunicorn(gevent)

1093.92

19.9.0

5Tornado

572.77

5.1.1

6Twisted

1041.45

18.9.0

7Falcon

1181.62

1.4.1

8Sanic

908.83

0.8.3

9Vibora

756.53

0.0.6

Centos7.5 + Python3.6.7

wrk -t100 -c1000 -d10s http://localhost:5000/

Index

Frameworks

Requests/Sec

Version

1Flask

337.44

1.0.2

2uWSGI

756.72

2.0.17.1

3Gevent

817.57

1.3.7

4Gunicorn(gevent)

1952.39

19.9.0

5Tornado

587.53

5.1.1

6Twisted

1820.75

18.9.0

7Falcon

2655.53

1.4.1

8Sanic

1081.31

0.8.3

9Vibora

4011.29

0.0.6

siege -c100 -r10000 -t10s http://localhost:5000/

Index

Frameworks

Requests/Sec

Version

1Flask

336.62

1.0.2

2uWSGI

918.54

2.0.17.1

3Gevent

450.48

1.3.7

4Gunicorn(gevent)

1294.98

19.9.0

5Tornado

410.35

5.1.1

6Twisted

949.29

18.9.0

7Falcon

884.20

1.4.1

8Sanic

665.61

0.8.3

9Vibora

956.35

0.0.6

图表

地址:https://datastudio.google.com/open/1YCVHX0qyoGx2lZaqzQA979c28G6Fxr1-

综合下来,Vibora, Falcon, Gunicorn 整体表现不错,进入三强。

尴尬的是:尽管Sanic在各平台表现也很好,但是整体下来没有进入三强,很可惜。。。想哭

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/521982
推荐阅读
相关标签