forked from noahmorrison/chevron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
executable file
·83 lines (69 loc) · 2.02 KB
/
benchmark.py
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
#!/usr/bin/python
# coding: utf-8
from sys import argv
from timeit import timeit
import chevron
def make_test(template=None, data=None, expected=None):
def test():
result = chevron.render(template, data)
if result != expected:
error = 'Test failed:\n-- got --\n{}\n-- expected --\n{}'
raise Exception(error.format(result, expected))
return test
def main(times):
args = {
'template': """\
{{# comments }}
<div class=comment>
<span class=user>{{ user }}</span>
<span class=body>{{ body }}</span>
<span class=vote>{{ vote }}</span>
</div>
{{/ comments }}
""",
'data': {
'comments': [
{'user': 'tommy',
'body': 'If this gets to the front page I\'ll eat my hat!',
'vote': 625},
{'user': 'trololol',
'body': 'this',
'vote': -142},
{'user': 'mctom',
'body': 'I wish thinking of test phrases was easier',
'vote': 83},
{'user': 'the_thinker',
'body': 'Why is /u/trololol\'s post higher than ours?',
'vote': 36}
]
},
'expected': """\
<div class=comment>
<span class=user>tommy</span>
<span class=body>If this gets to the front page I'll eat my hat!</span>
<span class=vote>625</span>
</div>
<div class=comment>
<span class=user>trololol</span>
<span class=body>this</span>
<span class=vote>-142</span>
</div>
<div class=comment>
<span class=user>mctom</span>
<span class=body>I wish thinking of test phrases was easier</span>
<span class=vote>83</span>
</div>
<div class=comment>
<span class=user>the_thinker</span>
<span class=body>Why is /u/trololol's post higher than ours?</span>
<span class=vote>36</span>
</div>
"""
}
test = make_test(**args)
print(timeit(test, number=times))
if __name__ == '__main__':
try:
main(int(argv[1]))
except IndexError:
main(10000)