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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#! /usr/bin/env python
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Run benchmarks of TAL vs. DTML
"""
import errno
import getopt
import os
import sys
import time
import warnings
from cStringIO import StringIO
from zope.tal.dummyengine import DummyEngine
from zope.tal.htmltalparser import HTMLTALParser
from zope.tal.talinterpreter import TALInterpreter
warnings.filterwarnings("ignore", category=DeprecationWarning)
os.environ['NO_SECURITY'] = 'true'
# from zope.documenttemplate.dt_html import HTMLFile
def time_apply(f, args, kwargs, count):
r = [None] * count
for i in range(4):
f(*args, **kwargs)
t0 = time.clock()
for i in r:
pass
t1 = time.clock()
for i in r:
f(*args, **kwargs)
t = time.clock() - t1 - (t1 - t0)
return t / count
def time_zpt(fn, count):
from zope.pagetemplate.pagetemplate import PageTemplate
pt = PageTemplate()
pt.write(open(fn).read())
return time_apply(pt.pt_render, (data,), {}, count)
def time_tal(fn, count):
p = HTMLTALParser()
p.parseFile(fn)
program, macros = p.getCode()
engine = DummyEngine(macros)
engine.globals = data
tal = TALInterpreter(program, macros, engine, StringIO(), wrap=0,
tal=1, strictinsert=0)
return time_apply(tal, (), {}, count)
def time_dtml(fn, count):
html = HTMLFile(fn) # noqa: F821 undefined name 'HTMLFile'
return time_apply(html, (), data, count)
def profile_zpt(fn, count, profiler):
from zope.pagetemplate.pagetemplate import PageTemplate
pt = PageTemplate()
pt.write(open(fn).read())
for i in range(4):
pt.pt_render(extra_context=data)
r = [None] * count
for i in r:
profiler.runcall(pt.pt_render, 0, data)
def profile_tal(fn, count, profiler):
p = HTMLTALParser()
p.parseFile(fn)
program, macros = p.getCode()
engine = DummyEngine(macros)
engine.globals = data
tal = TALInterpreter(program, macros, engine, StringIO(), wrap=0,
tal=1, strictinsert=0)
for i in range(4):
tal()
r = [None] * count
for i in r:
profiler.runcall(tal)
# Figure out where the benchmark files are:
try:
fname = __file__
except NameError:
fname = sys.argv[0]
taldir = os.path.dirname(os.path.dirname(os.path.abspath(fname)))
benchdir = os.path.join(taldir, 'benchmark')
# Construct templates for the filenames:
tal_fn = os.path.join(benchdir, 'tal%.2d.html')
dtml_fn = os.path.join(benchdir, 'dtml%.2d.html')
def compare(n, count, profiler=None, verbose=1):
if verbose:
t1 = int(time_zpt(tal_fn % n, count) * 1000 + 0.5)
t2 = int(time_tal(tal_fn % n, count) * 1000 + 0.5)
t3 = 'n/a' # int(time_dtml(dtml_fn % n, count) * 1000 + 0.5)
print('%.2d: %10s %10s %10s' % (n, t1, t2, t3))
if profiler:
profile_tal(tal_fn % n, count, profiler)
def main(count, profiler=None, verbose=1):
n = 1
if verbose:
print('##: %10s %10s %10s' % ('ZPT', 'TAL', 'DTML'))
while os.path.isfile(tal_fn % n) and os.path.isfile(dtml_fn % n):
compare(n, count, profiler, verbose)
n = n + 1
def get_signal_name(sig):
import signal
for name in dir(signal):
if getattr(signal, name) == sig:
return name
return None
data = {'x': 'X', 'r2': [0, 1], 'r8': list(range(8)), 'r64': list(range(64))}
for i in range(10):
data['x%s' % i] = 'X%s' % i
if __name__ == "__main__":
filename = "markbench.prof"
profiler = None
runtests = False
verbose = True
opts, args = getopt.getopt(sys.argv[1:], "pqt")
for opt, arg in opts:
if opt == "-p":
import profile
profiler = profile.Profile()
elif opt == "-q":
verbose = False
elif opt == "-t":
runtests = True
if runtests:
srcdir = os.path.dirname(os.path.dirname(taldir))
topdir = os.path.dirname(srcdir)
pwd = os.getcwd()
os.chdir(topdir)
rc = os.spawnl(os.P_WAIT, sys.executable,
sys.executable, "test.py", "zope.tal.tests")
if rc > 0:
# TODO: Failing tests don't cause test.py to report an
# error; not sure why. ;-(
sys.exit(rc)
elif rc < 0:
sig = -rc
print(("Process exited, signal %d (%s)." %
(sig, get_signal_name(sig) or "<unknown signal>")),
file=sys.stderr)
sys.exit(1)
os.chdir(pwd)
if len(args) >= 1:
for arg in args:
compare(int(arg), 25, profiler, verbose)
else:
main(25, profiler, verbose)
if profiler is not None:
profiler.dump_stats(filename)
import pstats
p = pstats.Stats(filename)
p.strip_dirs()
p.sort_stats('time', 'calls')
try:
p.print_stats(20)
except OSError as e:
if e.errno != errno.EPIPE:
raise
|