contrib/hgperf
author Julien Cristau <jcristau@debian.org>
Fri, 26 Jul 2024 10:52:28 +0200
branchstable
changeset 51665 63ede7a43a37
parent 46819 d4ba4d51f85f
permissions -rwxr-xr-x
demandimport: don't delay threading import A recent cpython change breaks demandimport by importing threading locally in importlib.util.LazyLoader.exec_module; add it (plus warnings and _weakrefset, which are imported by threading) to demandimport's ignore list. ``` Traceback (most recent call last): File "/usr/bin/hg", line 57, in <module> from mercurial import dispatch File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "/usr/lib/python3/dist-packages/hgdemandimport/demandimportpy3.py", line 52, in exec_module super().exec_module(module) File "<frozen importlib.util>", line 257, in exec_module File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "/usr/lib/python3/dist-packages/hgdemandimport/demandimportpy3.py", line 52, in exec_module super().exec_module(module) File "<frozen importlib.util>", line 267, in exec_module AttributeError: partially initialized module 'threading' has no attribute 'RLock' (most likely due to a circular import) ``` Ref: https://github.com/python/cpython/issues/117983 https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1076449 https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1076747
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45849
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43703
diff changeset
     1
#!/usr/bin/env python3
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     2
#
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     3
# hgperf - measure performance of Mercurial commands
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     4
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 45849
diff changeset
     5
# Copyright 2014 Olivia Mackall <olivia@selenic.com>
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     6
#
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     7
# This software may be used and distributed according to the terms of the
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     8
# GNU General Public License version 2 or any later version.
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     9
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    10
'''measure performance of Mercurial commands
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    11
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    12
Using ``hgperf`` instead of ``hg`` measures performance of the target
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    13
Mercurial command. For example, the execution below measures
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    14
performance of :hg:`heads --topo`::
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    15
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    16
    $ hgperf heads --topo
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    17
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    18
All command output via ``ui`` is suppressed, and just measurement
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    19
result is displayed: see also "perf" extension in "contrib".
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    20
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    21
Costs of processing before dispatching to the command function like
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    22
below are not measured::
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    23
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    24
    - parsing command line (e.g. option validity check)
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    25
    - reading configuration files in
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    26
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    27
But ``pre-`` and ``post-`` hook invocation for the target command is
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    28
measured, even though these are invoked before or after dispatching to
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    29
the command function, because these may be required to repeat
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    30
execution of the target command correctly.
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    31
'''
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    32
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    33
import os
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    34
import sys
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    35
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    36
libdir = '@LIBDIR@'
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    37
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    38
if libdir != '@' 'LIBDIR' '@':
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    39
    if not os.path.isabs(libdir):
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    40
        libdir = os.path.join(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    41
            os.path.dirname(os.path.realpath(__file__)), libdir
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    42
        )
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    43
        libdir = os.path.abspath(libdir)
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    44
    sys.path.insert(0, libdir)
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    45
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    46
# enable importing on demand to reduce startup time
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    47
try:
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    48
    from mercurial import demandimport
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    49
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    50
    demandimport.enable()
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    51
except ImportError:
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    52
    import sys
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    53
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    54
    sys.stderr.write(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    55
        "abort: couldn't find mercurial libraries in [%s]\n"
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    56
        % ' '.join(sys.path)
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    57
    )
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    58
    sys.stderr.write("(check your install and PYTHONPATH)\n")
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    59
    sys.exit(-1)
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    60
33910
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
    61
from mercurial import (
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
    62
    dispatch,
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
    63
    util,
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
    64
)
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    65
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    66
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    67
def timer(func, title=None):
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    68
    results = []
33910
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
    69
    begin = util.timer()
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    70
    count = 0
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    71
    while True:
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    72
        ostart = os.times()
33910
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
    73
        cstart = util.timer()
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    74
        r = func()
33910
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
    75
        cstop = util.timer()
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    76
        ostop = os.times()
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    77
        count += 1
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    78
        a, b = ostart, ostop
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    79
        results.append((cstop - cstart, b[0] - a[0], b[1] - a[1]))
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    80
        if cstop - begin > 3 and count >= 100:
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    81
            break
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    82
        if cstop - begin > 10 and count >= 3:
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    83
            break
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    84
    if title:
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    85
        sys.stderr.write("! %s\n" % title)
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    86
    if r:
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    87
        sys.stderr.write("! result: %s\n" % r)
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    88
    m = min(results)
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    89
    sys.stderr.write(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    90
        "! wall %f comb %f user %f sys %f (best of %d)\n"
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    91
        % (m[0], m[1] + m[2], m[1], m[2], count)
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    92
    )
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    93
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    94
33910
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
    95
orgruncommand = dispatch.runcommand
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    96
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
    97
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    98
def runcommand(lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions):
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    99
    ui.pushbuffer()
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   100
    lui.pushbuffer()
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
   101
    timer(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
   102
        lambda: orgruncommand(
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
   103
            lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
   104
        )
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
   105
    )
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   106
    ui.popbuffer()
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   107
    lui.popbuffer()
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   108
43703
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
   109
33910
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
   110
dispatch.runcommand = runcommand
20839
377a111d1cd2 contrib: add "hgperf" command to measure performance of commands easily
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   111
33910
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30995
diff changeset
   112
dispatch.run()