comparison contrib/perf.py @ 40680:9d88ae5c635b

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.
author Boris Feld <boris.feld@octobus.net>
date Mon, 19 Nov 2018 22:55:07 +0000
parents 20d2fd6036ed
children c0a1686d9391
comparison
equal deleted inserted replaced
40679:20d2fd6036ed 40680:9d88ae5c635b
273 273
274 # experimental config: perf.all-timing 274 # experimental config: perf.all-timing
275 displayall = ui.configbool(b"perf", b"all-timing", False) 275 displayall = ui.configbool(b"perf", b"all-timing", False)
276 return functools.partial(_timer, fm, displayall=displayall), fm 276 return functools.partial(_timer, fm, displayall=displayall), fm
277 277
278 def stub_timer(fm, func, title=None): 278 def stub_timer(fm, func, setup=None, title=None):
279 func() 279 func()
280 280
281 @contextlib.contextmanager 281 @contextlib.contextmanager
282 def timeone(): 282 def timeone():
283 r = [] 283 r = []
287 cstop = util.timer() 287 cstop = util.timer()
288 ostop = os.times() 288 ostop = os.times()
289 a, b = ostart, ostop 289 a, b = ostart, ostop
290 r.append((cstop - cstart, b[0] - a[0], b[1]-a[1])) 290 r.append((cstop - cstart, b[0] - a[0], b[1]-a[1]))
291 291
292 def _timer(fm, func, title=None, displayall=False): 292 def _timer(fm, func, setup=None, title=None, displayall=False):
293 gc.collect() 293 gc.collect()
294 results = [] 294 results = []
295 begin = util.timer() 295 begin = util.timer()
296 count = 0 296 count = 0
297 while True: 297 while True:
298 if setup is not None:
299 setup()
298 with timeone() as item: 300 with timeone() as item:
299 r = func() 301 r = func()
300 count += 1 302 count += 1
301 results.append(item[0]) 303 results.append(item[0])
302 cstop = util.timer() 304 cstop = util.timer()