aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2008-05-15 22:47:27 +0200
committerArmin Ronacher <armin.ronacher@active-4.com>2008-05-15 22:47:27 +0200
commitdc02b64d62e1871a619cac2dab42d041755c3686 (patch)
tree5fa0aa3c89aefc0cacf5278fba860975bf7e2a89 /examples
parente9411b41890c0882236918297fb2e77a3bf60cd1 (diff)
downloadjinja-dc02b64d62e1871a619cac2dab42d041755c3686.tar.gz
added a semi realworld benchmark (jinja2 and mako)
--HG-- branch : trunk
Diffstat (limited to 'examples')
-rw-r--r--examples/rwbench/jinja/helpers.html12
-rw-r--r--examples/rwbench/jinja/index.html26
-rw-r--r--examples/rwbench/jinja/layout.html29
-rw-r--r--examples/rwbench/mako/helpers.html11
-rw-r--r--examples/rwbench/mako/index.html27
-rw-r--r--examples/rwbench/mako/layout.html30
-rw-r--r--examples/rwbench/rwbench.py83
7 files changed, 218 insertions, 0 deletions
diff --git a/examples/rwbench/jinja/helpers.html b/examples/rwbench/jinja/helpers.html
new file mode 100644
index 00000000..89976aa0
--- /dev/null
+++ b/examples/rwbench/jinja/helpers.html
@@ -0,0 +1,12 @@
+{% macro input_field(name, value='', type='text') -%}
+ <input type="{{ type }}" value="{{ value|e }}" name="{{ name }}">
+{%- endmacro %}
+
+{% macro textarea(name, value='', rows=10, cols=40) -%}
+ <textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{
+ value|e }}</textarea>
+{%- endmacro %}
+
+{% macro form(action='', method='post') -%}
+ <form action="{{ action|e }}" method="{{ method }}">{{ caller() }}</form>
+{%- endmacro %}
diff --git a/examples/rwbench/jinja/index.html b/examples/rwbench/jinja/index.html
new file mode 100644
index 00000000..2b97e706
--- /dev/null
+++ b/examples/rwbench/jinja/index.html
@@ -0,0 +1,26 @@
+{% extends "layout.html" %}
+{% from "helpers.html" import input_field, textarea, form %}
+{% block page_title %}Index Page{% endblock %}
+{% block body %}
+ {%- for article in articles %}
+ <div class="article">
+ <h2><a href="{{ article.href|e }}">{{ article.title|e }}</a></h2>
+ <p class="meta">written by <a href="{{ article.user.href|e
+ }}">{{ article.user.username|e }}</a> on {{ article.pub_date|dateformat }}</p>
+ <div class="text">{{ article.body }}</div>
+ </div>
+ {%- endfor %}
+ {%- call form() %}
+ <dl>
+ <dt>Name</dt>
+ <dd>{{ input_field('name') }}</dd>
+ <dt>E-Mail</dt>
+ <dd>{{ input_field('email') }}</dd>
+ <dt>URL</dt>
+ <dd>{{ input_field('url') }}</dd>
+ <dt>Comment</dd>
+ <dd>{{ textarea('comment') }}</dd>
+ </dl>
+ {{ input_field(type='submit', value='Submit') }}
+ {%- endcall %}
+{% endblock %}
diff --git a/examples/rwbench/jinja/layout.html b/examples/rwbench/jinja/layout.html
new file mode 100644
index 00000000..755789e2
--- /dev/null
+++ b/examples/rwbench/jinja/layout.html
@@ -0,0 +1,29 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+ <title>{% block page_title %}{% endblock %} | RealWorld Benchmark</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+</head>
+<body>
+ <div class="contents">
+ <div class="header">
+ <h1>RealWorld Benchmark</h1>
+ <blockquote><p>
+ A less stupid benchmark for Mako and Jinja2 to get an impression how
+ code changes affect runtime performance.
+ </p></blockquote>
+ </div>
+ <ul class="navigation">
+ {%- for href, caption in page_navigation %}
+ <li><a href="{{ href|e }}">{{ caption }}</a></li>
+ {%- endfor %}
+ </ul>
+ <div class="body">
+ {% block body %}{% endblock %}
+ </div>
+ <div class="footer">
+ &copy; Copyright 2008 by I don't know who.
+ </div>
+ </div>
+</body>
+</html>
diff --git a/examples/rwbench/mako/helpers.html b/examples/rwbench/mako/helpers.html
new file mode 100644
index 00000000..a0290ebb
--- /dev/null
+++ b/examples/rwbench/mako/helpers.html
@@ -0,0 +1,11 @@
+<%def name="input_field(name='', value='', type='text')">
+ <input type="${type}" value="${value|h}" name="${name}">
+</%def>
+
+<%def name="textarea(name, value='', rows=10, cols=40)">
+ <textarea name="${name}" rows="${rows}" cols="${cols}">${value|h}</textarea>
+</%def>
+
+<%def name="form(action='', method='post')">
+ <form action="${action|h}" method="${method}">${caller.body()}</form>
+</%def>
diff --git a/examples/rwbench/mako/index.html b/examples/rwbench/mako/index.html
new file mode 100644
index 00000000..33bfe321
--- /dev/null
+++ b/examples/rwbench/mako/index.html
@@ -0,0 +1,27 @@
+<%!
+ from rwbench import dateformat
+%>
+<%inherit file="layout.html" />
+<%namespace file="helpers.html" import="input_field, textarea, form" />
+<%def name="page_title()">Index Page</%def>
+% for article in articles:
+<div class="article">
+ <h2><a href="${article.href|h}">${article.title|h}</a></h2>
+ <p class="meta">written by <a href="${article.user.href|h
+ }">${article.user.username|h}</a> on ${dateformat(article.pub_date)}</p>
+ <div class="text">${article.body}</div>
+</div>
+% endfor
+<%call expr="form()">
+ <dl>
+ <dt>Name</dt>
+ <dd>${input_field('name')}</dd>
+ <dt>E-Mail</dt>
+ <dd>${input_field('email')}</dd>
+ <dt>URL</dt>
+ <dd>${input_field('url')}</dd>
+ <dt>Comment</dd>
+ <dd>${textarea('comment')}</dd>
+ </dl>
+ ${input_field(type='submit', value='Submit')}
+</%call>
diff --git a/examples/rwbench/mako/layout.html b/examples/rwbench/mako/layout.html
new file mode 100644
index 00000000..a9c353e1
--- /dev/null
+++ b/examples/rwbench/mako/layout.html
@@ -0,0 +1,30 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+ <title>${self.page_title()} | RealWorld Benchmark</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+</head>
+<body>
+ <div class="contents">
+ <div class="header">
+ <h1>RealWorld Benchmark</h1>
+ <blockquote><p>
+ A less stupid benchmark for Mako and Jinja2 to get an impression how
+ code changes affect runtime performance.
+ </p></blockquote>
+ </div>
+ <ul class="navigation">
+ % for href, caption in page_navigation:
+ <li><a href="${href|h}">${caption}</a></li>
+ % endfor
+ </ul>
+ <div class="body">
+ ${self.body()}
+ </div>
+ <div class="footer">
+ &copy; Copyright 2008 by I don't know who.
+ </div>
+ </div>
+</body>
+</html>
+<%def name="page_title()"></%def>
diff --git a/examples/rwbench/rwbench.py b/examples/rwbench/rwbench.py
new file mode 100644
index 00000000..ccd144fe
--- /dev/null
+++ b/examples/rwbench/rwbench.py
@@ -0,0 +1,83 @@
+# -*- coding: utf-8 -*-
+"""
+ RealWorldish Benchmark
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ A more real-world benchmark of Jinja2.
+
+ :copyright: Copyright 2008 by Armin Ronacher.
+ :license: BSD.
+"""
+import sys
+from os.path import join, dirname, abspath
+from random import choice, randrange
+from datetime import datetime
+from timeit import Timer
+from jinja2 import Environment, FileSystemLoader
+from jinja2.utils import generate_lorem_ipsum
+from mako.lookup import TemplateLookup
+
+
+ROOT = abspath(dirname(__file__))
+
+
+def dateformat(x):
+ return x.strftime('%Y-%m-%d')
+
+
+jinja_env = Environment(loader=FileSystemLoader(join(ROOT, 'jinja')))
+jinja_env.filters['dateformat'] = dateformat
+
+mako_lookup = TemplateLookup(directories=[join(ROOT, 'mako')])
+
+
+class Article(object):
+
+ def __init__(self, id):
+ self.id = id
+ self.href = '/article/%d' % self.id
+ self.title = generate_lorem_ipsum(1, False, 5, 10)
+ self.user = choice(users)
+ self.body = generate_lorem_ipsum()
+ self.pub_date = datetime.utcfromtimestamp(randrange(1000000000,
+ 2000000000))
+
+
+class User(object):
+
+ def __init__(self, username):
+ self.href = '/user/%s' % username
+ self.username = username
+
+
+users = map(User, [u'John Doe', u'Jane Doe', u'Peter Somewhat'])
+articles = map(Article, range(20))
+navigation = [
+ ('index', 'Index'),
+ ('about', 'About'),
+ ('foo?bar=1', 'Foo with Bar'),
+ ('foo?bar=2&s=x', 'Foo with X')
+]
+
+context = dict(users=users, articles=articles, page_navigation=navigation)
+
+
+jinja_template = jinja_env.get_template('index.html')
+mako_template = mako_lookup.get_template('index.html')
+
+
+def test_jinja():
+ jinja_template.render(context)
+
+def test_mako():
+ mako_template.render_unicode(**context)
+
+
+if __name__ == '__main__':
+ sys.stdout.write('Realworldish Benchmark:\n')
+ for test in 'jinja', 'mako':
+ t = Timer(setup='from __main__ import test_%s as bench' % test,
+ stmt='bench()')
+ sys.stdout.write(' >> %-20s<running>' % test)
+ sys.stdout.flush()
+ sys.stdout.write('\r %-20s%.4f seconds\n' % (test, t.timeit(number=50) / 50))