| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | import os |
|---|
| 5 | import sys |
|---|
| 6 | import time |
|---|
| 7 | |
|---|
| 8 | from genshi.template import TemplateLoader |
|---|
| 9 | |
|---|
| 10 | def test(): |
|---|
| 11 | base_path = os.path.dirname(os.path.abspath(__file__)) |
|---|
| 12 | loader = TemplateLoader([base_path], auto_reload=True) |
|---|
| 13 | |
|---|
| 14 | start = time.clock() |
|---|
| 15 | tmpl = loader.load('test.html') |
|---|
| 16 | print ' --> parse stage: %.4f ms' % ((time.clock() - start) * 1000) |
|---|
| 17 | |
|---|
| 18 | data = dict(hello='Wörld', skin='default', hey='ZYX', bozz=None, |
|---|
| 19 | items=['Number %d' % num for num in range(1, 15)], |
|---|
| 20 | prefix='#') |
|---|
| 21 | |
|---|
| 22 | print tmpl.generate(**data).render(method='html') |
|---|
| 23 | |
|---|
| 24 | times = [] |
|---|
| 25 | for i in range(1000): |
|---|
| 26 | start = time.clock() |
|---|
| 27 | list(tmpl.generate(**data)) |
|---|
| 28 | times.append(time.clock() - start) |
|---|
| 29 | sys.stdout.write('.') |
|---|
| 30 | sys.stdout.flush() |
|---|
| 31 | print |
|---|
| 32 | |
|---|
| 33 | print ' --> render stage: %s ms (average)' % ( |
|---|
| 34 | (sum(times) / len(times) * 1000)) |
|---|
| 35 | |
|---|
| 36 | if __name__ == '__main__': |
|---|
| 37 | if '-p' in sys.argv: |
|---|
| 38 | import hotshot, hotshot.stats |
|---|
| 39 | prof = hotshot.Profile("template.prof") |
|---|
| 40 | benchtime = prof.runcall(test) |
|---|
| 41 | stats = hotshot.stats.load("template.prof") |
|---|
| 42 | stats.strip_dirs() |
|---|
| 43 | stats.sort_stats('time', 'calls') |
|---|
| 44 | stats.print_stats() |
|---|
| 45 | else: |
|---|
| 46 | test() |
|---|