[PATCH 5 of 8] perf: extract the timing of a section in a context manager
Boris Feld
boris.feld at octobus.net
Thu Oct 11 04:01:48 EDT 2018
# HG changeset patch
# User Boris Feld <boris.feld at octobus.net>
# Date 1538499246 -7200
# Tue Oct 02 18:54:06 2018 +0200
# Node ID 236a33954a26bae0735388b2f7a34fa8d8606195
# Parent 532b680ff96db665fd5aa1559f487221cba92c20
# EXP-Topic revlog-perf
# Available At https://bitbucket.org/octobus/mercurial-devel/
# hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 236a33954a26
perf: extract the timing of a section in a context manager
This makes it easier to reuse it in other (future) part of the code that
requires their own time management.
diff --git a/contrib/perf.py b/contrib/perf.py
--- a/contrib/perf.py
+++ b/contrib/perf.py
@@ -19,6 +19,7 @@
# Mercurial
from __future__ import absolute_import
+import contextlib
import functools
import gc
import os
@@ -273,20 +274,28 @@ def gettimer(ui, opts=None):
def stub_timer(fm, func, title=None):
func()
+ at contextlib.contextmanager
+def timeone():
+ r = []
+ ostart = os.times()
+ cstart = util.timer()
+ yield r
+ cstop = util.timer()
+ ostop = os.times()
+ a, b = ostart, ostop
+ r.append((cstop - cstart, b[0] - a[0], b[1]-a[1]))
+
def _timer(fm, func, title=None, displayall=False):
gc.collect()
results = []
begin = util.timer()
count = 0
while True:
- ostart = os.times()
- cstart = util.timer()
- r = func()
+ with timeone() as item:
+ r = func()
+ count += 1
+ results.append(item[0])
cstop = util.timer()
- ostop = os.times()
- count += 1
- a, b = ostart, ostop
- results.append((cstop - cstart, b[0] - a[0], b[1]-a[1]))
if cstop - begin > 3 and count >= 100:
break
if cstop - begin > 10 and count >= 3:
More information about the Mercurial-devel
mailing list