flask-profiler的使用

使用 profiler测量在你的Flask 应用程序中定义的端点;并通过web界面提供细粒度的报告。

它给出了这些问题的答案:

  • 应用程序中的瓶颈在哪里?
  • 应用程序中最慢的终结点?
  • 哪些是最常被调用的终结点?
  • 什么导致我的慢速端点? 在哪个上下文中,什么是 ARGS 和 kwargs?
  • 特定请求花费了多少时间?

简而言之,如果你对端点正在做什么和接收的请求进行了了解,请尝试打瓶探查器。

通过使用烧瓶分析器接口,你可以监视所有端点的性能,并通过向下钻取过滤器来调查端点和接收的请求。

1.安装 flask_profiler

pip install flask_profiler

2.在创建 Flask 应用程序时编辑你的代码。

from flask import Flask
import flask_profiler

app = Flask(__name__)
app.config["DEBUG"] = True
# 您需要声明必要的配置才能初始化
app.config["flask_profiler"] = {
    "enabled": app.config["DEBUG"],
    "storage": {
        "engine": "sqlite"},
    "basicAuth": {
        "enabled": True,
        "username": "admin",
        "password": "admin"},
    "ignore": [
        "^/static/.*"]
}
# 为了激活flask-profiler,您必须通过flask
# app作为flask-profiler的参数。
# 到目前为止,flask-profiler将跟踪所有已声明的端点。
flask_profiler.init_app(app)


@app.route('/helloworld')
@flask_profiler.profile()  #  来明确指定此端点()
def hello_world():
    return 'Hello World!'

@app.route('/welcome', methods=['GET'])
@flask_profiler.profile()
def doSomethingImportant():
    return "welcome"



if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)
访问url 确保接口可以成功调用
# http://127.0.0.1:5000/helloworld
# http://127.0.0.1:5000/welcome
# 如果一切可以,Flask-profiler会衡量这些请求。你可以看到结果前往http://127.0.0.1:5000/flask-profiler/
##使用与不同的数据库系统当前支持** Sqlite **和** Mongodb **数据库系统。但是,很容易支持其他数据库系统。如果您想要其他人,请转到贡献文档。(这确实很容易。)

原文地址:https://www.cnblogs.com/zlel/p/11676571.html