Odoo Profiler Menggunakan Trace

Proses profiling python Odoo menggunakan Trace

Share

OpenSynergy Indonesia, Michael Viriyananda

Odoo Profiler Menggunakan Trace

Proses profiling python Odoo menggunakan Trace

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

import trace
import linecache

class Trace(trace.Trace):
    def globaltrace_lt(self, frame, why, arg):
        """Handler for call events.

        If the code block being entered is to be ignored, returns `None',
        else returns self.localtrace.
        """
        if why == 'call':
            code = frame.f_code
            filename = frame.f_globals.get('__file__', None)
            if filename:
                # XXX modname() doesn't work right for packages, so
                # the ignore support won't work right for packages
                modulename = trace.modname(filename)
                if modulename is not None:
                    ignore_it = self.ignore.names(filename, modulename)
                    if not ignore_it:
                        if self.trace:
                            print (" --- modulename: %s, funcname: %s, filename: %s"
                                   % (modulename, code.co_name, filename))
                        return self.localtrace
            else:
                return None

    def localtrace_trace(self, frame, why, arg):
        if why == "line":
            # record the file name and line number of every trace
            filename = frame.f_code.co_filename
            lineno = frame.f_lineno

            if self.start_time:
                print '%.2f' % (time.time() - self.start_time),
            print "%s (%d): %s" % (filename, lineno,
                                  linecache.getline(filename, lineno)),
        return self.localtrace

commands = "<<< PYTHON CODE HERE >>>"
tracer = Trace(count=False, trace=True,ignoremods=["api"],ignoredirs=["/usr/lib/python2.7","/home/michael/odoo8"])
tracer.run(commands)
tracer.results().write_results(show_missing=True)

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

import sys
sys.path.append("/home/michael/odoo8/odoo")
import openerp
import linecache
import trace

class Trace(trace.Trace):
    def globaltrace_lt(self, frame, why, arg):
        """Handler for call events.

        If the code block being entered is to be ignored, returns `None',
        else returns self.localtrace.
        """
        if why == 'call':
            code = frame.f_code
            filename = frame.f_globals.get('__file__', None)
            if filename:
                # XXX modname() doesn't work right for packages, so
                # the ignore support won't work right for packages
                modulename = trace.modname(filename)
                if modulename is not None:
                    ignore_it = self.ignore.names(filename, modulename)
                    if not ignore_it:
                        if self.trace:
                            print (" --- modulename: %s, funcname: %s, filename: %s"
                                   % (modulename, code.co_name, filename))
                        return self.localtrace
            else:
                return None

    def localtrace_trace(self, frame, why, arg):
        if why == "line":
            # record the file name and line number of every trace
            filename = frame.f_code.co_filename
            lineno = frame.f_lineno

            if self.start_time:
                print '%.2f' % (time.time() - self.start_time),
            print "%s (%d): %s" % (filename, lineno,
                                  linecache.getline(filename, lineno)),
        return self.localtrace
 
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)
        commands = "env['res.partner'].create({'name': 'Test Create Partner'})"
        tracer = Trace(count=False, trace=True,ignoremods=["api"],ignoredirs=["/usr/lib/python2.7","/home/michael/odoo8"])
        tracer.run(commands)
        tracer.results().write_results(show_missing=True)