Skip to content

Instantly share code, notes, and snippets.

@Julian-Nash
Created December 13, 2020 17:49
Show Gist options
  • Save Julian-Nash/679136bb997eff9b16839af513b63d99 to your computer and use it in GitHub Desktop.
Save Julian-Nash/679136bb997eff9b16839af513b63d99 to your computer and use it in GitHub Desktop.
Flask profiler
""" Flask application profiler
Will run the Flask application and log profile data to stdout.
The `restrictions` parameter passed to `ProfilerMiddleware` is used to limit the number of items listed
in the report.
"""
import os
from werkzeug.middleware.profiler import ProfilerMiddleware
from app import create_app
app = create_app(os.getenv("FLASK_ENV"))
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
app.run(debug=True)
@Julian-Nash
Copy link
Author

Example truncated output

PATH: '/api/v1/test'
         6386 function calls (6234 primitive calls) in 0.025 seconds

   Ordered by: internal time, call count
   List reduced from 645 to 30 due to restriction <30>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       12    0.015    0.001    0.015    0.001 {method 'recv_into' of '_socket.socket' objects}
        1    0.000    0.000    0.000    0.000 {built-in method _socket.getaddrinfo}
        5    0.000    0.000    0.000    0.000 {built-in method pymongo._cmessage._op_msg}
        1    0.000    0.000    0.000    0.000 {method 'readlines' of '_io._IOBase' objects}
     1095    0.000    0.000    0.000    0.000 {built-in method builtins.isinstance}
        1    0.000    0.000    0.000    0.000 {method 'read' of '_io.BufferedReader' objects}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment