Bob Belderbos: Profile First: ... Note

Bob Belderbos: Profile First: A 10x Faster Django Test Suite

The author's Rust Platform Django test suite initially ran for 30 seconds, prompting a hunch about database performance. However, profiling with cProfile revealed the real bottleneck was not database-related, cutting the runtime to 3 seconds. The text emphasizes the importance of measuring performance rather than guessing at solutions like too many fixtures or database slowness. It advocates for using profiling tools like cProfile to identify specific bottlenecks. The author used cProfile to generate a pstats file and then profiling-explorer to visualize the data. The profiler highlighted that the _hashlib.pbkdf2_hmac function, Django's default password hasher, was consuming excessive time during tests. This hasher is intentionally slow to resist brute-force attacks, but it unnecessarily slows down every test that creates a user. A simple five-line fix was implemented by swapping to a faster MD5 hasher specifically for tests. This change proved highly effective, reducing test suite execution time significantly. After this initial optimization, the next most time-consuming function was cursor.execute, which initially seemed like a potential N+1 query issue. However, further measurement confirmed no N+1 queries were occurring; the high call count was attributed to legitimate test setup operations. This experience reinforces the central message: always profile before optimizing to avoid wasted effort and incorrect assumptions.
CdXz5zHNQW_5c2sgQR2VI.png