gpt4 book ai didi

flask - 为什么原始 wsgi 应用程序比 flask 应用程序慢?

转载 作者:行者123 更新时间:2023-12-03 16:53:25 25 4
gpt4 key购买 nike

我写了两个简单的应用程序,一个是像下面这样的原始 wsgi 应用程序,另一个是用 Flask 构建的,它们都运行在 gevent wsgi 服务器上。
当应用程序中没有网络连接时,如我所料,原始 wsgi 应用程序比 flask 应用程序更快,但是当应用程序中有一些网络连接时,原始 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()

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

我正在使用 ab做基准测试:
$ ab -c10 -n10000 http://127.0.0.1:8080/

这是原始的 wsgi 应用程序结果:

并发级别:10
测试时间:306.216 秒
每秒请求数:1.52 [#/sec](平均值)
每个请求的时间:6585.299 [ms](平均)
每个请求的时间:658.530 [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% 5938
66% 7584
75% 8597
80% 9186
90% 10829
95% 12033
98% 13209
99% 14722
100% 15963(最长请求)

和 flask 应用程序的:

并发级别:10
测试时间:19.909 秒
每秒请求数:502.28 [#/sec](平均)
每个请求的时间:19.909 [ms](平均值)
每个请求的时间:1.991 [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% 19
66% 23
75% 25
80% 27
90% 31
95% 36
98% 41
99% 45
100% 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% 时间,这似乎是合理的。

关于flask - 为什么原始 wsgi 应用程序比 flask 应用程序慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33842299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com