From 13e4fe3a185e62e067265576e9288e84a5bca829 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Mon, 2 Sep 2019 03:51:21 +0200
Subject: [PATCH] testPerformance: support choosing between pprofile and
 cProfile

---
 product/ERP5Type/tests/testPerformance.py | 35 +++++++++++++++++------
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/product/ERP5Type/tests/testPerformance.py b/product/ERP5Type/tests/testPerformance.py
index d729b09ae9..7197811fbb 100644
--- a/product/ERP5Type/tests/testPerformance.py
+++ b/product/ERP5Type/tests/testPerformance.py
@@ -110,8 +110,13 @@ LISTBOX_COEF=0.00173                # 0.02472
 #   LISTBOX_COEF : 0.02472 -> 0.001725
 DO_TEST = 1
 
+# Profiler support.
 # set 1 to get profiler's result (unit_test/tests/<func_name>)
-PROFILE=0
+PROFILE = 0
+# set this to 'pprofile' to profile with pprofile ( https://github.com/vpelletier/pprofile )
+# instad of python's standard library profiler ( https://docs.python.org/2/library/profile.html )
+PROFILER = 'pprofile'
+
 
 class TestPerformanceMixin(ERP5TypeTestCase, LogInterceptor):
 
@@ -147,22 +152,34 @@ class TestPerformanceMixin(ERP5TypeTestCase, LogInterceptor):
       """
       return self.portal['bar_module']
 
-    def profile(self, func, suffix=''):
+    def profile(self, func, suffix='', args=(), kw=None):
+      """Profile `func(*args, **kw)` with selected profiler,
+      and dump output in a file called `func.__name__ + suffix`
+      """
+      if not kw:
+        kw = {}
+
+      if PROFILER == 'pprofile':
+        import pprofile
+        prof = pprofile.Profile()
+      else:
         from cProfile import Profile
-        prof_file = '%s%s' % (func.__name__, suffix)
-        try:
-            os.unlink(prof_file)
-        except OSError:
-            pass
         prof = Profile()
-        prof.runcall(func)
-        prof.dump_stats(prof_file)
+
+      prof_file = '%s%s' % (func.__name__, suffix)
+      try:
+        os.unlink(prof_file)
+      except OSError:
+        pass
+      prof.runcall(func, *args, **kw)
+      prof.dump_stats(prof_file)
 
     def beforeTearDown(self):
       # Re-enable gc at teardown.
       gc.enable()
       self.abort()
 
+
 class TestPerformance(TestPerformanceMixin):
 
     def getTitle(self):
-- 
2.30.9