Skip to content

Instantly share code, notes, and snippets.

@benny-shotvibe
Created November 8, 2015 13:32
Show Gist options
  • Save benny-shotvibe/4a632a1be06ff06f0ab8 to your computer and use it in GitHub Desktop.
Save benny-shotvibe/4a632a1be06ff06f0ab8 to your computer and use it in GitHub Desktop.
Django poor man's view performance measurement
# Make sure to set in settings.py:
# DEBUG = True
from django.db import connection
from django import db
import time
db.reset_queries()
start = time.time()
result = VIEW_FUNCTION_HERE(request) # !!!!! EDIT THIS LINE !!!!!
end = time.time()
total_queries_time = 0
with open('/tmp/log.txt', 'w') as f:
for q in connection.queries:
total_queries_time += float(q['time']) + 0.0005
f.write(q['time'] + ' ')
f.write(q['sql'] + '\n')
f.write('Queries time: ' + str(total_queries_time) + '\n')
f.write('Total time: ' + str(end - start) + '\n')
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment