]> Devi Nivas Git - chevron.git/commitdiff
Added a benchmark
authornoah morrison <noah@morrison.ph>
Tue, 18 Nov 2014 18:13:22 +0000 (13:13 -0500)
committernoah morrison <noah@morrison.ph>
Tue, 18 Nov 2014 18:15:36 +0000 (13:15 -0500)
currently running
    ./benchmark
will return around 1.3631531159917358

benchmark.py [new file with mode: 0755]

diff --git a/benchmark.py b/benchmark.py
new file mode 100755 (executable)
index 0000000..e5e9452
--- /dev/null
@@ -0,0 +1,83 @@
+#!/usr/bin/python
+# coding: utf-8
+
+from sys import argv
+from timeit import timeit
+
+import chevron
+
+
+def make_test(template=None, data=None, expected=None):
+    def test():
+        result = chevron.render(template, data)
+        if result != expected:
+            error = 'Test failed:\n-- got --\n{}\n-- expected --\n{}'
+            raise Exception(error.format(result, expected))
+
+    return test
+
+
+def main(times):
+    args = {
+        'template': """\
+{{# comments }}
+<div class=comment>
+    <span class=user>{{ user }}</span>
+    <span class=body>{{ body }}</span>
+    <span class=vote>{{ vote }}</span>
+</div>
+{{/ comments }}
+""",
+        'data': {
+            'comments': [
+                {'user': 'tommy',
+                 'body': 'If this gets to the front page I\'ll eat my hat!',
+                 'vote': 625},
+
+                {'user': 'trololol',
+                 'body': 'this',
+                 'vote': -142},
+
+                {'user': 'mctom',
+                 'body': 'I wish thinking of test phrases was easier',
+                 'vote': 83},
+
+                {'user': 'the_thinker',
+                 'body': 'Why is /u/trololol\'s post higher than ours?',
+                 'vote': 36}
+            ]
+        },
+        'expected': """\
+<div class=comment>
+    <span class=user>tommy</span>
+    <span class=body>If this gets to the front page I'll eat my hat!</span>
+    <span class=vote>625</span>
+</div>
+<div class=comment>
+    <span class=user>trololol</span>
+    <span class=body>this</span>
+    <span class=vote>-142</span>
+</div>
+<div class=comment>
+    <span class=user>mctom</span>
+    <span class=body>I wish thinking of test phrases was easier</span>
+    <span class=vote>83</span>
+</div>
+<div class=comment>
+    <span class=user>the_thinker</span>
+    <span class=body>Why is /u/trololol's post higher than ours?</span>
+    <span class=vote>36</span>
+</div>
+"""
+    }
+
+    test = make_test(**args)
+
+    print(timeit(test, number=times))
+
+
+if __name__ == '__main__':
+    try:
+        main(int(argv[1]))
+    except IndexError:
+        main(10000)