3
|
1 from datetime import datetime, timedelta
|
|
2 import os
|
|
3 import sys
|
|
4
|
|
5 import kid
|
|
6
|
|
7 def test():
|
|
8 base_path = os.path.dirname(os.path.abspath(__file__))
|
|
9 kid.path = kid.TemplatePath([os.path.join(base_path, 'common'),
|
|
10 os.path.join(base_path, 'module')])
|
|
11
|
|
12 ctxt = dict(hello='<world>', hey='ZYX', bozz=None,
|
|
13 items=['Number %d' % num for num in range(1, 15)],
|
|
14 prefix='#')
|
|
15
|
|
16 start = datetime.now()
|
|
17 template = kid.Template(file='test.kid', **ctxt)
|
|
18 print ' --> parse stage: ', datetime.now() - start
|
|
19
|
|
20 times = []
|
|
21 for i in range(100):
|
|
22 start = datetime.now()
|
|
23 for output in template.generate():
|
|
24 if i == 0:
|
|
25 sys.stdout.write(output)
|
|
26 if i == 0:
|
|
27 print
|
|
28 else:
|
|
29 sys.stdout.write('.')
|
|
30 sys.stdout.flush()
|
|
31 times.append(datetime.now() - start)
|
|
32 print
|
|
33
|
|
34 total_ms = sum([t.seconds * 1000 + t.microseconds for t in times])
|
|
35 print ' --> render stage: %s (avg), %s (min), %s (max):' % (
|
|
36 timedelta(microseconds=total_ms / len(times)),
|
|
37 timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])),
|
|
38 timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times])))
|
|
39
|
|
40 if __name__ == '__main__':
|
|
41 if '-p' in sys.argv:
|
|
42 import profile, pstats
|
|
43 profile.run('test()', '.tmpl_prof')
|
|
44 stats = pstats.Stats('.tmpl_prof')
|
|
45 stats.strip_dirs().sort_stats('time').print_stats(10)
|
|
46 else:
|
|
47 test()
|