changeset 59:352d176b4f06

Minor improvements to the benchmark thing.
author cmlenz
date Wed, 05 Jul 2006 07:58:36 +0000
parents a814095d89e7
children 94154a39c986
files examples/bench/kid/header.kid examples/bench/run.py
diffstat 2 files changed, 60 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
deleted file mode 100644
--- a/examples/bench/kid/header.kid
+++ /dev/null
@@ -1,6 +0,0 @@
-<div py:def="header()" id="header">
-  <h1><?cs var:title ?></h1>
-</div>
-
-<div py:def="footer()" id="footer">
-</div>
--- a/examples/bench/run.py
+++ b/examples/bench/run.py
@@ -3,22 +3,6 @@
 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)
@@ -31,44 +15,54 @@
 
 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)
+    try:
+        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 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
+    except ImportError:
+        return None
 
 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
+    try:
+        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')
+            cs.render()
+        return render
+    except ImportError:
+        return None
 
 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
+    try:
+        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
+    except ImportError:
+        return None
 
 def main():
     basepath = os.path.abspath(os.path.dirname(__file__))
@@ -76,7 +70,23 @@
         dirname = os.path.join(basepath, engine)
         print '%s:' % engine.capitalize()
         func = globals()[engine](dirname)
-        _measure_time(func)
+        if not func:
+            print 'Skipping %s, not installed?' % engine.capitalize()
+            continue
+        times = []
+        for i in range(100):
+            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])))
         print
 
 if __name__ == '__main__':
Copyright (C) 2012-2017 Edgewall Software