Login

Unit Test Profiling for Django 1.3/1.4

Author:
hoffmaje
Posted:
April 22, 2012
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

The snippet is a modification of snippet 1315 to fit the needs for Django 1.3 and 1.4. You can follow the explanations and instructions there.

To plot a nice and so useful call-graph with timings, call:

$ gprof2dot -f pstats unittest.profile | dot -Tpng -o unittest.profile.graph.png

where 'unittest.profile' is the test runners profile output defined in your settings.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#
# File : profiling.py
#
from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings
from django.utils import unittest


try:
    import cProfile as profile
except ImportError:
    import profile


class DjangoTestSuiteRunnerWithProfile(DjangoTestSuiteRunner):

    def run_suite(self, suite, **kwargs):
        runner = unittest.TextTestRunner(
            verbosity=self.verbosity, failfast=self.failfast).run

        profile.runctx(
            'result = run_tests(suite)',
            {
                'run_tests': runner,
                'suite': suite,
            },
            locals(),
            getattr(settings,'TEST_PROFILE', None)
        )
        return locals()['result']

#
# File : settings.py
#
TEST_RUNNER = 'profiling.DjangoTestSuiteRunnerWithProfile'
TEST_PROFILE = 'unittest.profile'

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.