contrib/hgperf
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 46819 d4ba4d51f85f
permissions -rwxr-xr-x
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43659
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: 45830
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):
43659
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:
43659
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
43659
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
33892
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
diff changeset
    61
from mercurial import (
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
diff changeset
    62
    dispatch,
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
diff changeset
    63
    util,
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
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
43659
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 = []
33892
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
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()
33892
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
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()
33892
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
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
43659
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)
43659
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
33892
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
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
43659
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()
43659
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
43659
99e231afc29c black: blacken scripts
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34533
diff changeset
   109
33892
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
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
33892
78f644fdaa2a hgperf: update to conform with import style checks
Augie Fackler <raf@durin42.com>
parents: 30975
diff changeset
   112
dispatch.run()