You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

  1. import logging
  2. from openerp.http import WebRequest
  3. from openerp.sql_db import Cursor
  4. from .models.profiler_profile import ProfilerProfile
  5. _logger = logging.getLogger(__name__)
  6. def patch_web_request_call_function():
  7. """Modify Odoo entry points so that profile can record.
  8. Odoo is a multi-threaded program. Therefore, the :data:`profile` object
  9. needs to be enabled/disabled each in each thread to capture all the
  10. execution.
  11. For instance, Odoo spawns a new thread for each request.
  12. """
  13. _logger.info('Patching http.WebRequest._call_function')
  14. webreq_f_origin = WebRequest._call_function
  15. def webreq_f(*args, **kwargs):
  16. with ProfilerProfile.profiling():
  17. return webreq_f_origin(*args, **kwargs)
  18. WebRequest._call_function = webreq_f
  19. def patch_cursor_init():
  20. _logger.info('Patching sql_dp.Cursor.__init__')
  21. cursor_f_origin = Cursor.__init__
  22. def init_f(self, *args, **kwargs):
  23. cursor_f_origin(self, *args, **kwargs)
  24. enable = ProfilerProfile.activate_deactivate_pglogs
  25. if enable is not None:
  26. self._obj.execute('SET log_min_duration_statement TO "%s"' %
  27. ((not enable) * -1,))
  28. Cursor.__init__ = init_f
  29. def post_load():
  30. patch_web_request_call_function()
  31. patch_cursor_init()