16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# ***** BEGIN LICENSE BLOCK *****
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Version: MPL 1.1/GPL 2.0/LGPL 2.1
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# The contents of this file are subject to the Mozilla Public License Version
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# 1.1 (the "License"); you may not use this file except in compliance with
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the License. You may obtain a copy of the License at
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# http://www.mozilla.org/MPL/
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Software distributed under the License is distributed on an "AS IS" basis,
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# for the specific language governing rights and limitations under the
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# License.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# The Original Code is Python XPCOM language bindings.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# The Initial Developer of the Original Code is
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# ActiveState Tool Corp.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Portions created by the Initial Developer are Copyright (C) 2000, 2001
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the Initial Developer. All Rights Reserved.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Contributor(s):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Mark Hammond <mhammond@skippinet.com.au> (original author)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Alternatively, the contents of this file may be used under the terms of
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# either the GNU General Public License Version 2 or later (the "GPL"), or
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# in which case the provisions of the GPL or the LGPL are applicable instead
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# of those above. If you wish to allow use of your version of this file only
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# under the terms of either the GPL or the LGPL, and not to allow others to
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# use your version of this file under the terms of the MPL, indicate your
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# decision by deleting the provisions above and replace them with the notice
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# and other provisions required by the GPL or the LGPL. If you do not delete
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the provisions above, a recipient may use your version of this file under
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# the terms of any one of the MPL, the GPL or the LGPL.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# ***** END LICENSE BLOCK *****
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# This is a demo is how to use the xpcom.server "tracer" facility.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync#
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# This demo installs a tracer that uses the Python profiler. It then
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# creates the Python test component, and references some methods
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# and properties. It then dumps the profile statistics.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# This same technique could also be used for debugging, for example.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncimport profile
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncp = profile.Profile()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncgetters = {}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncsetters = {}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# A wrapper around a function - looks like a function,
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# but actually profiles the delegate.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncclass TracerDelegate:
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def __init__(self, callme):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.callme = callme
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def __call__(self, *args):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return p.runcall(self.callme, *args)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# A wrapper around each of our XPCOM objects. All PyXPCOM calls
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# in are made on this object, which creates a TracerDelagate around
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# every function. As the function is called, it collects profile info.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncclass Tracer:
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def __init__(self, ob):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.__dict__['_ob'] = ob
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def __repr__(self):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return "<Tracer around %r>" % (self._ob,)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def __str__(self):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return "<Tracer around %r>" % (self._ob,)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def __getattr__(self, attr):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync ret = getattr(self._ob, attr) # Attribute error just goes up
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync if callable(ret):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return TracerDelegate(ret)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync else:
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync if not attr.startswith("_com_") and not attr.startswith("_reg_"):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync getters[attr] = getters.setdefault(attr,0) + 1
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return ret
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync def __setattr__(self, attr, val):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync if self.__dict__.has_key(attr):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync self.__dict__[attr] = val
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync setters[attr] = setters.setdefault(attr,0) + 1
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync setattr(self._ob, attr, val)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# Installed as a global XPCOM function that if exists, will be called
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync# to wrap each XPCOM object created.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncdef MakeTracer(ob):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync # In some cases we may be asked to wrap ourself, so handle that.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync if isinstance(ob, Tracer):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return ob
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync return Tracer(ob)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncdef test():
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync import xpcom.server, xpcom.components
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync xpcom.server.tracer = MakeTracer
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync contractid = "Python.TestComponent"
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync for i in range(100):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync c = xpcom.components.classes[contractid].createInstance().queryInterface(xpcom.components.interfaces.nsIPythonTestInterface)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync c.boolean_value = 0
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync a = c.boolean_value
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync c.do_boolean(0,1)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync print "Finshed"
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync p.print_stats()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync print "%-30s%s" % ("Attribute Gets", "Number")
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync print "-" * 36
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync for name, num in getters.items():
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync print "%-30s%d" % (name, num)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync print "%-30s%s" % ("Attribute Sets", "Number")
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync print "-" * 36
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync for name, num in setters.items():
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync print "%-30s%d" % (name, num)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsynctest()