Profiling and speeding up Django and Pytest

Éloi made a pull request on IHateMoney to speedup the tests, with some great tooling for pytest that I wasn’t aware of:

So, I spent some time using these on the tests for La Chariotte, because they were slow.

I found two things :

Changing the hashing algorithm to speedup tests

By default, Django uses a slow (but secure !) hashing mechanism for checking the user credentials. In the tests, we don’t need this security, but we need the speed.

Changing them to use MD5 turns out to be a way to greatly speed them up! Here is how to do it with a pytest fixture :

@pytest.fixture(autouse=True)
def password_hasher_setup(settings):
    # Use a weaker password hasher during tests, for speed
    settings.PASSWORD_HASHERS = [
        "django.contrib.auth.hashers.MD5PasswordHasher",
    ]

Speeding DNS lookups

I’m currently using a MacOSX machine, and for for whatever reason, the local lookup was not configured properly on my machine. I don’t think I did anything specific to get this wrong, so it might be your case too. Calls to resolve the local domain were tooking 5s.

If the answer to scutil --get LocalHostName, hostname and scutil --get HostName differ, then you might be in this case. Here is the fix :

sudo scutil --set HostName <YourHostName>

#django, #pytest - Posté dans la catégorie code