Files
odoo_source/addons/website_blog/tests/test_performance.py
T
Rémy Voet (ryv) d2b68d186e [FIX] website: remove force prefetch for translate fields
Issue
-----
When website is installed, the rendering of template uses a side effect
of the ORM cache (cache shared between sudoed env vs non-sudoed env) and
the fields prefetching feature to work correctly.

The `self.visibility` in (`_handle_visibility`, website/ir_ui_view.py)
is done in sudo mode, then it will fetch all prefetchable fields and put
them in the cache (that will be read in non-sudo mode in the render of
the template).  Another example of issue related to this:
https://github.com/odoo/odoo/pull/83341.

Because of this, the fields of mixin `website.seo.metadata` were forced
to be prefetchable (the default for translate is to be not prefetchable
since https://github.com/odoo/odoo/pull/82896), which causes a useless
LEFT JOIN on "ir_translation" in most of business flow.

Fix
---
Remove the `prefetch=True` on mixin fields, and add a extra read to fill
the cache in case of website rendering.  It also allows to read these
fields at the same time.

Part-of: odoo/odoo#85220
2022-03-03 11:03:55 +00:00

48 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.website.tests.test_performance import UtilPerf
import random
class TestBlogPerformance(UtilPerf):
def setUp(self):
super().setUp()
# if website_livechat is installed, disable it
if 'channel_id' in self.env['website']:
self.env['website'].search([]).channel_id = False
def test_10_perf_sql_blog_standard_data(self):
self.assertEqual(self._get_url_hot_query('/blog'), 26)
self.assertEqual(self._get_url_hot_query('/blog', cache=False), 25)
def test_20_perf_sql_blog_bigger_data_scaling(self):
BlogPost = self.env['blog.post']
BlogTag = self.env['blog.tag']
blogs = self.env['blog.blog'].search([])
blog_tags = BlogTag.create([{'name': 'Blog Tag Test %s' % i} for i in range(1, 20)])
BlogPost.create([{'name': 'Blog Post Test %s' % i, 'is_published': True, 'blog_id': blogs[i % 2].id} for i in range(1, 20)])
blog_posts = BlogPost.search([])
for blog_post in blog_posts:
blog_post.tag_ids += blog_tags
blog_tags = blog_tags[:-1]
self.assertEqual(self._get_url_hot_query('/blog'), 26)
self.assertEqual(self._get_url_hot_query('/blog', cache=False), 25)
self.assertEqual(self._get_url_hot_query(blog_post[0].website_url), 31)
self.assertEqual(self._get_url_hot_query(blog_post[0].website_url, cache=False), 30)
def test_30_perf_sql_blog_bigger_data_scaling(self):
BlogPost = self.env['blog.post']
BlogTag = self.env['blog.tag']
blogs = self.env['blog.blog'].search([])
blog_tags = BlogTag.create([{'name': 'New Blog Tag Test %s' % i} for i in range(1, 50)])
BlogPost.create([{'name': 'New Blog Post Test %s' % i, 'is_published': True, 'blog_id': blogs[random.randint(0, 1)].id} for i in range(1, 100)])
blog_posts = BlogPost.search([])
for blog_post in blog_posts:
blog_post.write({'tag_ids': [[6, 0, random.choices(blog_tags.ids, k=random.randint(0, len(blog_tags)))]]})
self.assertLessEqual(self._get_url_hot_query('/blog'), 27)
self.assertLessEqual(self._get_url_hot_query('/blog', cache=False), 26)
self.assertLessEqual(self._get_url_hot_query(blog_post[0].website_url), 32)
self.assertLessEqual(self._get_url_hot_query(blog_post[0].website_url, cache=False), 31)