forked from django-cms/django-classy-tags
/
performance.py
63 lines (56 loc) · 1.68 KB
/
performance.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
"""
Tests the performance of django builtin tags versus classytags implementations
of them.
"""
from testdata import pool, Benchmark
import sys
def format_num(num):
try:
return "%0.3f" % num
except TypeError:
return str(num)
def get_max_width(table, index):
return max([len(format_num(row[index])) for row in table])
def pprint_table(out, table):
col_paddings = []
for i in range(len(table[0])):
col_paddings.append(get_max_width(table, i))
for row in table:
# left col
print >> out, row[0].ljust(col_paddings[0] + 1),
# rest of the cols
for i in range(1, len(row)):
col = format_num(row[i]).rjust(col_paddings[i] + 2)
print >> out, col,
print >> out
def run(prnt, iterations):
print
print "Performance of django tags versus classytags. %s iterations." % iterations
print
pool.autodiscover()
table = []
table.append(["Tagname", "Django", "Classytags", "Ratio"])
for tagname, data in pool:
bench = Benchmark(data['tag'])
django = bench.django(iterations)
classy = bench.classy(iterations)
ratio = classy / django
if tagname.startswith('ct_'):
tagname = tagname[3:]
table.append([tagname, django, classy, ratio])
if prnt:
pprint_table(sys.stdout, table)
else:
return table
if __name__ == '__main__':
import optparse
parser = optparse.OptionParser()
options, args = parser.parse_args()
if len(args):
try:
iterations = int(args[0])
except TypeError:
iterations = 10000
else:
iterations = 10000
run(True, iterations)