基于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

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在各平台表现也很好,但是整体下来没有进入三强,很可惜。。。想哭

Flask性能优化对比的更多相关文章

  1. laravel 5.1 性能优化对比 - 框架提供的方法

    写了一个项目发现性能不如人意. 于是便测试下, 看下性能瓶颈在什么地方. 使用 ab -n 20 http://www.lartest.com/ 软件环境: OS : windows 8.1 CPU: ...

  2. JDBC插入性能优化对比

    今天对Insert进行了性能测试,结果反差很大,平时都是单条插入,虽然性能要求没有那么高,但是突然在项目中,人家给定时间内完成,这就尴尬了. 优化数据库,优化服务器,优化代码,反正通过各种优化提高数据 ...

  3. Redis各种数据结构性能数据对比和性能优化实践

    很对不起大家,又是一篇乱序的文章,但是满满的干货,来源于实践,相信大家会有所收获.里面穿插一些感悟和生活故事,可以忽略不看.不过听大家普遍的反馈说这是其中最喜欢看的部分,好吧,就当学习之后轻松一下. ...

  4. [评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)

    [评测]低配环境下,PostgresQL和Mysql读写性能简单对比 原文链接:https://www.cnblogs.com/blog5277/p/10658426.html 原文作者:博客园--曲 ...

  5. 针对特定网站scrapy爬虫的性能优化

    在使用scrapy爬虫做性能优化时,一定要根据不同网站的特点来进行优化,不要使用一种固定的模式去爬取一个网站,这个是真理,以下是对58同城的爬取优化策略: 一.先来分析一下影响scrapy性能的set ...

  6. Web性能优化:What? Why? How?

    为什么要提升web性能? Web性能黄金准则:只有10%~20%的最终用户响应时间花在了下载html文档上,其余的80%~90%时间花在了下载页面组件上. web性能对于用户体验有及其重要的影响,根据 ...

  7. Web性能优化:图片优化

    程序员都是懒孩子,想直接看自动优化的点:传送门 我自己的Blog:http://cabbit.me/web-image-optimization/ HTTP Archieve有个统计,图片内容已经占到 ...

  8. JavaScript性能优化

    如今主流浏览器都在比拼JavaScript引擎的执行速度,但最终都会达到一个理论极限,即无限接近编译后程序执行速度. 这种情况下决定程序速度的另一个重要因素就是代码本身. 在这里我们会分门别类的介绍J ...

  9. 前端性能优化的另一种方式——HTTP2.0

    最近在读一本书叫<web性能权威指南>谷歌公司高性能团队核心成员的权威之作. 一直听说HTTP2.0,对此也仅仅是耳闻,没有具体研读过,这次正好有两个篇章,分别讲HTTP1.1和HTTP2 ...

随机推荐

  1. Python_多进程

    Python 多进程库 multiprocessing ,支持子进程.通信.数据共享.执行不同形式的同步 多进程,绕过gil ,实现多核的利用,多进程也是原生进程,由操作系统维护 在pycharm中, ...

  2. asp+SqlServer2008开发【第一集:安装SqlServer2008以及登陆】

    参考:https://blog.csdn.net/i_likechard/article/details/75299983 1,安装sqlServer2008的具体步骤参考网上其他教程,自己根据注释引 ...

  3. 002 python准备做题的一些准备

    在这里,刷刷题,然后,将比较有用的连接粘贴一下在这里.

  4. Mapjoin和Reducejoin案例

    一.Mapjoin案例 1.需求:有两个文件,分别是订单表.商品表, 订单表有三个属性分别为订单时间.商品id.订单id(表示内容量大的表), 商品表有两个属性分别为商品id.商品名称(表示内容量小的 ...

  5. 利用kibana插件对Elasticsearch进行批量操作

    #############批量获取################# #获取所有数据 GET _mget { "docs": [ {"_index":" ...

  6. python 类和元类(metaclass)的理解和简单运用

    (一) python中的类 首先这里讨论的python类,都基于继承于object的新式类进行讨论. 首先在python中,所有东西都是对象.这句话非常重要要理解元类我要重新来理解一下python中的 ...

  7. C语言面对对象设计模式汇编

    面向对象发展到今天,已经出现了许许多多优秀的实践.方法和技术.很多的技术都能够有效的提高软件质量.IBM上的<面向对象软件开发和过程>系列文章对面对对象设计从如下层面进行了详细的介绍:代码 ...

  8. Asia-Tsukuba 2017

    A. Secret of Chocolate Poles DP,$f[i][j]$表示高度为$i$,顶层颜色为$j$的方案数. 时间复杂度$O(l)$. #include<cstdio> ...

  9. bzoj2982: combination(lucas定理板子)

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 664  Solved: 397[Submit][Status][Di ...

  10. CSS3_扇形导航_transitionend 事件

    扇形导航 圆形按钮,切换一系列图片导航的显示与隐藏. 如果涉及过渡动画,定位的 top 和 left 必须写 Math.sin(弧度) 一圈弧度 = 2π,一圈角度 = 360 弧度 = (deg*2 ...