changeset 50676:28620be88da9

perf: add a new "context" argument to timer This allow to simple setup/teardown outside of the timed section. Especially using object that need context manager, like a temporary files.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 12 Jun 2023 18:04:09 +0200
parents 3ce370a00225
children b8de54ac5a21
files contrib/perf.py
diffstat 1 files changed, 12 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/perf.py	Tue Jun 06 01:48:10 2023 +0200
+++ b/contrib/perf.py	Mon Jun 12 18:04:09 2023 +0200
@@ -532,10 +532,16 @@
 )
 
 
+@contextlib.contextmanager
+def noop_context():
+    yield
+
+
 def _timer(
     fm,
     func,
     setup=None,
+    context=noop_context,
     title=None,
     displayall=False,
     limits=DEFAULTLIMITS,
@@ -551,14 +557,16 @@
     for i in range(prerun):
         if setup is not None:
             setup()
-        func()
+        with context():
+            func()
     keepgoing = True
     while keepgoing:
         if setup is not None:
             setup()
-        with profiler:
-            with timeone() as item:
-                r = func()
+        with context():
+            with profiler:
+                with timeone() as item:
+                    r = func()
         profiler = NOOPCTX
         count += 1
         results.append(item[0])