[ARVADOS] created: 24fa04e6fee7a6cb94910b0ae93325816b1b2970
git at public.curoverse.com
git at public.curoverse.com
Mon Jun 22 16:41:34 EDT 2015
at 24fa04e6fee7a6cb94910b0ae93325816b1b2970 (commit)
commit 24fa04e6fee7a6cb94910b0ae93325816b1b2970
Author: Tom Clegg <tom at curoverse.com>
Date: Mon Jun 22 16:41:22 2015 -0400
6218: Refactor profiler as a decorator
diff --git a/sdk/python/tests/performance/performance_profiler.py b/sdk/python/tests/performance/performance_profiler.py
index 3810f92..89268ce 100644
--- a/sdk/python/tests/performance/performance_profiler.py
+++ b/sdk/python/tests/performance/performance_profiler.py
@@ -14,22 +14,29 @@
# ./run-tests.sh WORKSPACE=~/arvados --only sdk/python sdk/python_test="--test-suite=tests.performance"
#
+import functools
import os
-import unittest
+import pstats
import sys
-from datetime import datetime
+import unittest
try:
import cProfile as profile
except ImportError:
import profile
-class PerformanceProfiler(unittest.TestCase):
- def run_profiler(self, function, test_name):
- filename = os.getcwd()+'/tmp/performance/'+ datetime.now().strftime('%Y-%m-%d-%H-%M-%S') +'-' +test_name
-
- directory = os.path.dirname(filename)
- if not os.path.exists(directory):
- os.makedirs(directory)
+output_dir = os.path.abspath(os.path.join('tmp', 'performance'))
+if not os.path.exists(output_dir):
+ os.makedirs(output_dir)
- sys.stdout = open(filename, 'w')
- profile.runctx(function, globals(), locals())
+def profiled(function):
+ @functools.wraps(function)
+ def profiled_function(*args, **kwargs):
+ out_base = os.path.join(output_dir, function.__name__)
+ pr = profile.Profile()
+ pr.enable()
+ ret = function(*args, **kwargs)
+ pr.disable()
+ ps = pstats.Stats(pr, stream=open(out_base+".txt","w"))
+ ps.print_stats()
+ return ret
+ return profiled_function
diff --git a/sdk/python/tests/performance/test_a_sample.py b/sdk/python/tests/performance/test_a_sample.py
index aa35813..4cac3fd 100644
--- a/sdk/python/tests/performance/test_a_sample.py
+++ b/sdk/python/tests/performance/test_a_sample.py
@@ -1,10 +1,11 @@
import unittest
-from performance_profiler import PerformanceProfiler
+from performance_profiler import profiled
-class PerformanceTestSample(PerformanceProfiler):
- def func(self):
+class PerformanceTestSample(unittest.TestCase):
+ @profiled
+ def test_profiled_decorator(self):
+ j = 0
+ for i in range(0,2**20):
+ j += i
print 'Hello'
-
- def test_performance(self):
- self.run_profiler('self.func()', 'test_sample')
-----------------------------------------------------------------------
hooks/post-receive
--
More information about the arvados-commits
mailing list