[Twisted-web] [Twisted-Python] Speed of rendering?

Peter Westlake peter.westlake at pobox.com
Fri Jan 4 13:30:47 EST 2013

A while back I promised to write some benchmarks for
twisted.web.template's flattening functions. Is something like this
suitable? If so, I'll add lots more test cases. The output format could
be improved, too - any preferences?


from twisted.web.template import flatten
from twisted.web.server import Request
import twisted.web.http

channel = twisted.web.http.HTTPChannel()
request = Request(channel, False)

def make(content):
    def f():
        d = flatten(request, content, lambda _: None)
        assert d.called
    return f

def test(content):
    return timeit.timeit(stmt=make(content), number=repeats)

repeats = 1000

deeplist = ['centre']
for n in range(100):
    deeplist = [deeplist]

tests = {
    'empty': '',
    'string': 'hello',
    'shortlist': ['hello'],
    'longlist': [str(n) for n in range(100)],
    'deeplist': deeplist,

if __name__ == '__main__':
    import timeit
    from sys import argv
    for name in argv[1:] or tests:
        print name, test(tests[name])

More information about the Twisted-web mailing list