Odoo12中性能检测装饰器@profile用法示例

Odoo12中性能检测装饰器@profile用法示例

Odoo12中源码关于profile.py的代码如下:
注: 源代码位置在…\odoo\odoo\odoo\tools\profiler.py ,这里不对不对源码进行展示,这里仅对方法介绍及参数部分进行解释

@profile装饰器的参数如下:

	def profile(method=None, whitelist=None, blacklist=(None,), files=None,
        minimum_time=0, minimum_queries=0):
    	"""
        Decorate an entry point method.  装饰入口方法
        If profile is used without params, log as shallow mode else, log
        all methods for all odoo models by applying the optional filters.
        如果profile没有参数,则以shallow mode方式记录,可通过其他可选的过滤器参数来记录odoo模型的其他方法。

        :param whitelist: None or list of model names to display in the log
                        (Default: None)  日志中显示的模型名称列表(默认:None)
        :type whitelist: list or None

        :param files: None or list of filenames to display in the log
                        (Default: None) 用于记录日志的文件名(默认:None)
        :type files: list or None

        :param list blacklist: list model names to remove from the log 不显示日志文件的模型
                        (Default: remove non odoo model from the log: [None])
        :param int minimum_time: minimum time (ms) to display a method  显示方法的最小时间(ms)
                        (Default: 0)
        :param int minimum_queries: minimum sql queries to display a method  显示方法的最少sql查询
                        (Default: 0)
        
        .. code-block:: python   用法示例,先引入profile,然后在方法上添加@profile装饰器

          from odoo.tools.profiler import profile

          class SaleOrder(models.Model):
            ...

            @api.model
            @profile                    # log only this create method
            def create(self, vals):
            ...
            @api.multi
            @profile()                  # log all methods for all odoo models
            def unlink(self):
            ...
            @profile(whitelist=['sale.order', 'ir.model.data'])
            def action_quotation_send(self):
            ...
            @profile(files=['/home/openerp/odoo/odoo/addons/sale/models/sale.py'])
            def write(self):
            ...

        NB: The use of the profiler modifies the execution time
    """

@profile用法示例如下:

	# 引入profile
	from odoo.tools.profiler import profile
		
	# 其他逻辑代码
	... 

	# test_profile 方法添加@profile
	@profile
	@api.model
	def test_profile(self):
	    week_str = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] * 10000
	    i = 0
	    for x in week_str:
	        if x == 'Monday':
	            i += 1
	    print(i)

上边 test_profile 为测试方法,可以看到 @profile 用法很简单:

  1. 引入profile: from odoo.tools.profiler import profile;
  2. 在对应方法上添加 @profile 即可。
控制台打印结果如下:
  • calls :调用次数
  • queries :查询次数
  • ms :耗费时间
calls     queries   ms
dy.stock.period --------------------- /opt/odoo/hand-china/hrp/dy-scm/dy_stock/models/inventory_snapshots.py, 35

1         0         0.02          @profile
                                  @api.model
                                  def test_profile(self):
1         0         0.22              week_str = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] * 10000
1         0         0.01              i = 0
70001     0         899.54            for x in week_str:
70000     0         897.25                if x == 'Monday':
10000     0         134.51                    i += 1
1         0         0.32              print(i)

Total:
1         0         1931.87


结尾语:Odoo12中的@profile装饰器很不错,可以在代码完成之后,对关键逻辑代码进行一次检测,前期尽量避免一些性能效率问题,值得推荐。

猜你喜欢

转载自blog.csdn.net/sinat_23931991/article/details/104052470