File: run.py

package info (click to toggle)
genshi 0.7.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,032 kB
  • sloc: python: 16,598; javascript: 3,285; ansic: 734; xml: 45; cs: 31; sh: 28; makefile: 16
file content (48 lines) | stat: -rwxr-xr-x 1,316 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python
# -*- coding: utf-8 -*-

from __future__ import print_function

import os
import sys
import time

from genshi.template import TemplateLoader

def test():
    base_path = os.path.dirname(os.path.abspath(__file__))
    loader = TemplateLoader([base_path], auto_reload=True)

    start = time.clock()
    tmpl = loader.load('test.html')
    print(' --> parse stage: %.4f ms' % ((time.clock() - start) * 1000))

    data = dict(hello='<world>', skin='default', hey='ZYX', bozz=None,
                items=['Number %d' % num for num in range(1, 15)],
                prefix='#')

    print(tmpl.generate(**data).render(method='html'))

    times = []
    for i in range(1000):
        start = time.clock()
        list(tmpl.generate(**data))
        times.append(time.clock() - start)
        sys.stdout.write('.')
        sys.stdout.flush()
    print()

    print(' --> render stage: %s ms (average)' % (
          (sum(times) / len(times) * 1000)))

if __name__ == '__main__':
    if '-p' in sys.argv:
        import hotshot, hotshot.stats
        prof = hotshot.Profile("template.prof")
        benchtime = prof.runcall(test)
        stats = hotshot.stats.load("template.prof")
        stats.strip_dirs()
        stats.sort_stats('time', 'calls')
        stats.print_stats()
    else:
        test()