Browse Source

better profiled function, with capabilities for production

Zachary Pavlov 18 years ago
parent
commit
a2a8f7297f
1 changed files with 36 additions and 1 deletions
  1. 36 1
      direct/src/showbase/PythonUtil.py

+ 36 - 1
direct/src/showbase/PythonUtil.py

@@ -43,7 +43,7 @@ import new
 import gc
 import gc
 #if __debug__:
 #if __debug__:
 import traceback
 import traceback
-
+import hotshot, hotshot.stats
 
 
 
 
 from direct.directutil import Verify
 from direct.directutil import Verify
@@ -2778,6 +2778,7 @@ def recordCreationStack(cls):
     cls.getCreationStackTrace = getCreationStackTrace
     cls.getCreationStackTrace = getCreationStackTrace
     return cls
     return cls
 
 
+
 # class 'decorator' that logs all method calls for a particular class
 # class 'decorator' that logs all method calls for a particular class
 def logMethodCalls(cls):
 def logMethodCalls(cls):
     if not hasattr(cls, 'notify'):
     if not hasattr(cls, 'notify'):
@@ -2954,6 +2955,38 @@ if __debug__:
     assert obj2count[3] == 3 * 3
     assert obj2count[3] == 3 * 3
     assert obj2count[4] == 4 * 3
     assert obj2count[4] == 4 * 3
 
 
+def quickProfile(name="unnamed"):
+    def profileDecorator(f):
+        if(not base.config.GetBool("use-profiler",0)):
+            return f
+        def _profiled(*args, **kArgs):
+            #import pdb;pdb.set_trace()
+            # must do this in here because we don't have base/simbase
+            # at the time that PythonUtil is loaded
+            if(not base.config.GetBool("profile-debug",0)):
+                #dumb timings
+                st=time.time()
+                f(*args,**kArgs)
+                s=time.time()-st
+                print "Function %s.%s took %s seconds"%(f.__module__, f.__name__,s)
+            else:
+                #detailed profile, stored in base.stats under (
+                if(not hasattr(base,"stats")):
+                    base.stats={}
+                if(not base.stats.get(name)):
+                    base.stats[name]=[]
+
+                p=hotshot.Profile("t.prof")
+                p.runctx('f(*args, **kArgs)', {'f':f,'args':args,'kArgs':kArgs},None)
+                s = hotshot.stats.load('t.prof')
+                s.strip_dirs()
+                s.sort_stats("cumulative")
+                base.stats[name].append(s)
+
+        _profiled.__doc__ = f.__doc__
+        return _profiled
+    return profileDecorator
+
 def choice(condition, ifTrue, ifFalse):
 def choice(condition, ifTrue, ifFalse):
     # equivalent of C++ (condition ? ifTrue : ifFalse)
     # equivalent of C++ (condition ? ifTrue : ifFalse)
     if condition:
     if condition:
@@ -2961,6 +2994,8 @@ def choice(condition, ifTrue, ifFalse):
     else:
     else:
         return ifFalse
         return ifFalse
 
 
+
+
 import __builtin__
 import __builtin__
 __builtin__.Functor = Functor
 __builtin__.Functor = Functor
 __builtin__.Stack = Stack
 __builtin__.Stack = Stack