-
Notifications
You must be signed in to change notification settings - Fork 19
Debugging and Profiling
Use the bpython or ipython django shell to test functions and models (see instructions below).
Debug backend code using ipdb or pudb:
def my_view_func(request):
user = request.user
import ipdb; ipdb.set_trace() # this line sets a breakpoint. also check out pudb as a graphical alternative to ipdb
return render('my/template.html', {'name': user.get_full_name()})
./manage.py runserver --ipdb # will drop into ipdb on exceptions in any view (only active when DEBUG=True)
./manage.py test --ipdb # will drop into ipdb on exceptions in any test (only active when DEBUG=True)
This enable ipdb shell for your environment. The current stack should be stopped when you exec the command.
# Adding the breakpoint - import ipdb; ipdb.set_trace()
docker-compose run --rm django
Make sure you're using oddslingers watchjs
and not compjs
when debugging, not only is it faster, it also enables friendly errors and tracebacks with sourcemaps that aren't available in production mode. Beware this slows execution down somewhat, so don't profile JS when using watchjs
. Make sure you have the dev console open and "Disable Cache" checked in the Network tab so you aren't running stale or cached javascript.
The Chrome Dev Tools are your best friend. Learn them thoroughly and learn them well, and you will never need another debugging tool.
You can set breakpoints to inspect in the Chrome debugger live in the browser, but you can also add them in your editor.
function someJSfunction() {
cont a = 1
if (a != 1) {
debugger // this line sets a JS breakpoint that you can inspect in browser devtools
}
}
Use oddslingers watchcss
when changing CSS, and make sure you have the dev console open and "Disable Cache" checked in the Network tab so you aren't viewing stale changes.
The browser devtools profiler for JS, make sure to profile only production JS builds made with compjs.
Use the django-debug-toolbar's SQL Queries panel. You can expand queries using the "Explain" tab to see where they were executed in the code, and how the SQL engine executed them.
Use the django-debug-toolbars panels for:
- Profiling to break down functions by CPU time taken
- Cache panels to profile backend performance.
- Templates for template rendering performance
You can also profile raw python snippets by simply recording the timestamps before and after, or with import cProfile
cProfile.run('''for i in range(100000): some_slow_func(i)''')
.