]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a template benchmark, based on one in jinja2
authorBen Darnell <ben@bendarnell.com>
Thu, 28 Jul 2011 05:01:05 +0000 (22:01 -0700)
committerBen Darnell <ben@bendarnell.com>
Thu, 28 Jul 2011 05:01:05 +0000 (22:01 -0700)
demos/benchmark/template_benchmark.py [new file with mode: 0755]

diff --git a/demos/benchmark/template_benchmark.py b/demos/benchmark/template_benchmark.py
new file mode 100755 (executable)
index 0000000..07833af
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+#
+# A simple benchmark of tornado template rendering, based on
+# https://github.com/mitsuhiko/jinja2/blob/master/examples/bench.py
+
+from timeit import Timer
+
+from tornado.options import options, define, parse_command_line
+from tornado.template import Template
+
+define('num', default=100, help='number of iterations')
+
+context = {
+    'page_title': 'mitsuhiko\'s benchmark',
+    'table': [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) for x in range(1000)]
+}
+
+tmpl = Template("""\
+<!doctype html>
+<html>
+  <head>
+    <title>{{ page_title }}</title>
+  </head>
+  <body>
+    <div class="header">
+      <h1>{{ page_title }}</h1>
+    </div>
+    <ul class="navigation">
+    {% for href, caption in [ \
+        ('index.html', 'Index'), \
+        ('downloads.html', 'Downloads'), \
+        ('products.html', 'Products') \
+      ] %}
+      <li><a href="{{ href }}">{{ caption }}</a></li>
+    {% end %}
+    </ul>
+    <div class="table">
+      <table>
+      {% for row in table %}
+        <tr>
+        {% for cell in row %}
+          <td>{{ cell }}</td>
+        {% end %}
+        </tr>
+      {% end %}
+      </table>
+    </div>
+  </body>
+</html>\
+""")
+
+def render():
+    tmpl.generate(**context)
+
+def main():
+    parse_command_line()
+    t = Timer(render)
+    results = t.timeit(options.num) / options.num
+    print '%0.3f ms per iteration' % (results*1000)
+
+if __name__ == '__main__':
+    main()