I wrote two simple apps, one is raw wsgi application like below while another one is built with Flask, and both run on gevent wsgi server.
When there are no network connections in the app, as I expected, raw wsgi app is more faster than the flask app, but when there are some network connections in the app, raw wsgi app is way more slower than the flask app.
raw
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()
flask
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()
I'm using ab
to do the benchmark test:
$ ab -c10 -n10000 http://127.0.0.1:8080/
here is the raw wsgi app result:
Concurrency Level: 10 Time taken for tests: 306.216 seconds Requests per second: 1.52 [#/sec] (mean) Time per request: 6585.299 [ms] (mean) Time per request: 658.530 [ms] (mean, across all concurrent requests) Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.4 0 7 Processing: 1084 6499 3050.3 5951 15963 Waiting: 96 5222 3051.4 4577 15096 Total: 1085 6500 3050.2 5951 15963 Percentage of the requests served within a certain time (ms) 50% 5938 66% 7584 75% 8597 80% 9186 90% 10829 95% 12033 98% 13209 99% 14722 100% 15963 (longest request)
and flask app's:
Concurrency Level: 10 Time taken for tests: 19.909 seconds Requests per second: 502.28 [#/sec] (mean) Time per request: 19.909 [ms] (mean) Time per request: 1.991 [ms] (mean, across all concurrent requests) Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 2 Processing: 3 20 9.0 19 87 Waiting: 2 20 8.9 19 86 Total: 3 20 9.0 19 87 Percentage of the requests served within a certain time (ms) 50% 19 66% 23 75% 25 80% 27 90% 31 95% 36 98% 41 99% 45 100% 87 (longest request)
So I'm wondering what did flask do and what can I do to be more faster using a simple wsgi app without a framework?