Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions profiler/README.rst
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.
5 changes: 5 additions & 0 deletions profiler/__init__.py
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
24 changes: 24 additions & 0 deletions profiler/__manifest__.py
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",
}
6 changes: 6 additions & 0 deletions profiler/models/__init__.py
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
85 changes: 85 additions & 0 deletions profiler/models/profiler_function.py
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
82 changes: 82 additions & 0 deletions profiler/models/profiler_report.py
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()
Loading
Loading