基于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. vitualbox中的centos7与主机共享文件

    我在vitualbox中安装了一个centos7,最小安装.主机是win10操作系统.那么如何在虚拟机和主机之间进行文件共享呢,下面是本人实现过程,以及过程中遇到的一些问题. 1.在主机中选择一个文件 ...

  2. 在 Mac OS X 下,如何向 sudoers 文件添加新用户

    注: 1.使用待添加的用户名替换"username". 2.以下涉及输入的标点符号均为半角字符,即英文输入法下的标点符号. 以管理员账号密码登录 Mac OS X,打开 Termi ...

  3. HRBUST 1181 移动 bfs模板

    #include<bits/stdc++.h>///该头文件为万能头文件,有些学校oj不能使用,读者可根据需要自行修改 using namespace std; ; int vis[MAX ...

  4. [linux]主机访问虚拟机web服务(CentOS)

    目的为了实现主机和虚拟机的通信,访问虚拟机中架设的web服务.按理说通过虚拟机ip + web服务端口,即可在浏览器访问虚拟机的web服务.但是由于CentOS的防火墙问题,对应web端口无法访问.通 ...

  5. SpringCloud教程 | 第三篇: 服务消费者(Feign)

    上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务.一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http ...

  6. 友元(friend)

    1.友元类的关系不能传递和继承 ...待续

  7. (转)RBAC权限模型——项目实战

    一.前言 权限一句话来理解就是对资源的控制,对web应用来说就是对url的控制,关于权限可以毫不客气的说几乎每个系统都会包含,只不过不同系统关于权限的应用复杂程序不一样而已,现在我们在用的权限模型基本 ...

  8. DAY01-Python入门学习-计算机硬件

    一.Python是编程语言 语言: 一种事物与另外一种事物沟通的介质所以说编程语言是程序员与计算机沟通的介质 什么是编程: 就是程序员用计算机所能理解的表达方式(编程语言)把自己的思维逻辑写下来,编程 ...

  9. jmeter实例介绍

    JMeter基础之一 一个简单的性能测试  测试需求: 1)测试目标网站是fnng.cnblogs.com 和 tt-topia.rhcloud.com 2)测试目的是该网站在负载达到20 QPS 时 ...

  10. Installation of CarbonData 1.1.0 with Spark 1.6.2

    关键词:carbondata spark thrift 数据仓库 [Install thrift 0.9.3] 注意 要装thrift-java必须先装ant . 有人说要装boost,我在cento ...