/usr/share/pyshared/gluon/contrib/timecollect.py is in python-gluon 1.99.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | # Only Python 2.6 and up, because of NamedTuple.
import time
from collections import namedtuple
Score = namedtuple('Score', ['tag', 'stamp'])
class TimeCollector(object):
def __init__(self):
'''The first time stamp is created here'''
self.scores = [Score(tag='start',stamp=time.clock())]
def addStamp(self, description):
'''Adds a new time stamp, with a description.'''
self.scores.append(Score(tag=description, stamp=time.clock()))
def _stampDelta(self, index1, index2):
'''Private utility function to clean up this common calculation.'''
return self.scores[index1].stamp - self.scores[index2].stamp
def getReportItems(self, orderByCost=True):
'''Returns a list of dicts. Each dict has
start (ms),
end (ms),
delta (ms),
perc (%),
tag (str)
'''
self.scores.append(Score(tag='finish', stamp=time.clock()))
total_time = self._stampDelta(-1, 0)
data = []
for i in range(1, len(self.scores)):
delta = self._stampDelta(i, i-1)
if abs(total_time) < 1e-6:
perc = 0
else:
perc = delta / total_time * 100
data.append(
dict(
start = self._stampDelta(i-1, 0) * 1000,
end = self._stampDelta(i, 0) * 1000,
delta = delta * 1000,
perc = perc,
tag = self.scores[i].tag
)
)
if orderByCost:
data.sort(key=lambda x: x['perc'], reverse=True)
return data
def getReportLines(self, orderByCost=True):
'''Produces a report of logged time-stamps as a list of strings.
if orderByCost is False, then the order of the stamps is
chronological.'''
data = self.getReportItems(orderByCost)
headerTemplate = '%10s | %10s | %10s | %11s | %-30s'
headerData = ('Start(ms)', 'End(ms)', 'Delta(ms)', 'Time Cost',
'Description')
bodyTemplate = '%(start)10.0f | %(end)10.0f | %(delta)10.0f |' \
+ ' %(perc)10.0f%% | %(tag)-30s'
return [headerTemplate % headerData] + [bodyTemplate % d for d in data]
def getReportText(self, **kwargs):
return '\n'.join(self.getReportLines(**kwargs))
def restart(self):
self.scores = [Score(tag='start',stamp=time.clock())]
if __name__=='__main__':
print('')
print('Testing:')
print('')
# First create the collector
t = TimeCollector()
x = [i for i in range(1000)]
# Every time some work gets done, add a stamp
t.addStamp('Initialization Section')
x = [i for i in range(10000)]
t.addStamp('A big loop')
x = [i for i in range(100000)]
t.addStamp('calling builder function')
# Finally, obtain the results
print('')
print(t.getReportText())
# If you want to measure something else in the same scope, you can
# restart the collector.
t.restart()
x = [i for i in range(1000000)]
t.addStamp('Part 2')
x = [i for i in range(1000000)]
t.addStamp('Cleanup')
# And once again report results
print('')
print(t.getReportText())
t.restart()
for y in range(1, 200, 20):
x = [i for i in range(10000)*y]
t.addStamp('Iteration when y = ' + str(y))
print('')
# You can turn off ordering of results
print(t.getReportText(orderByCost=False))
|