Profiling is an essential tool for identifying performance bottlenecks in your
TurboGears application. The tgext.debugbar extension provides profiling
capabilities when it is installed and enabled.
Quickstarted projects include tg.devtools in their development extra; it
installs tgext.debugbar. Install that extra to enable the generated debugbar
configuration:
$ python -m pip install -e ".[development]"
The generated configuration enables the installed debugbar when debug = true.
For a project without tg.devtools, install tgext.debugbar and add this to
config/app_cfg.py:
$ python -m pip install tgext.debugbar
from tgext.debugbar import enable_debugbar
enable_debugbar(base_config)
The debugbar is injected only into non-AJAX text/html responses that contain
both </head> and </body> markup.
Once enabled, the debugbar provides several tabs with profiling information:
The SQL tab shows database queries executed during the request, including:
The SQL statement
Parameters used
Duration
This helps identify slow queries or N+1 query problems.
For SELECT statements, Results displays the returned rows and column
names. You can also view the execution plan by clicking Explain, which helps
optimize complex queries.
The profiling tab provides detailed timing information for the request:
Total time
Controller time
Render time
A Template table with grouped template entries, including total render time and invocation count
cProfile function statistics
This allows you to quickly identify which parts of your application are taking the most time to execute.
To optimize your application performance using the debugbar:
Identify slow requests: Look for requests with high total execution times
Check SQL queries: Look for queries taking more than 100ms or queries executed multiple times for similar data (N+1 problem)
Analyze function statistics: Identify expensive controller or template work
Review execution plans: For slow SQL queries, check if they’re using proper indexes or if the query plan is suboptimal
The debugbar SQL tab will show many similar queries being executed in a loop.
Solution: Use SQLAlchemy’s joinedload or subqueryload to eager load
relationships.
If template rendering is taking significant time, check for complex logic in templates or large data sets being iterated. Solution: Move complex logic to controllers or use caching.
The execution plan view can reveal full table scans on large tables. Solution: Add appropriate indexes to your database tables.
The profiling function statistics show where controller time is spent. Solution: Optimize the algorithm, add caching, or move to background tasks.
The debugbar is designed for development use only and should not be enabled in production environments as it:
Adds overhead to every request
Exposes sensitive information about your application
Can impact performance significantly
For production profiling, consider:
Using dedicated profiling tools like cProfile
Enabling profiling temporarily on staging environments
Using monitoring tools like New Relic or Datadog
Reviewing application logs for slow requests
Profile early and often: Check the debugbar regularly during development
Test with realistic data: N+1 problems often only appear with larger datasets
Profile the critical path: Focus on the most frequently used pages and the most important user flows
Set performance budgets: Decide on acceptable response times and investigate anything exceeding them
Profile after changes: Always check performance after adding new features or making significant changes