Profiling stuff that may be useful for the devs
Posted: 2007-02-12 01:25
Hey guys, here's a quick scope timer implementation I put together that might be useful for tracking down places that are causing lag:
profiling.py:
example.py
example output:
As you can see, this gives you profiling information in decreasing order by time spent in that scope. This is usually a nice way to attack this problem because it's at easy as adding one of those ScopeTimer objects to whatever scope you want to time. When it goes out of scope, it logs how much time it spent in that scope into the given Profiler. I'll probably be trying to figure out a way to guess a scope as an improvement, but that will likely only work at the function level if I can even find a way to do it.
I have some other debugging stuff that I've found useful for working on a TK punishment system that I'll be posting soon, but thought you might find a use for this stuff right now with the lag issues going on with .5.
Let me know if you have any questions or need any additional features, I'd love to help out any way I can.
profiling.py:
Code: Select all
################################################
## Andrew Burke ([email protected])
import sys, traceback, time
def timeSort( lhs, rhs ):
return int( lhs[ 'totalTime' ] - rhs[ 'totalTime' ] )
class Profiler:
def __init__( self ):
self.records = {}
def AddRecord( self, scopeName, runTime ):
if ( scopeName not in self.records ):
self.records[ scopeName ] = []
self.records[ scopeName ].append( runTime )
def PrintReport( self ):
scopeInfo = []
for scopeName in self.records:
totalRuns = 0
totalTime = 0
for runTime in self.records[ scopeName ]:
totalTime += runTime
totalRuns += 1
averageTime = totalTime / totalRuns
scopeInfo.append( { 'scopeName' : scopeName, 'totalRuns' : totalRuns, 'totalTime' : totalTime, 'averageTime' : averageTime } )
scopeInfo.sort( timeSort )
scopeInfo.reverse()
print "[ %10s ] [ %10s ] [ %6s ] [ Scope ]" % ( 'Total', 'Avg', 'Runs' )
for info in scopeInfo:
print "[ %0.8f ] [ %0.8f ] [ %06d ] [ %s ]" % ( info[ 'totalTime' ], info[ 'averageTime' ], info[ 'totalRuns' ], info[ 'scopeName' ] )
class ScopeTimer:
def __init__( self, profiler, scopeName = 'Unknown' ):
self.scopeName = str( scopeName )
self.profiler = profiler
self.startTime = time.clock()
def __del__( self ):
timeDelta = time.clock() - self.startTime
self.profiler.AddRecord( self.scopeName, timeDelta )
Code: Select all
from profiling import Profiler, ScopeTimer
import time
p = Profiler()
def test1():
s = ScopeTimer( p, 'test1' )
time.sleep( 1 )
def test2():
s = ScopeTimer( p, 'test2' )
time.sleep( 3 )
test1()
test1()
test2()
test2()
test1()
test1()
test2()
p.PrintReport()
Code: Select all
[ Total ] [ Avg ] [ Runs ] [ Scope ]
[ 9.00093084 ] [ 3.00031028 ] [ 000003 ] [ test2 ]
[ 3.99950524 ] [ 0.99987631 ] [ 000004 ] [ test1 ]
I have some other debugging stuff that I've found useful for working on a TK punishment system that I'll be posting soon, but thought you might find a use for this stuff right now with the lag issues going on with .5.
Let me know if you have any questions or need any additional features, I'd love to help out any way I can.