#24380 closed Cleanup/optimization (wontfix)
First locmem email sent in tests is absurdly slow.
Reported by: | Fletcher Tomalty | Owned by: | nobody |
---|---|---|---|
Component: | Core (Mail) | Version: | 1.7 |
Severity: | Normal | Keywords: | email fqdn performance tests |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Some of my application's tests send email. When running tests, Django sets EMAIL_BACKEND
to be django.core.mail.backends.locmem.EmailBackend
. This backend's send_messages
calls message.message()
on each email in order to do header validation. However, this has the side effect of generating a Message-ID
email header, which calls make_msgid()
, which ends up calling socket.getfqdn()
. On my machine, socket.getfqdn()
takes over 5 seconds to return. The socket.getfqdn()
is cached, fortunately, so the 5 second delay only happens once.
Five seconds may seem small but it is really significant when you're rapidly alternating between writing code and running tests, and it's totally unnecessary to set a Message-ID
header on emails in django.core.mail.backends.locmem
. Right now my large test suite runs in 600 ms if I comment out that one pesky call that turns it into 5800 ms. So while this isn't a bug, I think fixing it would be a really nice quality-of-life change.
Change History (4)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I agree, I think the locmem backend should handle messages as similarly as possible to the real backend. If socket.getfqdn()
is a problem on your particular machine, it shouldn't be hard to mock it for your tests.
comment:3 by , 10 years ago
I also agree that your suggestion to use a mock monkey patch is a better idea that changing the locmem
backend just for tests. Thanks!
comment:4 by , 7 years ago
I've come up with a simple email backend subclass/mixin that doesn't require monkeypatching. Check it out here: https://gist.github.com/jonashaag/69ed72632e0780899755b592492756dd
We might argue that the real bug is your machine's
socket.getfqdn()
slowness. I'm not sure it's a good idea to strip thelocmem
backend (see #18861 about the reason this was introduced).I think the solution would be for you to mock
socket.getfqdn()
in your tests.Keeping it open for getting another opinion.