aboutsummaryrefslogtreecommitdiff
path: root/wqflask/utility/benchmark.py
diff options
context:
space:
mode:
authorZachary Sloan2013-06-20 22:20:23 +0000
committerZachary Sloan2013-06-20 22:20:23 +0000
commit4ffee373494170e708678039dca132f1bd729ab1 (patch)
treea9d6054380f5a9d612a4d1d88889f68ea4923a75 /wqflask/utility/benchmark.py
parent939058c4a3b668037974f2876b072c4be008da26 (diff)
parent52ac4b6e1c014801080cbbcad53df868058d2657 (diff)
downloadgenenetwork2-4ffee373494170e708678039dca132f1bd729ab1.tar.gz
Merge branch 'flask'
Diffstat (limited to 'wqflask/utility/benchmark.py')
-rw-r--r--wqflask/utility/benchmark.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/wqflask/utility/benchmark.py b/wqflask/utility/benchmark.py
new file mode 100644
index 00000000..182187ae
--- /dev/null
+++ b/wqflask/utility/benchmark.py
@@ -0,0 +1,45 @@
+from __future__ import print_function, division, absolute_import
+
+import collections
+import inspect
+import time
+
+
+class Bench(object):
+ entries = collections.OrderedDict()
+
+ def __init__(self, name=None):
+ self.name = name
+
+ def __enter__(self):
+ if self.name:
+ print("Starting benchmark: %s" % (self.name))
+ else:
+ print("Starting benchmark at: %s [%i]" % (inspect.stack()[1][3], inspect.stack()[1][2]))
+ self.start_time = time.time()
+
+ def __exit__(self, type, value, traceback):
+ if self.name:
+ name = self.name
+ else:
+ name = "That"
+
+ time_taken = time.time() - self.start_time
+ print(" %s took: %f seconds" % (name, (time_taken)))
+
+ if self.name:
+ Bench.entries[self.name] = Bench.entries.get(self.name, 0) + time_taken
+
+
+ @classmethod
+ def report(cls):
+ total_time = sum((time_taken for time_taken in cls.entries.itervalues()))
+ print("\nTiming report\n")
+ for name, time_taken in cls.entries.iteritems():
+ percent = int(round((time_taken/total_time) * 100))
+ print("[{}%] {}: {}".format(percent, name, time_taken))
+ print()
+
+ def reset(cls):
+ """Reset the entries"""
+ cls.entries = collections.OrderedDict() \ No newline at end of file