Files
odoo_source/addons/test_mass_mailing/tests/test_performance.py
T
Raphael ColletandVictor Feyens 840609975a [IMP] core: better way to set/update magic fields
The goal of this change is to simplify the code managing `create_date`
and `write_date` in methods `create()` and `write()`, and also to remove
weird behaviors caused by the way those fields were updated.

Assume we update a simple field on a record.  This adds pending updates
for the field and `write_date`.  However, the value of `write_date` is
not known yet: it will be updated as `NOW() AT TIME ZONE 'UTC'` in SQL.
So `write_date` is actually given a dummy value in pending updates, and
it is invalidated from cache, until its value is flushed to the database
and fetched again.

Now assume we access another field on the record, and that field is not
in cache.  The prefetching mechanism will read all column fields,
including `write_date`, and flush them first.

    # this adds pending updates foo: 42, write_uid: 1, write_date: False
    record.foo = 42

    # assume 'bar' is not in cache; this prefetches all column fields,
    # which flushes the pending updates above before reading them back
    result = record.bar

We can avoid flushing pending updates if the values read from database
do not overwrite existing values in cache.  If you assume that the value
of a pending update is in cache (in the example, `foo: 42`), you don't
need to flush the corresponding field.  Indeed, the value of `foo` will
remain 42 in cache, whatever its value in the database.  This assumption
(pending updates are in cache) is true for all fields *except* for
`write_date`: it is invalidated from cache, and given a dummy value in
pending updates.  This branch actually makes this assumption true for
all fields.  The avoidance of flushing pending updates will be done in
another commit.

In order to directly assign `write_date` its value, we use a cache for
the value `NOW() AT TIME ZONE 'UTC'` from the database.  This costs at
most one query per transaction, and potentially saves a few queries.

Co-authored-by: Victor Feyens <vfe@odoo.com>
2021-02-22 16:20:55 +00:00

99 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.mail.tests.common import mail_new_test_user
from odoo.tests.common import TransactionCase, users, warmup
from odoo.tests import tagged
from odoo.tools import mute_logger
class TestMassMailPerformanceBase(TransactionCase):
def setUp(self):
super(TestMassMailPerformanceBase, self).setUp()
self.user_employee = mail_new_test_user(
self.env, login='emp',
groups='base.group_user',
name='Ernest Employee', notification_type='inbox')
self.user_marketing = mail_new_test_user(
self.env, login='marketing',
groups='base.group_user,mass_mailing.group_mass_mailing_user',
name='Martial Marketing', signature='--\nMartial')
# patch registry to simulate a ready environment
self.patch(self.env.registry, 'ready', True)
@tagged('mail_performance')
class TestMassMailPerformance(TestMassMailPerformanceBase):
def setUp(self):
super(TestMassMailPerformance, self).setUp()
values = [{
'name': 'Recipient %s' % x,
'email_from': 'Recipient <rec.%s@example.com>' % x,
} for x in range(0, 50)]
self.mm_recs = self.env['mailing.performance'].create(values)
@users('__system__', 'marketing')
@warmup
@mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink', 'odoo.tests')
def test_send_mailing(self):
mailing = self.env['mailing.mailing'].create({
'name': 'Test',
'subject': 'Test',
'body_html': '<p>Hello <a role="button" href="https://www.example.com/foo/bar?baz=qux">quux</a><a role="button" href="/unsubscribe_from_list">Unsubscribe</a></p>',
'reply_to_mode': 'email',
'mailing_model_id': self.ref('test_mass_mailing.model_mailing_performance'),
'mailing_domain': [('id', 'in', self.mm_recs.ids)],
})
# runbot needs +50 compared to local
with self.assertQueryCount(__system__=1664, marketing=1664):
mailing.action_send_mail()
self.assertEqual(mailing.sent, 50)
self.assertEqual(mailing.delivered, 50)
@tagged('mail_performance')
class TestMassMailBlPerformance(TestMassMailPerformanceBase):
def setUp(self):
""" In this setup we prepare 20 blacklist entries. We therefore add
20 recipients compared to first test in order to have comparable results. """
super(TestMassMailBlPerformance, self).setUp()
values = [{
'name': 'Recipient %s' % x,
'email_from': 'Recipient <rec.%s@example.com>' % x,
} for x in range(0, 62)]
self.mm_recs = self.env['mailing.performance.blacklist'].create(values)
for x in range(1, 13):
self.env['mail.blacklist'].create({
'email': 'rec.%s@example.com' % (x * 5)
})
self.env['mailing.performance.blacklist'].flush()
@users('__system__', 'marketing')
@warmup
@mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink', 'odoo.tests')
def test_send_mailing_w_bl(self):
mailing = self.env['mailing.mailing'].create({
'name': 'Test',
'subject': 'Test',
'body_html': '<p>Hello <a role="button" href="https://www.example.com/foo/bar?baz=qux">quux</a><a role="button" href="/unsubscribe_from_list">Unsubscribe</a></p>',
'reply_to_mode': 'email',
'mailing_model_id': self.ref('test_mass_mailing.model_mailing_performance_blacklist'),
'mailing_domain': [('id', 'in', self.mm_recs.ids)],
})
# runbot needs +62 compared to local
with self.assertQueryCount(__system__=1942, marketing=1942):
mailing.action_send_mail()
self.assertEqual(mailing.sent, 50)
self.assertEqual(mailing.delivered, 50)