-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement profiler #3529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
wouitmil
wants to merge
8
commits into
OCA:18.0
Choose a base branch
from
camptocamp:18-implement-profiler
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,450
−0
Draft
Implement profiler #3529
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6191458
Init profiler
fb76409
Added callgrind downloadable file
b98b7c2
Cleanup code
d327a7a
Init readme
5c58e85
Move html ton views
6d63994
Reorder views in manifest
ba7a33f
Tidy
cec7ae9
implement first tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| ===================================== | ||
| Queue Job and Thread Profiler (Yappi) | ||
| ===================================== | ||
|
|
||
| .. | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! This file is generated by oca-gen-addon-readme !! | ||
| !! changes will be overwritten. !! | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! source digest: sha256:f7f74561a87f9017647be70515bfbb630fb4e6c0cd85fbaaad907046e76d3bcc | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
|
||
| .. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png | ||
| :target: https://odoo-community.org/page/development-status | ||
| :alt: Alpha | ||
| .. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png | ||
| :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
| :alt: License: AGPL-3 | ||
| .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github | ||
| :target: https://github.com/OCA/server-tools/tree/18.0/profiler | ||
| :alt: OCA/server-tools | ||
| .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png | ||
| :target: https://translation.odoo-community.org/projects/server-tools-18-0/server-tools-18-0-profiler | ||
| :alt: Translate me on Weblate | ||
| .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png | ||
| :target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=18.0 | ||
| :alt: Try me on Runboat | ||
|
|
||
| |badge1| |badge2| |badge3| |badge4| |badge5| | ||
|
|
||
| This module aims to help profle code executed in Odoo, and to analyze | ||
| the results of the profiling. It is based on yappi profiler, which is a | ||
| Python profiler that supports multi-threading and multi-processing. It's | ||
| main use case is to profile function executed in queue jobs or executed | ||
| a large amount of times in a short time, which is not possible with the | ||
| default cProfile profiler used in Odoo. It also allows to store the | ||
| results of the profiling in the database, and to analyze them in Odoo or | ||
| with external tools like snakeviz or flameprof. | ||
|
|
||
| .. IMPORTANT:: | ||
| This is an alpha version, the data model and design can change at any time without warning. | ||
| Only for development or testing purpose, do not use in production. | ||
| `More details on development status <https://odoo-community.org/page/development-status>`_ | ||
|
|
||
| **Table of contents** | ||
|
|
||
| .. contents:: | ||
| :local: | ||
|
|
||
| Usage | ||
| ===== | ||
|
|
||
| To use this module, you need to: | ||
|
|
||
| - got to General Settings > Proflier > profiled functions and create a | ||
| new record with the name of the function you want to profile, and the | ||
| model if it's a method of a model. For example, if you want to profile | ||
| the method ``my_method`` of the model ``my.model``, you need to create | ||
| a record with the name ``my_method`` and the model | ||
| ``my.model.my_method``. EG: | ||
| - name: Stock Rule | ||
| - Python Path: | ||
| odoo.addons.stock.models.stock_rule.ProcurementGroup.run_scheduler | ||
| - Sample rate (from 0 to 1): 0.1 (to profile 10% of the calls to this | ||
| method and avoid too much overhead) | ||
| - Active if you want it to be active. | ||
|
|
||
| Known issues / Roadmap | ||
| ====================== | ||
|
|
||
|
|
||
|
|
||
| Bug Tracker | ||
| =========== | ||
|
|
||
| Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_. | ||
| In case of trouble, please check there if your issue has already been reported. | ||
| If you spotted it first, help us to smash it by providing a detailed and welcomed | ||
| `feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20profiler%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
|
||
| Do not contact contributors directly about support or help with technical issues. | ||
|
|
||
| Credits | ||
| ======= | ||
|
|
||
| Authors | ||
| ------- | ||
|
|
||
| * Camptocamp | ||
|
|
||
| Contributors | ||
| ------------ | ||
|
|
||
| - Hadrien Huvelle <hadrien.huvelle@camptocamp.com> | ||
|
|
||
| Other credits | ||
| ------------- | ||
|
|
||
| The implementation of this module was financially supported by | ||
| Camptocamp. | ||
|
|
||
| Maintainers | ||
| ----------- | ||
|
|
||
| This module is maintained by the OCA. | ||
|
|
||
| .. image:: https://odoo-community.org/logo.png | ||
| :alt: Odoo Community Association | ||
| :target: https://odoo-community.org | ||
|
|
||
| OCA, or the Odoo Community Association, is a nonprofit organization whose | ||
| mission is to support the collaborative development of Odoo features and | ||
| promote its widespread use. | ||
|
|
||
| This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/18.0/profiler>`_ project on GitHub. | ||
|
|
||
| You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
|
||
| from . import models | ||
| from . import tools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
|
||
| { | ||
| "name": "Queue Job and Thread Profiler (Yappi)", | ||
| "version": "18.0.1.0.0", | ||
| "category": "Tools", | ||
| "summary": "yappi profiler decorator with database storage", | ||
| "author": "Camptocamp, Odoo Community Association (OCA)", | ||
| "license": "AGPL-3", | ||
| "website": "https://github.com/OCA/server-tools", | ||
| "depends": ["base"], | ||
| "external_dependencies": { | ||
| "python": ["yappi", "cairosvg", "flameprof"], | ||
| }, | ||
| "data": [ | ||
| "security/ir.model.access.csv", | ||
| "views/profiler_function_views.xml", | ||
| "views/profiler_report_views.xml", | ||
| "views/profiler_result_views.xml", | ||
| ], | ||
| "installable": True, | ||
| "development_status": "Alpha", | ||
| } | ||
wouitmil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
|
||
| from . import profiler_result | ||
| from . import profiler_report | ||
| from . import profiler_function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
|
||
| import logging | ||
|
|
||
| from odoo import api, fields, models | ||
| from odoo.exceptions import ValidationError | ||
|
|
||
| from ..tools import dynamic_profile | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ProfilerFunction(models.Model): | ||
| _name = "profiler.function" | ||
| _description = "Profiled Function" | ||
| _order = "name" | ||
|
|
||
| name = fields.Char(required=True) | ||
| python_path = fields.Char( | ||
| required=True, | ||
| index=True, | ||
| help="Use module.function or module.Class.method", | ||
| ) | ||
| sample_rate = fields.Float( | ||
| default=0.05, | ||
| help="Percentage of calls to profile (0.0 - 1.0)", | ||
| ) | ||
| active = fields.Boolean(default=True) | ||
|
|
||
| @api.constrains("python_path") | ||
| def _check_python_path(self): | ||
| for record in self: | ||
| if not record.python_path: | ||
| continue | ||
| try: | ||
| dynamic_profile.validate_path(record.python_path) | ||
| except Exception as exc: | ||
| raise ValidationError( | ||
| f"Invalid python path {record.python_path}: {exc}" | ||
| ) from exc | ||
|
|
||
| @api.model_create_multi | ||
| def create(self, vals_list): | ||
| records = super().create(vals_list) | ||
| records._update_registry() | ||
| return records | ||
|
|
||
| def write(self, vals): | ||
| res = super().write(vals) | ||
| if {"python_path", "sample_rate", "active"} & set(vals): | ||
| self._update_registry() | ||
| return res | ||
|
|
||
| def unlink(self): | ||
| res = super().unlink() | ||
| self._update_registry() | ||
| return res | ||
|
|
||
| def _update_registry(self): | ||
| if self.env.registry.ready: | ||
| self._unregister_hook() | ||
| self._register_hook() | ||
| self.env.registry.registry_invalidated = True | ||
| else: | ||
| _logger.info("Registry not ready, skipping profiler patch update") | ||
|
|
||
| def _register_hook(self): | ||
| res = super()._register_hook() | ||
| active_records = self.search([("active", "=", True)]) | ||
| dynamic_profile.patch_active_records(active_records) | ||
| return res | ||
|
|
||
| def _unregister_hook(self): | ||
| res = super()._unregister_hook() | ||
| for record in self.with_context(active_test=False).search([]): | ||
| try: | ||
| dynamic_profile.unpatch_path(record.python_path) | ||
| except Exception as exc: | ||
| _logger.warning( | ||
| "Unable to remove profile patch for %s: %s", | ||
| record.python_path, | ||
| exc, | ||
| ) | ||
| return res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright 2026 Camptocamp SA | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
|
||
| from odoo import api, fields, models | ||
|
|
||
|
|
||
| class ProfilerReport(models.TransientModel): | ||
| _name = "profiler.report" | ||
| _description = "Profiler Statistics Report" | ||
|
|
||
| name = fields.Char(string="Function Name") | ||
| p95 = fields.Float(string="P95 Duration (s)", digits=(12, 6)) | ||
| p99 = fields.Float(string="P99 Duration (s)", digits=(12, 6)) | ||
| count = fields.Integer(string="Call Count") | ||
|
|
||
| def _parse_time_delta(self, time_delta_str): | ||
| """Parse time delta string in format HH:MM:SS and return total seconds.""" | ||
| try: | ||
| parts = time_delta_str.split(":") | ||
| if len(parts) != 3: | ||
| raise ValueError("Invalid format") | ||
| hours, minutes, seconds = map(int, parts) | ||
| return hours * 3600 + minutes * 60 + seconds | ||
| except Exception: | ||
| # Default to 24 hours if parsing fails | ||
| return 86400 | ||
|
|
||
| @api.model | ||
| def generate_report(self): | ||
| """Generate the profiler statistics report.""" | ||
| self.search([]).unlink() | ||
|
|
||
| # Get time_delta from context (format: "HH:MM:SS") | ||
| time_delta_str = self.env.context.get("time_delta", "24:00:00") | ||
| total_seconds = self._parse_time_delta(time_delta_str) | ||
|
|
||
| query = """ | ||
| SELECT name, | ||
| percentile_disc(0.95) WITHIN GROUP (ORDER BY duration) AS p95, | ||
| percentile_disc(0.99) WITHIN GROUP (ORDER BY duration) AS p99, | ||
| count(*) AS count | ||
| FROM profiler_result | ||
| WHERE create_date > now() - interval '%s second' | ||
| GROUP BY name | ||
| ORDER BY p99 DESC | ||
| LIMIT 20 | ||
| """ | ||
|
|
||
| self.env.cr.execute(query, (total_seconds,)) | ||
| results = self.env.cr.fetchall() | ||
|
|
||
| report_records = [] | ||
| for row in results: | ||
| report_records.append( | ||
| { | ||
| "name": row[0], | ||
| "p95": row[1], | ||
| "p99": row[2], | ||
| "count": row[3], | ||
| } | ||
| ) | ||
|
|
||
| if report_records: | ||
| self.create(report_records) | ||
|
|
||
| return { | ||
| "type": "ir.actions.act_window", | ||
| "name": "Profiler Statistics Report", | ||
| "res_model": "profiler.report", | ||
| "view_mode": "list", | ||
| "target": "current", | ||
| "context": { | ||
| "create": False, | ||
| "edit": False, | ||
| "delete": False, | ||
| "time_delta": time_delta_str, | ||
| }, | ||
| } | ||
|
|
||
| def action_refresh(self): | ||
| """Refresh the report with current data.""" | ||
| return self.generate_report() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.