[Igor Sobreira] Improving performance of Django test suite

Published on: 19/09/2012 12:03h

I’m working on a small django application these days which offers templates to override django-filebrowser’s templates to remove the Grappelli dependency.

It’s a small app, consisting 99% of templates. There are only 19 tests which take 35 seconds to execute! All of them performs at least one request using django test client, no selenium, no real HTTP requests, and still 35 seconds!

After a run with cProfile I’ve noticed many calls to hash functions, specifically django.utils.crypto.pbkdf2() and it’s helper _fast_hmac. Then I came up with this very complex patch to my settings:

+    PASSWORD_HASHERS = (+        'django.contrib.auth.hashers.UnsaltedMD5PasswordHasher',	+    ),

Now the tests execute on 6 seconds!

You probably want a safer hash function for your user passwords in production, but there is no problem to use a simpler one for tests. This extensible way to deal with password hashers was introduced in Django 1.4, you can read more about it on the docs.

By Igor Sobreira