我写了两个简单的应用程序,一个是下面的原始wsgi应用程序,另一个是用Flask构建的,都在gevent-wsgi服务器上运行
正如我所料,当应用程序中没有网络连接时,raw-wsgi应用程序比flask应用程序更快,但当应用程序有一些网络连接时时,raw-wsgi应用软件比flask程序慢得多。
未加工的
import json
from gevent import monkey
monkey.patch_all() # monkey patch for both apps
import pymysql
conn = pymysql.connect(host=HOST,port=PORT,...,cursorclass=pymysql.cursors.DictCursor)
import requests
def application(environ, start_response):
# database connection
cur = conn.cursor()
cur.execute('select * from food')
res = cur.fetchall()
# requests
resp = requests.get('http://www.baidu.com')
start_response('200 OK', [('Content-Type', 'application')])
return json.dumps(res)
# return resp.content
from gevent.wsgi import WSGIServer
http_server = WSGIServer(('', 8080), application)
http_server.serve_forever()
烧瓶
from gevent import monkey
monkey.patch_all()
import json
from flask import Flask
app = Flask(__name__)
conn = pymysql.connect(host=HOST,port=PORT,...,cursorclass=pymysql.cursors.DictCursor)
@app.route('/')
def index():
# database connection
cur = conn.cursor()
cur.execute('select * from food')
res = cur.fetchall()
# requests
resp = requests.get('http://www.baidu.com')
return json.dumps(res), 200
from gevent.wsgi import WSGIServer
http_server = WSGIServer(('', 8080), app)
http_server.serve_forever()
我使用ab
进行基准测试:
$ ab -c10 -n10000 http://127.0.0.1:8080/
以下是wsgi应用程序的原始结果:
并发级别:10测试时间:306.216秒每秒请求数:1.52[#/sec](平均值)每次请求的时间:6585.299[ms](平均值)每个请求的时间:658.530[ms](所有并发请求的平均值)连接时间(ms)最小平均值[+/-sd]中值最大值连接:0 0 0.4 0 7处理:1084 6499 3050.3 5951 15963等待:96 5222 3051.4 4577 15096总计:1085 6500 3050.2 5951 15963在特定时间内送达的请求的百分比(毫秒)50%593866%758475%859780%918690%1082995%1203398%1320999%14722100%15963(最长请求)
和烧瓶应用程序:
并发级别:10测试时间:19.909秒每秒请求数:502.28[#/sec](平均值)每次请求的时间:19.909[ms](平均值)每个请求的时间:1.991[ms](所有并发请求的平均值)连接时间(ms)最小平均值[+/-sd]中值最大值连接:0 0 0.0 0 2处理:3 20 9.0 19 87等待:2 20 8.9 19 86总计:3 20 9.0 19 87在特定时间内送达的请求的百分比(毫秒)50%1966%2375%2580%2790%3195%3698%4199%45100%87(最长请求)
所以我想知道flask做了什么,在没有框架的情况下使用一个简单的wsgi应用程序,我能做些什么来更快?
我认为你的问题不存在。您犯的最大错误是引入了与web框架性能无关的IO部分(网络IO和磁盘IO)。
为了证明这一点,我将您的演示简化为以下内容:
import json
from gevent import monkey
monkey.patch_all() # monkey patch for both apps
def application(environ, start_response):
res = dict(hello='world')
start_response('200 OK', [('Content-Type', 'application')])
return json.dumps(res)
from gevent.wsgi import WSGIServer
http_server = WSGIServer(('', 8088), application)
http_server.serve_forever()
和:
from gevent import monkey
monkey.patch_all()
import json
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
res = dict(hello='world')
return json.dumps(res), 200
from gevent.wsgi import WSGIServer
http_server = WSGIServer(('', 8088), app)
http_server.serve_forever()
我的原始WSGI结果是:
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 5
Processing: 1 5 0.7 5 23
Waiting: 1 5 0.7 5 23
Total: 1 6 0.7 5 24
WARNING: The median and mean for the total time are not within a normal deviation
These results are probably not that reliable.
Percentage of the requests served within a certain time (ms)
50% 5
66% 6
75% 6
80% 6
90% 6
95% 6
98% 7
99% 8
100% 24 (longest request)
对于Flask,它是:
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 1 6 0.6 6 11
Waiting: 1 6 0.6 6 11
Total: 2 6 0.6 6 11
Percentage of the requests served within a certain time (ms)
50% 6
66% 6
75% 6
80% 6
90% 7
95% 7
98% 7
99% 8
100% 11 (longest request)
忽略最长的1%请求,你可以发现原始WSGI比Flask快20%,这似乎是合理的。