Skip to content

Commit

Permalink
add: pyinstrument for profiling api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
prabinoid committed May 6, 2024
1 parent 8ce70d1 commit 6cc13c3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
21 changes: 19 additions & 2 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import json
from logging.handlers import RotatingFileHandler

from flask import Flask, redirect, request
from flask import Flask, redirect, request, g, make_response
from flask_cors import CORS
from flask_migrate import Migrate
from requests_oauthlib import OAuth2Session
Expand All @@ -26,7 +26,7 @@
from flask_mail import Mail

from backend.config import EnvironmentConfig

from pyinstrument import Profiler

# Load error_messages.json and store it so that it is loaded only once at startup (Used in exceptions.py)
# Construct the path to the JSON file
Expand Down Expand Up @@ -150,6 +150,23 @@ def handle_generic_error(error):
def index_redirect():
return redirect(format_url("system/heartbeat/"), code=302)


@app.before_request
def before_request():
if "pyinstrument" in request.args:
g.profiler = Profiler()
g.profiler.start()


@app.after_request
def after_request(response):
if not hasattr(g, "profiler"):
return response
g.profiler.stop()
output_html = g.profiler.output_html()
return make_response(output_html)


# Add paths to API endpoints
add_api_endpoints(app)

Expand Down
48 changes: 43 additions & 5 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies = [
"shapely==2.0.1",
"SQLAlchemy==2.0.19",
"Werkzeug==2.3.6",
"pyinstrument==4.6.2",
# Indirect, but required dependencies (often required for efficient deployments)
"gevent==22.10.2",
"greenlet==2.0.2",
Expand Down

0 comments on commit 6cc13c3

Please sign in to comment.