Odoo Profiler Menggunakan cProfile

Proses profiling python Odoo menggunakan cProfile

Share

OpenSynergy Indonesia, Michael Viriyananda

Odoo Profiler Menggunakan cProfile

Proses profiling python Odoo menggunakan cProfile

Berikut adalah contoh kode python untuk melakukan profiling python di Odoo menggunakan cProfile.:

import cProfile, StringIO, pstats

pr = cProfile.Profile()
pr.enable()

<<< PYTHON CODE HERE >>>

pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_callees()
print s.getvalue()

Berikut adalah contoh profiling python di Odoo menggunakan cProfile yang digabungkan dengan Odoo Eksternal Script.:

import openerp
import cProfile, StringIO, pstats

pr = cProfile.Profile()

DB_NAME="odoo-development"
ODOO_CONF="/home/michael/.8_0"
UID=openerp.SUPERUSER_ID

openerp.tools.config.parse_config(["--config=%s" % ODOO_CONF])
with openerp.api.Environment.manage():
    registry = openerp.modules.registry.RegistryManager.get(DB_NAME)
    with registry.cursor() as cr:
        ctx = openerp.api.Environment(cr, UID, {})["res.users"].context_get()
        env = openerp.api.Environment(cr, UID, ctx)
        pr.enable()
        env['res.partner'].create({'name': 'Test Create Partner'})
        pr.disable()
        s = StringIO.StringIO()
        sortby = 'cumulative'
        ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
        ps.print_callees()
        print s.getvalue()