Skip to content

Instantly share code, notes, and snippets.

@zzantares
Last active March 30, 2021 12:46
Show Gist options
  • Save zzantares/d2b7553feb0fd900a79112fd8e5cd4d5 to your computer and use it in GitHub Desktop.
Save zzantares/d2b7553feb0fd900a79112fd8e5cd4d5 to your computer and use it in GitHub Desktop.
Profiling django tests, read more at https://pymotw.com/2/profile/
# based on https://djangosnippets.org/snippets/1315/
from django.conf import settings
from django.test.runner import DiscoverRunner
try:
import cProfile as profile
except ImportError:
import profile
class ProfilingRunner(DiscoverRunner):
def run_tests(self, *args, **kwargs):
s = super(ProfilingRunner, self)
profile.runctx(
'run_tests(*args, **kwargs)',
{
'run_tests': s.run_tests,
'args': args,
'kwargs': kwargs
},
{},
getattr(settings, 'TEST_PROFILE', None))
TEST_RUNNER = 'profiling.ProfilingRunner'
TEST_PROFILE = None # Set to a file path to create a pstats readable binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment