7

I have a complex Django web application that has many person-years of work put into it. It might need optimisation sometime. There are several common operation/flows that I could script with (say) django's test client. Is there some programme that, given a python script like that, will run then, and report on various django specific performance metrics, like 'number of sql queries run'.

Essentially something like a unittest test suite, but rather than reporting "0 tests failed", it'd report "X db queries were made"

I could write this myself, it's not exactly a complex problem, but I wonder has anyone done it before.

I know about Django Debug Toolbar, which can do a lot of this already, but is there something more 'command line' and works on many pages, rather than one page refresh. Likewise getting the actual queries is relatively easy. But has anyone wrapped the whole thing up in a script/library?

Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
  • 1
    this is a similar question explaining how to view queries made by django. http://stackoverflow.com/questions/7918751/capture-sql-queries-via-django-debug-toolbar – dm03514 Feb 22 '12 at 12:47

2 Answers2

3

You can make a TestCase ancestor, something like PerformanceTestCase, which uses setUp() to start the timer and tearDown() to measure time taken and sql queries, and then output wherever you like.

class PerformanceTestCase(TestCase):
    def setUp(self):
        self.begin_time = datetime.datetime.now()

    def tearDown(self):
        delta = datetime.datetime.now() - self.begin_time
        print 'Time taken', delta.seconds

        from django.db import connection
        print 'SQL queries', len(connection.queries)

Maybe you'll need to reset the connection, but i think it's being reset between tests.

ilvar
  • 5,718
  • 1
  • 20
  • 17
3

Use something like graphite or opentsdb combined with something like statsd for non-blocking stats that allow you to measure anything, and plot them in realtime. The best part is that it lets your engineers easily plot whatever they need. Hooked up with collectd, you can graph your apps against memory/cpu usage, db queries.

Here is a sample image from a blog article on how etsy is using graphite:

etsy graphite example

ashwoods
  • 2,209
  • 1
  • 22
  • 36
  • +1 for statsd. The pystatsd module is really easy to work with. Graphite has some quirks you have to get used to, not familiar with opentsdb – Endophage Feb 23 '12 at 06:41