Performance Profiling with Pyinstrument

The Python package Pyinstrument can be added to GovReady-Q to profile performance issues within the code.

Pyinstrument includes a Django middleware that when added to GovReady-Q’s siteapp/settings_application.py file enables displaying of profile results directly in the browser.

Use Pip to install Pyinstrument Python package.

pip install pyinstrument

Add pyinstrument.middleware.ProfilerMiddleware to siteapp/settings_application.py.

MIDDLEWARE += [
    #'debug_toolbar.middleware.DebugToolbarMiddleware',
    'siteapp.middleware.ContentSecurityPolicyMiddleware',
    'guidedmodules.middleware.InstrumentQuestionPageLoadTimes',
    'pyinstrument.middleware.ProfilerMiddleware'
]

Restart GovReady-Q server.

Once installed, add ?profile to the end of a request URL to activate the profiler (or &profile if URL already includes GET params).

If you wish to profile every request, make sure to set your profiles directory in your settings.py:

PYINSTRUMENT_PROFILE_DIR = 'profiles'

The HTML documents generated will be in the designated profiles folder which will be instantiated if there is not already one in your working directory.

See Pyinstrument Profile a web request in Django for more details.