[Django] #24380: First locmem email sent in tests is absurdly slow.

15 views
Skip to first unread message

Django

unread,
Feb 20, 2015, 1:11:32 PM2/20/15
to django-...@googlegroups.com
#24380: First locmem email sent in tests is absurdly slow.
-------------------------------------+-------------------------------------
Reporter: fletom | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Mail) | Version: 1.7
Severity: Normal | Keywords: email fqdn performance
| tests
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/24380>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 20, 2015, 2:01:21 PM2/20/15
to django-...@googlegroups.com
#24380: First locmem email sent in tests is absurdly slow.
-------------------------------------+-------------------------------------
Reporter: fletom | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Mail) | Version: 1.7
Severity: Normal | Resolution:
Keywords: email fqdn | Triage Stage:
performance tests | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

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 the `locmem` 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/24380#comment:1>

Django

unread,
Feb 20, 2015, 2:49:36 PM2/20/15
to django-...@googlegroups.com
#24380: First locmem email sent in tests is absurdly slow.
-------------------------------------+-------------------------------------
Reporter: fletom | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Core (Mail) | Version: 1.7
Severity: Normal | Resolution: wontfix

Keywords: email fqdn | Triage Stage:
performance tests | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by carljm):

* status: new => closed
* resolution: => wontfix


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/24380#comment:2>

Django

unread,
Feb 20, 2015, 4:22:31 PM2/20/15
to django-...@googlegroups.com
#24380: First locmem email sent in tests is absurdly slow.
-------------------------------------+-------------------------------------
Reporter: fletom | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Core (Mail) | Version: 1.7
Severity: Normal | Resolution: wontfix
Keywords: email fqdn | Triage Stage:
performance tests | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by fletom):

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!

--
Ticket URL: <https://code.djangoproject.com/ticket/24380#comment:3>

Django

unread,
Aug 22, 2017, 6:26:19 AM8/22/17
to django-...@googlegroups.com
#24380: First locmem email sent in tests is absurdly slow.
-------------------------------------+-------------------------------------
Reporter: Fletcher Tomalty | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Core (Mail) | Version: 1.7
Severity: Normal | Resolution: wontfix
Keywords: email fqdn | Triage Stage:
performance tests | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jonas Haag):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24380#comment:4>

Reply all
Reply to author
Forward
0 new messages