Mercurial > genshi > mirror
changeset 57:300b6a3b0730 trunk
Add some simple benchmarks to compare performance against Clearsilver, Kid, Cheetah, and more soon.
author | cmlenz |
---|---|
date | Tue, 04 Jul 2006 18:19:22 +0000 |
parents | 6474226bb8b6 |
children | 6b93567d871d |
files | examples/bench/cheetah/footer.tmpl examples/bench/cheetah/header.tmpl examples/bench/cheetah/template.tmpl examples/bench/clearsilver/footer.cs examples/bench/clearsilver/header.cs examples/bench/clearsilver/template.cs examples/bench/kid/base.kid examples/bench/kid/header.kid examples/bench/kid/template.kid examples/bench/markup/footer.html examples/bench/markup/header.html examples/bench/markup/template.html examples/bench/run.py |
diffstat | 13 files changed, 198 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/examples/bench/cheetah/footer.tmpl @@ -0,0 +1,2 @@ +<div id="footer"> +</div>
new file mode 100644 --- /dev/null +++ b/examples/bench/cheetah/header.tmpl @@ -0,0 +1,3 @@ +<div id="header"> + <h1>$title</h1> +</div>
new file mode 100644 --- /dev/null +++ b/examples/bench/cheetah/template.tmpl @@ -0,0 +1,22 @@ +<!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" lang="en"> + <head> + <title>${title}</title> + </head> + <body> + #include "header.tmpl" + + <h2>Loop</h2> + #if $items + <ul> + #for $item in $items + <li>$item</li> + #end for + </ul> + #end if + + #include "footer.tmpl" + </body> +</html>
new file mode 100644 --- /dev/null +++ b/examples/bench/clearsilver/footer.cs @@ -0,0 +1,2 @@ +<div id="footer"> +</div>
new file mode 100644 --- /dev/null +++ b/examples/bench/clearsilver/header.cs @@ -0,0 +1,3 @@ +<div id="header"> + <h1><?cs var:title ?></h1> +</div>
new file mode 100644 --- /dev/null +++ b/examples/bench/clearsilver/template.cs @@ -0,0 +1,22 @@ +<!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" lang="en"> + <head> + <title><?cs var:title ?></title> + </head> + <body> + <?cs include:"header.cs" ?> + + <h2>Loop</h2> + <?cs if:len(items) ?> + <ul> + <?cs each:item = items ?> + <li><?cs var:item ?></li> + <?cs /each ?> + </ul> + <?cs /if ?> + + <?cs include:"footer.cs" ?> + </body> +</html>
new file mode 100644 --- /dev/null +++ b/examples/bench/kid/base.kid @@ -0,0 +1,8 @@ +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:py="http://purl.org/kid/ns#"> + <div py:def="header()" id="header"> + <h1><?cs var:title ?></h1> + </div> + <div py:def="footer()" id="footer"> + </div> +</html>
new file mode 100644 --- /dev/null +++ b/examples/bench/kid/header.kid @@ -0,0 +1,6 @@ +<div py:def="header()" id="header"> + <h1><?cs var:title ?></h1> +</div> + +<div py:def="footer()" id="footer"> +</div>
new file mode 100644 --- /dev/null +++ b/examples/bench/kid/template.kid @@ -0,0 +1,21 @@ +<!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" + xmlns:py="http://purl.org/kid/ns#" + py:extends="'base.kid'" + lang="en"> + <head> + <title>${title}</title> + </head> + <body> + ${header()} + + <h2>Loop</h2> + <ul py:if="items"> + <li py:for="item in items">${item}</li> + </ul> + + ${footer()} + </body> +</html>
new file mode 100644 --- /dev/null +++ b/examples/bench/markup/footer.html @@ -0,0 +1,2 @@ +<div id="footer"> +</div>
new file mode 100644 --- /dev/null +++ b/examples/bench/markup/header.html @@ -0,0 +1,3 @@ +<div id="header"> + <h1>${title}</h1> +</div>
new file mode 100644 --- /dev/null +++ b/examples/bench/markup/template.html @@ -0,0 +1,21 @@ +<!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" + xmlns:py="http://purl.org/kid/ns#" + xmlns:xi="http://www.w3.org/2001/XInclude" + lang="en"> + <head> + <title>${title}</title> + </head> + <body> + <xi:include href="header.html" /> + + <h2>Loop</h2> + <ul py:if="items"> + <li py:for="item in items" py:content="item"/> + </ul> + + <xi:include href="footer.html" /> + </body> +</html>
new file mode 100644 --- /dev/null +++ b/examples/bench/run.py @@ -0,0 +1,83 @@ +from cgi import escape +from datetime import datetime, timedelta +import os +import sys + +def _measure_time(func, repeat=100): + times = [] + for i in range(repeat): + start = datetime.now() + sys.stdout.write('.') + sys.stdout.flush() + func() + times.append(datetime.now() - start) + + print + total_ms = sum([t.seconds * 1000 + t.microseconds for t in times]) + print ' --> timing: %s (avg), %s (min), %s (max)' % ( + timedelta(microseconds=total_ms / len(times)), + timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])), + timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times]))) + +def markup(dirname): + from markup.template import Context, TemplateLoader + loader = TemplateLoader([dirname], False) + template = loader.load('template.html') + def render(): + ctxt = Context(title='Just a test', + items=['Number %d' % num for num in range(1, 15)]) + template.generate(ctxt).render('html') + return render + +def cheetah(dirname): + # FIXME: infinite recursion somewhere... WTF? + from Cheetah.Template import Template + class MyTemplate(Template): + def serverSidePath(self, path): return os.path.join(dirname, path) + filename = os.path.join(dirname, 'template.tmpl') + template = MyTemplate(file=filename) + + def render(): + template = MyTemplate(file=filename, + searchList=[{'title': 'Just a test', + 'items': [u'Number %d' % num for num in range(1, 15)]}]) + template.respond() + return render + +def clearsilver(dirname): + import neo_cgi + neo_cgi.update() + import neo_util + import neo_cs + def render(): + hdf = neo_util.HDF() + hdf.setValue('hdf.loadpaths.0', dirname) + hdf.setValue('title', escape('Just a test')) + for num in range(1, 15): + hdf.setValue('items.%d' % (num - 1), escape('Number %d' % num)) + cs = neo_cs.CS(hdf) + cs.parseFile('template.cs') + return render + +def kid(dirname): + import kid + kid.path = kid.TemplatePath([dirname]) + template = kid.Template(file='template.kid') + def render(): + template = kid.Template(file='template.kid', + title='Just a test', + items=['Number %d' % num for num in range(1, 15)]) + template.serialize(output='xhtml') + return render + +def main(): + basepath = os.path.abspath(os.path.dirname(__file__)) + for engine in ('markup', 'clearsilver', 'kid'): + dirname = os.path.join(basepath, engine) + print '%s:' % engine.capitalize() + func = globals()[engine](dirname) + _measure_time(func) + print + +if __name__ == '__main__': + main()