sapling/contrib/hggitperf.py
Jun Wu 9dc21f8d0b codemod: import from the edenscm package
Summary:
D13853115 adds `edenscm/` to `sys.path` and code still uses `import mercurial`.
That has nasty problems if both `import mercurial` and
`import edenscm.mercurial` are used, because Python would think `mercurial.foo`
and `edenscm.mercurial.foo` are different modules so code like
`try: ... except mercurial.error.Foo: ...`, or `isinstance(x, mercurial.foo.Bar)`
would fail to handle the `edenscm.mercurial` version. There are also some
module-level states (ex. `extensions._extensions`) that would cause trouble if
they have multiple versions in a single process.

Change imports to use the `edenscm` so ideally the `mercurial` is no longer
imported at all. Add checks in extensions.py to catch unexpected extensions
importing modules from the old (wrong) locations when running tests.

Reviewed By: phillco

Differential Revision: D13868981

fbshipit-source-id: f4e2513766957fd81d85407994f7521a08e4de48
2019-01-29 17:25:32 -08:00

91 lines
2.3 KiB
Python

# hggitperf.py - performance test routines
"""helper extension to measure performance of hg-git operations
This requires both the hggit and hggitperf extensions to be enabled and
available.
"""
import functools
import os
import tempfile
import time
from edenscm.mercurial import cmdutil
cmdtable = {}
command = cmdutil.command(cmdtable)
# the timer functions are copied from mercurial/contrib/perf.py
def gettimer(ui, opts=None):
"""return a timer function and formatter: (timer, formatter)
This functions exist to gather the creation of formatter in a single
place instead of duplicating it in all performance command."""
# enforce an idle period before execution to counteract power management
time.sleep(ui.configint("perf", "presleep", 1))
if opts is None:
opts = {}
# redirect all to stderr
ui = ui.copy()
ui.fout = ui.ferr
# get a formatter
fm = ui.formatter("perf", opts)
return functools.partial(_timer, fm), fm
def _timer(fm, func, title=None):
results = []
begin = time.time()
count = 0
while True:
ostart = os.times()
cstart = time.time()
r = func()
cstop = time.time()
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:
break
fm.startitem()
if title:
fm.write("title", "! %s\n", title)
if r:
fm.write("result", "! result: %s\n", r)
m = min(results)
fm.plain("!")
fm.write("wall", " wall %f", m[0])
fm.write("comb", " comb %f", m[1] + m[2])
fm.write("user", " user %f", m[1])
fm.write("sys", " sys %f", m[2])
fm.write("count", " (best of %d)", count)
fm.plain("\n")
@command("perfgitloadmap")
def perfgitloadmap(ui, repo):
timer, fm = gettimer(ui)
timer(repo.githandler.load_map)
fm.end()
@command("perfgitsavemap")
def perfgitsavemap(ui, repo):
timer, fm = gettimer(ui)
repo.githandler.load_map()
fd, f = tempfile.mkstemp(prefix=".git-mapfile-", dir=repo.path)
basename = os.path.basename(f)
try:
timer(lambda: repo.githandler.save_map(basename))
finally:
os.unlink(f)
fm.end()