[PATCH 2 of 8] perf: add a `setup` argument to run code outside of the timed section

Boris Feld boris.feld at octobus.net
Wed Nov 21 13:33:52 EST 2018


# HG changeset patch
# User Boris Feld <boris.feld at octobus.net>
# Date 1542668107 0
#      Mon Nov 19 22:55:07 2018 +0000
# Node ID 04a1a32aaaedc6a75117994dbf2823e39204fd7d
# Parent  954159d40a5f4bf43465c50cc97261ea139f0032
# EXP-Topic perf-ignore
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 04a1a32aaaed
perf: add a `setup` argument to run code outside of the timed section

With this new argument, it is possible to perform special setup and cleanup
outside of code actually timed. This is useful to provide more accurate
benchmark.

diff --git a/contrib/perf.py b/contrib/perf.py
--- a/contrib/perf.py
+++ b/contrib/perf.py
@@ -275,7 +275,7 @@ def gettimer(ui, opts=None):
     displayall = ui.configbool(b"perf", b"all-timing", False)
     return functools.partial(_timer, fm, displayall=displayall), fm
 
-def stub_timer(fm, func, title=None):
+def stub_timer(fm, func, setup=None, title=None):
     func()
 
 @contextlib.contextmanager
@@ -289,12 +289,14 @@ def timeone():
     a, b = ostart, ostop
     r.append((cstop - cstart, b[0] - a[0], b[1]-a[1]))
 
-def _timer(fm, func, title=None, displayall=False):
+def _timer(fm, func, setup=None, title=None, displayall=False):
     gc.collect()
     results = []
     begin = util.timer()
     count = 0
     while True:
+        if setup is not None:
+            setup()
         with timeone() as item:
             r = func()
         count += 1


More information about the Mercurial-devel mailing list