Commits

Andriy Kornatskyy committed b534271

Added widget benchmark for wheezy.template.

Comments (0)

Files changed (8)

06-widgets/Makefile

+.SILENT: clean env pypy
+.PHONY: clean env pypy
+
+VERSION=2.7
+PYPI=http://pypi.python.org/simple
+ENV=env
+
+PYTHON=$(ENV)/bin/python$(VERSION)
+PYPY=pypy-1.9
+
+
+env:
+	PYTHON_EXE=/usr/local/bin/python$(VERSION); \
+	if [ ! -x $$PYTHON_EXE ]; then \
+		    PYTHON_EXE=/usr/bin/python$(VERSION); \
+	fi; \
+	virtualenv --python=$$PYTHON_EXE --no-site-packages env
+
+	if [ "$$(echo $(VERSION) | sed 's/\.//')" -lt 30 ]; then \
+		cd $(ENV)/bin && ./easy_install-$(VERSION) -i $(PYPI) -O2 \
+			"wheezy.template>=0.1.125" "mako>=0.7.2" "jinja2>=2.6" \
+			"django>=1.4.2" "tornado>=2.4"; \
+	else \
+		cd $(ENV)/bin && ./easy_install-$(VERSION) -i $(PYPI) \
+				"distribute>=0.6.28" \
+			&& ./easy_install-$(VERSION) -i $(PYPI) -O2 \
+				"wheezy.template>=0.1.125" "mako>=0.7.2" "jinja2>=2.6" \
+				"tornado>=2.4"; \
+	fi
+
+pypy:
+	if [ `uname -m` = "x86_64" ]; then \
+		ARCH='64'; \
+	else \
+		ARCH=''; \
+	fi; \
+	if [ ! -f $(PYPY)-linux$$ARCH.tar.bz2 ]; then \
+		wget https://bitbucket.org/pypy/pypy/downloads/$(PYPY)-linux$$ARCH.tar.bz2; \
+	fi; \
+	tar xjf $(PYPY)-linux$$ARCH.tar.bz2; \
+	wget http://python-distribute.org/distribute_setup.py ; \
+	$(PYPY)/bin/pypy distribute_setup.py ; \
+	rm distribute* ; \
+	cd $(PYPY)/bin && ./easy_install -i $(PYPI) -O2 \
+		"wheezy.template>=0.1.125" "mako>=0.7.2" "jinja2>=2.6" \
+		"tornado>=2.4"
+
+clean:
+	find ./ -type d -name __pycache__ | xargs rm -rf
+	find ./ -name '*.py[co]' -delete

06-widgets/benchmark.py

+
+"""
+"""
+
+import os
+import sys
+
+try:
+    import cProfile as profile
+except ImportError:
+    import profile
+
+from pstats import Stats
+from timeit import timeit
+
+
+path = os.getcwd()
+
+frameworks = ['wheezy.template']
+frameworks = sorted(frameworks)
+
+
+def run(name, ctx, number=100000):
+    sys.path[0] = '.'
+    print("\n%-16s   msec    rps  tcalls  funcs" % name)
+    for framework in frameworks:
+        os.chdir(os.path.join(path, framework))
+        if not os.path.exists(name):
+            print("%-22s not available" % framework)
+            continue
+        try:
+            main = __import__('app', None, None, ['main']).main
+            render = main(name)
+            f = lambda: render(ctx)
+            f()  # warm up first call
+            time = timeit(f, number=number)
+            st = Stats(profile.Profile().runctx('f()', globals(), locals()))
+            print("%-16s %6.0f %6.0f %7d %6d" % (framework, 1000 * time,
+                  number / time, st.total_calls, len(st.stats)))
+            if 0:
+                st = Stats(profile.Profile().runctx(
+                    'timeit(f, number=number)', globals(), locals()))
+                st.strip_dirs().sort_stats('time').print_stats(10)
+            del sys.modules['app']
+        except ImportError:
+            print("%-22s not installed" % framework)
+
+
+def run_batch(ctx):
+    print("\nlen(names) == %s" % len(ctx['names']))
+    run('01-initial', ctx)
+    run('02-single', ctx)
+    run('03-loop', ctx)
+
+
+if __name__ == '__main__':
+    run_batch({'names': []})
+    run_batch({'names': ['World']})
+    run_batch({'names': ['World'] * 10})

06-widgets/wheezy.template/01-initial/welcome.html

+@require(names)
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <title>My Site - Welcome</title>
+    </head>
+    <body>
+        <div id="greeting">
+            @for name in names:
+            <p>
+                Hello <i>@name</i>!
+            </p>
+            @end
+        </div>
+    </body>
+</html>

06-widgets/wheezy.template/02-single/shared/widgets.html

+
+@def greeting(names):
+    <div id="greeting">
+        @for name in names:
+        <p>
+            Hello <i>@name</i>!
+        </p>
+        @end
+    </div>
+@end

06-widgets/wheezy.template/02-single/welcome.html

+@from "shared/widgets.html" import greeting
+@require(names)
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <title>My Site - Welcome</title>
+    </head>
+    <body>
+        @greeting(names)
+    </body>
+</html>

06-widgets/wheezy.template/03-loop/shared/widgets.html

+
+@def greeting(name):
+    <p>
+        Hello <i>@name</i>!
+    </p>
+@end

06-widgets/wheezy.template/03-loop/welcome.html

+@from "shared/widgets.html" import greeting
+@require(names)
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <title>My Site - Welcome</title>
+    </head>
+    <body>
+        <div id="greeting">
+            @for name in names:
+                @greeting(name)
+            @end
+        </div>
+    </body>
+</html>

06-widgets/wheezy.template/app.py

+
+"""
+"""
+
+from wheezy.template.engine import Engine
+from wheezy.template.loader import FileLoader
+from wheezy.template.ext.core import CoreExtension
+
+
+def main(name):
+    searchpath = [name]
+    engine = Engine(
+        loader=FileLoader(searchpath),
+        extensions=[
+            CoreExtension()
+        ]
+    )
+
+    template = engine.get_template('welcome.html')
+    return template.render