Skip to content

Instantly share code, notes, and snippets.

@mithrandi
Last active Apr 17, 2016
Embed
What would you like to do?
twisted.web.template / nevow.page optimization stuff
import time
from StringIO import StringIO
from nevow import tags
from nevow.context import WovenContext
from nevow.flat.twist import deferflatten
from nevow.loaders import stan, xmlstr
from nevow.page import Element, renderer
try:
import __pypy__
PYPY = True
except ImportError:
PYPY = False
class Elem(Element):
docFactory = xmlstr(
"""
<div xmlns:nevow="http://nevow.com/ns/nevow/0.1">
""" + "A" * 1000 + """
<div nevow:render="r" />
<div nevow:render="r2" />
<div nevow:render="r3">
<nevow:slot name="meep" />
</div>
</div>
""")
def __init__(self, children=[]):
super(Elem, self).__init__()
self.children = children
@renderer
def r(self, req, tag):
return tag(attr=u'value')[
[self.children,
u'hi mom!']]
@renderer
def r2(self, req, tag):
return tags.div(attr=u'value')[u'foo']
@renderer
def r3(self, req, tag):
return tag.fillSlots(
u'meep',
(u'slotvalue', u'42', 'bar', tags.div(attr=u'value')[u'meep']))
def render():
child = Elem()
for _ in xrange(20):
child = Elem([child])
root = stan([child] * 10).load()
out = StringIO()
deferflatten(
root, WovenContext(tag=tags.invisible[root]), out.write)
def bench(f, calibration_time=1.0, warmup=1000):
if PYPY:
for _ in xrange(warmup):
f()
start = time.time()
n = 0
while time.time() < start + calibration_time:
f()
n += 1
iterations = int(n * 5 / calibration_time)
start = time.time()
for _ in xrange(iterations):
f()
end = time.time()
return (
(end - start) / 5.0,
(end - start) / iterations * 1000,
iterations / (end - start))
if __name__ == '__main__':
print bench(render)
import time
from StringIO import StringIO
from twisted.web.template import (
Element, flatten, renderer, TagLoader, tags, XMLString)
try:
import __pypy__
PYPY = True
except ImportError:
PYPY = False
class Elem(Element):
loader = XMLString(
"""
<div xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
""" + "A" * 1000 + """
<div t:render="r" />
<div t:render="r2" />
<div t:render="r3">
<t:slot name="meep" />
</div>
</div>
""")
def __init__(self, children=[]):
super(Elem, self).__init__()
self.children = children
@renderer
def r(self, req, tag):
return tag(
[self.children,
u'hi mom!'],
attr=u'value')
@renderer
def r2(self, req, tag):
return tags.div(u'foo', attr=u'value')
@renderer
def r3(self, req, tag):
return tag.fillSlots(
meep=(u'slotvalue',
u'42',
'bar',
tags.div(u'meep', attr=u'value')))
def render():
child = Elem()
for _ in xrange(20):
child = Elem([child])
root = TagLoader([child] * 10).load()
out = StringIO()
flatten(None, root, out.write)
def bench(f, calibration_time=1.0, warmup=1000):
if PYPY:
for _ in xrange(warmup):
f()
start = time.time()
n = 0
while time.time() < start + calibration_time:
f()
n += 1
iterations = int(n * 5 / calibration_time)
start = time.time()
for _ in xrange(iterations):
f()
end = time.time()
return (
(end - start) / 5.0,
(end - start) / iterations * 1000,
iterations / (end - start))
if __name__ == '__main__':
print bench(render)
Renders / second Unoptimized Write optimization Speedup
nevow.page PyPy 5.0.1 156.330 246.745 57.8%
CPython 2.7.11 23.805 25.468 6.9%
t.w.template PyPy 5.0.1 119.027 166.140 39.6%
CPython 2.7.11 16.089 18.209 13.1%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment