Back to index

lightning-sunbird  0.9+nobinonly
tracer_demo.py
Go to the documentation of this file.
00001 # ***** BEGIN LICENSE BLOCK *****
00002 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003 #
00004 # The contents of this file are subject to the Mozilla Public License Version
00005 # 1.1 (the "License"); you may not use this file except in compliance with
00006 # the License. You may obtain a copy of the License at
00007 # http://www.mozilla.org/MPL/
00008 #
00009 # Software distributed under the License is distributed on an "AS IS" basis,
00010 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011 # for the specific language governing rights and limitations under the
00012 # License.
00013 #
00014 # The Original Code is Python XPCOM language bindings.
00015 #
00016 # The Initial Developer of the Original Code is
00017 # ActiveState Tool Corp.
00018 # Portions created by the Initial Developer are Copyright (C) 2000, 2001
00019 # the Initial Developer. All Rights Reserved.
00020 #
00021 # Contributor(s):
00022 #  Mark Hammond <mhammond@skippinet.com.au> (original author)
00023 #
00024 # Alternatively, the contents of this file may be used under the terms of
00025 # either the GNU General Public License Version 2 or later (the "GPL"), or
00026 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027 # in which case the provisions of the GPL or the LGPL are applicable instead
00028 # of those above. If you wish to allow use of your version of this file only
00029 # under the terms of either the GPL or the LGPL, and not to allow others to
00030 # use your version of this file under the terms of the MPL, indicate your
00031 # decision by deleting the provisions above and replace them with the notice
00032 # and other provisions required by the GPL or the LGPL. If you do not delete
00033 # the provisions above, a recipient may use your version of this file under
00034 # the terms of any one of the MPL, the GPL or the LGPL.
00035 #
00036 # ***** END LICENSE BLOCK *****
00037 
00038 # This is a demo is how to use the xpcom.server "tracer" facility.
00039 #
00040 # This demo installs a tracer that uses the Python profiler.  It then
00041 # creates the Python test component, and references some methods
00042 # and properties.  It then dumps the profile statistics.
00043 
00044 # This same technique could also be used for debugging, for example.
00045 
00046 import profile
00047 
00048 p = profile.Profile()
00049 getters = {}
00050 setters = {}
00051 
00052 # A wrapper around a function - looks like a function,
00053 # but actually profiles the delegate.
00054 class TracerDelegate:
00055     def __init__(self, callme):
00056         self.callme = callme
00057     def __call__(self, *args):
00058         return p.runcall(self.callme, *args)
00059 
00060 # A wrapper around each of our XPCOM objects.  All PyXPCOM calls
00061 # in are made on this object, which creates a TracerDelagate around
00062 # every function.  As the function is called, it collects profile info.
00063 class Tracer:
00064     def __init__(self, ob):
00065         self.__dict__['_ob'] = ob
00066     def __repr__(self):
00067         return "<Tracer around %r>" % (self._ob,)
00068     def __str__(self):
00069         return "<Tracer around %r>" % (self._ob,)
00070     def __getattr__(self, attr):
00071         ret = getattr(self._ob, attr) # Attribute error just goes up
00072         if callable(ret):
00073             return TracerDelegate(ret)
00074         else:
00075             if not attr.startswith("_com_") and not attr.startswith("_reg_"):
00076                 getters[attr] = getters.setdefault(attr,0) + 1
00077             return ret
00078     def __setattr__(self, attr, val):
00079         if self.__dict__.has_key(attr):
00080             self.__dict__[attr] = val
00081             return
00082         setters[attr] = setters.setdefault(attr,0) + 1
00083         setattr(self._ob, attr, val)
00084 
00085 # Installed as a global XPCOM function that if exists, will be called
00086 # to wrap each XPCOM object created.
00087 def MakeTracer(ob):
00088     # In some cases we may be asked to wrap ourself, so handle that.
00089     if isinstance(ob, Tracer):
00090         return ob
00091     return Tracer(ob)
00092 
00093 def test():
00094     import xpcom.server, xpcom.components
00095     xpcom.server.tracer = MakeTracer
00096     contractid = "Python.TestComponent"
00097     for i in range(100):
00098         c = xpcom.components.classes[contractid].createInstance().queryInterface(xpcom.components.interfaces.nsIPythonTestInterface)
00099         c.boolean_value = 0
00100         a = c.boolean_value
00101         c.do_boolean(0,1)
00102     print "Finshed"
00103     p.print_stats()
00104     print "%-30s%s" % ("Attribute Gets", "Number")
00105     print "-" * 36
00106     for name, num in getters.items():
00107         print "%-30s%d" % (name, num)
00108     print "%-30s%s" % ("Attribute Sets", "Number")
00109     print "-" * 36
00110     for name, num in setters.items():
00111         print "%-30s%d" % (name, num)
00112 
00113 test()