Flask-profiler measures endpoints defined in your flask application; and provides you fine-grained report through a web interface.
It gives answers to these questions:
- Where are the bottlenecks in my application?
- Which endpoints are the slowest in my application?
- Which are the most frequently called endpoints?
- What causes my slow endpoints? In which context, with what args and kwargs are they slow?
- How much time did a specific request take?
In short, if you are curious about what your endpoints are doing and what requests they are receiving, give a try to flask-profiler.
With flask-profiler's web interface, you can monitor all your endpoints' performance and investigate endpoints and received requests by drilling down through filters.
It is easier to understand flask-profiler going through an example. Let's dive in.
Install flask-profiler by pip.
pip install flask_profilerEdit your code where you are creating Flask app.
# your app.py
from flask import Flask
import flask_profiler
app = Flask(__name__)
app.config["DEBUG"] = True
# You need to declare necessary configuration to initialize
# flask-profiler as follows:
app.config["flask_profiler"] = {
"enabled": app.config["DEBUG"],
"basicAuth":{
"enabled": True,
"username": "admin",
"password": "admin"
},
}
@app.route('/product/<id>', methods=['GET'])
def getProduct(id):
return "product id is " + str(id)
@app.route('/product/<id>', methods=['PUT'])
def updateProduct(id):
return "product {} is being updated".format(id)
@app.route('/products', methods=['GET'])
def listProducts():
return "suppose I send you product list..."
@app.route('/static/something/', methods=['GET'])
def staticSomething():
return "this should not be tracked..."
# In order to activate flask-profiler, you have to pass flask
# app as an argument to init_app.
# All the endpoints declared so far will be tracked by flask-profiler.
flask_profiler.init_app(app)
# endpoint declarations after flask_profiler.init_app() will be
# hidden to flask_profiler.
@app.route('/doSomething', methods=['GET'])
def doSomething():
return "flask-profiler will not measure this."
# But in case you want an endpoint to be measured by flask-profiler,
# you can specify this explicitly by using profile() decorator
@app.route('/doSomethingImportant', methods=['GET'])
@flask_profiler.profile()
def doSomethingImportant():
return "flask-profiler will measure this request."
if __name__ == '__main__':
app.run(host="127.0.0.1", port=5000)
Now run your app.py
python app.py
And make some requests like:
curl http://127.0.0.1:5000/products
curl http://127.0.0.1:5000/product/123
curl -X PUT -d arg1=val1 http://127.0.0.1:5000/product/123If everything is okay, Flask-profiler will measure these requests. You can see the result heading to http://127.0.0.1:5000/flask-profiler/ or get results as JSON http://127.0.0.1:5000/flask-profiler/api/measurements?sort=elapsed,desc
app.config["flask_profiler"] = {
"storage": {
"FILE": "flask_profiler.sql",
}
}Below the other options are listed.
| Filter key | Description | Default |
|---|---|---|
| storage.FILE | SQLite database file name | flask_profiler.sql |
By default, we can access flask-profiler at /flask-profiler
app.config["flask_profiler"] = {
"endpointRoot": "secret-flask-profiler"
}- Musafa Atik
- Fatih Sucu
- Safa Yasin Yildirim
MIT