Skip to content

Instantly share code, notes, and snippets.

@drozdowsky
Created June 24, 2020 09:23
Show Gist options
  • Save drozdowsky/739ef5d8080d4feefb8ef859121649d3 to your computer and use it in GitHub Desktop.
Save drozdowsky/739ef5d8080d4feefb8ef859121649d3 to your computer and use it in GitHub Desktop.
Get most costly places (in terms of sql queries) in code
from collections import defaultdict
from silk import models
def should_ignore_traceback(traceback):
should_have = ["File", "ecommerce-backend"]
should_not_have = [
"wsgi/health_check.py",
"core/middleware.py",
"graphql/views.py",
"site/patch_sites.py",
"core/mutations.py",
"graphql/middleware.py",
]
for having in should_have:
if having not in traceback:
return True
for not_having in should_not_have:
if not_having in traceback:
return True
return False
def get_agg_traceback(request: "SilkRequest"):
agg_traceback = defaultdict(list)
for query in request.queries.all():
tracebacks = query.traceback.split("\n")
for traceback in tracebacks:
if not should_ignore_traceback(traceback):
agg_traceback[traceback].append(query.query)
# sort dictionary based on query number
for traceback, queries_list in sorted(
agg_traceback.items(), key=lambda item: len(item[1])
):
queries = set(queries_list)
print(traceback, queries, "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment