File: test_performance.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (344 lines) | stat: -rw-r--r-- 15,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging
from odoo.addons.base.tests.common import HttpCaseWithUserPortal, HttpCaseWithUserDemo

from contextlib import nullcontext

from odoo.sql_db import categorize_query
from odoo.tools import mute_logger
from odoo.tests.common import HttpCase, tagged


_logger = logging.getLogger(__name__)


class UtilPerf(HttpCaseWithUserPortal, HttpCaseWithUserDemo):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        # remove menu containing a slug url (only website_helpdesk normally), to
        # avoid the menu cache being disabled, which would increase sql queries
        cls.env['website.menu'].search([
            ('url', '=like', '/%/%-%'),
        ]).unlink()
        # if website_livechat is installed before another module, the
        # get_livechat_channel_info add unrelated query for the current test.
        # So we disable it.
        if 'channel_id' in cls.env['website']:
            cls.env['website'].search([]).channel_id = False

    def _get_url_hot_query(self, url, cache=True, query_list=False):
        """ This method returns the number of SQL Queries used inside a request.
        The returned query number will be the same as a "real" (outside of test
        mode) case: the method takes care of removing the extra queries related
        to the testing mode and to add the missing one from "real" use case.

        The goal is to ease the code reading and debugging of those perf testing
        methods, as one will have the same query count written in the test than
        it shows in "real" case logs.

        eg: if a page is taking X SQL query count to be loaded outside test mode
            in "real" case, the test is expected to also use X as query count
            value to be asserted/checked.

        :param str url: url to be checked
        :param bool cache: whether the QWeb `t-cache` should be disabled or not
        :param bool query_list: whether the method should also return list of
            queries (without test cursor savepoint queries)
        :return: the query count plus the list of queries if ``query_list``
            is ``True``
        :rtype: int|tuple(int, list)
        """
        url += ('?' not in url and '?' or '')
        if cache:
            url += '&debug='
        else:
            url += '&debug=disable-t-cache'

        # ensure worker is in hot state
        self.url_open(url)
        self.url_open(url)
        self.url_open(url)

        nested_profiler = self.profile(collectors=['sql'], db=False)
        with nested_profiler:
            self.registry.get_sequences(self.cr)
            self.url_open(url)

        profiler = nested_profiler.profiler
        self.assertEqual(len(profiler.sub_profilers), 1, "we expect to have only one accessed url") # if not adapt the code below
        route_profiler = profiler.sub_profilers[0]
        route_entries = route_profiler.collectors[0].entries
        entries = profiler.collectors[0].entries + route_entries
        sql_queries = [entry['full_query'].strip() for entry in entries]
        sql_count = len(sql_queries)
        if not query_list:
            return sql_count
        return sql_count, sql_queries

    def _check_url_hot_query(self, url, expected_query_count, select_tables_perf=None, insert_tables_perf=None):
        query_count, sql_queries = self._get_url_hot_query(url, query_list=True)

        sql_from_tables = {}
        sql_into_tables = {}

        query_separator = '\n' + '-' * 100 + '\n'
        queries = query_separator.join(sql_queries)

        for query in sql_queries:
            query_type, table = categorize_query(query)
            if query_type == 'into':
                log_target = sql_into_tables
            elif query_type == 'from':
                log_target = sql_from_tables
            else:
                _logger.warning("Query type %s for query %s is not supported by _check_url_hot_query", query_type, query)
            log_target.setdefault(table, 0)
            log_target[table] = log_target[table] + 1
        if query_count != expected_query_count:
            msq = f"Expected {expected_query_count} queries but {query_count} where ran: {query_separator}{queries}{query_separator}"
            self.fail(msq)
        self.assertEqual(sql_from_tables, select_tables_perf or {}, f'Select queries does not match: {query_separator}{queries}{query_separator}')
        self.assertEqual(sql_into_tables, insert_tables_perf or {}, f'Insert queries does not match: {query_separator}{queries}{query_separator}')


class TestStandardPerformance(UtilPerf):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.env['res.users'].browse(2).image_1920 = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhVAAAAAElFTkSuQmCC'

    @mute_logger('odoo.http')
    def test_10_perf_sql_img_controller(self):
        self.authenticate('demo', 'demo')
        # not published user, get the not found image placeholder
        self.assertEqual(self.env['res.users'].sudo().browse(2).website_published, False)
        url = '/web/image/res.users/2/image_256'
        self.assertEqual(self._get_url_hot_query(url), 8)
        self.assertEqual(self._get_url_hot_query(url, cache=False), 8)

    @mute_logger('odoo.http')
    def test_11_perf_sql_img_controller(self):
        self.authenticate('demo', 'demo')
        self.env['res.users'].sudo().browse(2).website_published = True
        url = '/web/image/res.users/2/image_256'
        select_tables_perf = {
            'base_registry_signaling': 1,
            'res_users': 2,
            'res_partner': 1,
            'ir_attachment': 2,
        }
        self._check_url_hot_query(url, 6, select_tables_perf)
        self.assertEqual(self._get_url_hot_query(url, cache=False), 6)

    @mute_logger('odoo.http')
    def test_20_perf_sql_img_controller_bis(self):
        url = '/web/image/website/1/favicon'
        select_tables_perf = {
            'base_registry_signaling': 1,
            'website': 2,
            # 1. `_find_record()` performs an access right check through
            #    `exists()` which perform a request on the website.
            # 2. `_get_stream_from` ends up reading the requested record to
            #    give a name to the file (downloaded_name)
            'ir_attachment': 2,
            # 1. `_record_to_stream()` does a `search()`..
            # 2. ..followed by a `_read()`
        }
        self._check_url_hot_query(url, 5, select_tables_perf)
        self.assertEqual(self._get_url_hot_query(url, cache=False), 5)

        self.authenticate('portal', 'portal')
        self._check_url_hot_query(url, 5, select_tables_perf)
        self.assertEqual(self._get_url_hot_query(url, cache=False), 5)


class TestWebsitePerformanceCommon(UtilPerf):

    def setUp(self):
        super().setUp()
        self.page, self.menu = self._create_page_with_menu('/sql_page')

    def _create_page_with_menu(self, url):
        name = url[1:]
        website = self.env['website'].browse(1)
        page = self.env['website.page'].create({
            'url': url,
            'name': name,
            'type': 'qweb',
            'arch': '<t name="%s" t-name="website.page_test_%s"> \
                       <t t-call="website.layout"> \
                         <div id="wrap"><div class="oe_structure"/></div> \
                       </t> \
                     </t>' % (name, name),
            'key': 'website.page_test_%s' % name,
            'is_published': True,
            'website_id': website.id,
            'track': False,
        })
        menu = self.env['website.menu'].create({
            'name': name,
            'url': url,
            'page_id': page.id,
            'website_id': website.id,
            'parent_id': website.menu_id.id
        })
        return (page, menu)


class TestWebsitePerformance(TestWebsitePerformanceCommon):

    def test_10_perf_sql_queries_page(self):
        # standard untracked website.page
        for readonly_enabled in (True, False):
            self.env.registry.test_readonly_enabled = readonly_enabled
            with self.subTest(readonly_enabled=readonly_enabled), self.env.cr.savepoint() as savepoint:
                select_tables_perf = {
                    'base_registry_signaling': 1,
                    'ir_attachment': 1,
                    # `_get_serve_attachment` dispatcher fallback
                    'website_page': 2,
                    # 1. `_serve_page` search page matching URL..
                    # 2. ..then reads it (`is_visible`)
                    'website': 1,
                }
                expected_query_count = 5
                if not readonly_enabled:
                    select_tables_perf['ir_ui_view'] = 1 # Check if `view.track` to track visitor or not
                    expected_query_count += 1
                self._check_url_hot_query(self.page.url, expected_query_count, select_tables_perf)
                self.assertEqual(self._get_url_hot_query(self.page.url, cache=False), 10)
                self.menu.unlink()  # page being or not in menu shouldn't add queries
                self._check_url_hot_query(self.page.url, expected_query_count, select_tables_perf)
                self.assertEqual(self._get_url_hot_query(self.page.url, cache=False), 10)
                savepoint.rollback()

    def test_15_perf_sql_queries_page(self):
        # standard tracked website.page
        for readonly_enabled in (True, False):
            self.env.registry.test_readonly_enabled = readonly_enabled
            with self.subTest(readonly_enabled=readonly_enabled), self.env.cr.savepoint() as savepoint:
                select_tables_perf = {
                    'base_registry_signaling': 1,
                    'ir_attachment': 1,
                    # `_get_serve_attachment` dispatcher fallback
                    'website_page': 2,
                    # 1. `_serve_page` search page matching URL..
                    # 2. ..then reads it (`is_visible`)
                    'website': 1,
                }
                expected_query_count = 5
                expected_query_count_no_cache = 10
                insert_tables_perf = {}
                if not readonly_enabled:
                    select_tables_perf['ir_ui_view'] = 1 # Check if `view.track` to track visitor or not
                    insert_tables_perf = {
                        'website_visitor': 1,
                        # Visitor upsert
                    }
                    expected_query_count += 2
                    expected_query_count_no_cache += 1
                self.page.track = True
                self._check_url_hot_query(self.page.url, expected_query_count, select_tables_perf, insert_tables_perf)
                self.assertEqual(self._get_url_hot_query(self.page.url, cache=False), expected_query_count_no_cache)

                self.menu.unlink()  # page being or not in menu shouldn't add queries
                self._check_url_hot_query(self.page.url, expected_query_count, select_tables_perf, insert_tables_perf)
                self.assertEqual(self._get_url_hot_query(self.page.url, cache=False), expected_query_count_no_cache)
                savepoint.rollback()

    def test_20_perf_sql_queries_homepage(self):
        # homepage "/" has its own controller
        for readonly_enabled in (True, False):
            self.env.registry.test_readonly_enabled = readonly_enabled
            with self.subTest(readonly=readonly_enabled), self.env.cr.savepoint() as savepoint:
                select_tables_perf = {
                    'base_registry_signaling': 1,
                    'website_menu': 1,
                    # homepage controller is prefetching all menus for perf in one go
                    'website_page': 2,
                    # 1. the menu prefetching is also prefetching all menu's pages
                    # 2. find page matching the `/` url
                    'website': 1,
                }
                expected_query_count = 5
                expected_query_count_no_cache = 8
                insert_tables_perf = {}
                if not readonly_enabled:
                    select_tables_perf['ir_ui_view'] = 1 # Check if `view.track` to track visitor or not
                    insert_tables_perf = {
                        'website_visitor': 1,
                        # Visitor upsert
                    }
                    expected_query_count += 2
                    expected_query_count_no_cache += 1
                self._check_url_hot_query('/', expected_query_count, select_tables_perf, insert_tables_perf)
                self.assertEqual(self._get_url_hot_query('/', cache=False), expected_query_count_no_cache)
                savepoint.rollback()

    def test_30_perf_sql_queries_page_no_layout(self):
        # untrack website.page with no call to layout templates
        self.page.arch = '<div>I am a blank page</div>'
        select_tables_perf = {
            'base_registry_signaling': 1,
            'ir_attachment': 1,
            # `_get_serve_attachment` dispatcher fallback
            'website_page': 2,
            # 1. `_serve_page` search page matching URL..
            # 2. ..then reads it (`is_visible`)
            'website': 1,
            # Check if website.cookies_bar is active
            'ir_ui_view': 1,
            # Check if `view.track` to track visitor or not
        }
        self._check_url_hot_query(self.page.url, 6, select_tables_perf)
        self.assertEqual(self._get_url_hot_query(self.page.url, cache=False), 6)

    def test_40_perf_sql_queries_page_multi_level_menu(self):
        # menu structure should not impact SQL requests
        _, menu_a = self._create_page_with_menu('/a')
        _, menu_aa = self._create_page_with_menu('/aa')
        _, menu_b = self._create_page_with_menu('/b')
        _, menu_bb = self._create_page_with_menu('/bb')
        _, menu_bbb = self._create_page_with_menu('/bbb')
        _, menu_bbbb = self._create_page_with_menu('/bbbb')
        _, menu_bbbbb = self._create_page_with_menu('/bbbbb')
        self._create_page_with_menu('c')
        menu_bbbbb.parent_id = menu_bbbb
        menu_bbbb.parent_id = menu_bbb
        menu_bbb.parent_id = menu_bb
        menu_bb.parent_id = menu_b
        menu_aa.parent_id = menu_a

        select_tables_perf = {
            'base_registry_signaling': 1,
            'ir_attachment': 1,
            # `_get_serve_attachment` dispatcher fallback
            'website_page': 2,
            # 1. `_serve_page` search page matching URL..
            # 2. ..then reads it (`is_visible`)
            'website': 1,
            'ir_ui_view': 1,
            # Check if `view.track` to track visitor or not
        }
        self._check_url_hot_query(self.page.url, 6, select_tables_perf)
        self.assertEqual(self._get_url_hot_query(self.page.url, cache=False), 10)

@tagged('-at_install', 'post_install')
class TestWebsitePerformancePost(UtilPerf):
    @mute_logger('odoo.http')
    def test_50_perf_sql_web_assets(self):
        # assets route /web/assets/..
        assets_url = self.env['ir.qweb']._get_asset_bundle('web.assets_frontend_lazy', css=False, js=True).get_links()[0]
        self.assertIn('web.assets_frontend_lazy.min.js', assets_url)
        select_tables_perf = {
            'base_registry_signaling': 1,
            'ir_attachment': 2,
            # All 2 coming from the /web/assets and ir.binary stack
            # 1. `search() the attachment`
            # 2. `_record_to_stream` reads the other attachment fields
        }
        self._check_url_hot_query(assets_url, 3, select_tables_perf)
        self.assertEqual(self._get_url_hot_query(assets_url, cache=False), 3)