tests/check-perf-code.py
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Sun, 09 Oct 2016 01:03:18 +0900
changeset 30146 148ccd1d9f2f
parent 30144 14031d183048
child 30149 d8a2c536dd96
permissions -rwxr-xr-x
perf: add functions to get vfs-like object for Mercurial earlier than 2.3 Before this patch, using svfs prevents perf.py from measuring performance of Mercurial earlier than 2.3 (or 7034365089bf), because svfs isn't available in such Mercurial, even though there are some code paths for Mercurial earlier than 2.3 in perf.py. For example, setting "_prereadsize" attribute in perfindex() and perfnodelookup() is effective only with hg earlier than 1.8 (or 61c9bc3da402). To get appropriate vfs-like object to access files under .hg/store, this patch adds getsvfs() (and also getvfs(), for future use). To avoid examining existence of attribute at each repetition while measuring performance, getsvfs() is invoked outside the function to be called repeatedly. This patch also adds check-perf-code.py an extra check entry to detect direct usage of repo.(vfs|svfs|opener|sopener) in perf.py.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29570
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     1
#!/usr/bin/env python
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     2
#
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     3
# check-perf-code - (historical) portability checker for contrib/perf.py
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     4
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     5
from __future__ import absolute_import
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     6
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     7
import os
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     8
import sys
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     9
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    10
# write static check patterns here
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    11
perfpypats = [
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    12
  [
30144
14031d183048 perf: get subsettable from appropriate module for Mercurial earlier than 2.9
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29571
diff changeset
    13
    (r'(branchmap|repoview)\.subsettable',
14031d183048 perf: get subsettable from appropriate module for Mercurial earlier than 2.9
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29571
diff changeset
    14
     "use getbranchmapsubsettable() for early Mercurial"),
30146
148ccd1d9f2f perf: add functions to get vfs-like object for Mercurial earlier than 2.3
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30144
diff changeset
    15
    (r'\.(vfs|svfs|opener|sopener)',
148ccd1d9f2f perf: add functions to get vfs-like object for Mercurial earlier than 2.3
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30144
diff changeset
    16
     "use getvfs()/getsvfs() for early Mercurial"),
29570
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    17
  ],
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    18
  # warnings
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    19
  [
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    20
  ]
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    21
]
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    22
29571
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    23
def modulewhitelist(names):
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    24
    replacement = [('.py', ''), ('.c', ''), # trim suffix
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    25
                   ('mercurial%s' % (os.sep), ''), # trim "mercurial/" path
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    26
                  ]
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    27
    ignored = set(['__init__'])
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    28
    modules = {}
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    29
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    30
    # convert from file name to module name, and count # of appearances
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    31
    for name in names:
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    32
        name = name.strip()
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    33
        for old, new in replacement:
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    34
            name = name.replace(old, new)
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    35
        if name not in ignored:
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    36
            modules[name] = modules.get(name, 0) + 1
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    37
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    38
    # list up module names, which appear multiple times
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    39
    whitelist = []
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    40
    for name, count in modules.items():
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    41
        if count > 1:
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    42
            whitelist.append(name)
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    43
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    44
    return whitelist
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    45
29570
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    46
if __name__ == "__main__":
29571
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    47
    # in this case, it is assumed that result of "hg files" at
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    48
    # multiple revisions is given via stdin
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    49
    whitelist = modulewhitelist(sys.stdin)
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    50
    assert whitelist, "module whitelist is empty"
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    51
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    52
    # build up module whitelist check from file names given at runtime
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    53
    perfpypats[0].append(
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    54
        # this matching pattern assumes importing modules from
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    55
        # "mercurial" package in the current style below, for simplicity
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    56
        #
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    57
        #    from mercurial import (
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    58
        #        foo,
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    59
        #        bar,
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    60
        #        baz
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    61
        #    )
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    62
        ((r'from mercurial import [(][a-z0-9, \n#]*\n(?! *%s,|^[ #]*\n|[)])'
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    63
          % ',| *'.join(whitelist)),
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    64
         "import newer module separately in try clause for early Mercurial"
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    65
         ))
d1a7d9c279bb tests: check importing modules in perf.py for historical portability
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29570
diff changeset
    66
29570
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    67
    # import contrib/check-code.py as checkcode
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    68
    assert 'RUNTESTDIR' in os.environ, "use check-perf-code.py in *.t script"
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    69
    contribpath = os.path.join(os.environ['RUNTESTDIR'], '..', 'contrib')
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    70
    sys.path.insert(0, contribpath)
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    71
    checkcode = __import__('check-code')
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    72
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    73
    # register perf.py specific entry with "checks" in check-code.py
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    74
    checkcode.checks.append(('perf.py', r'contrib/perf.py$', '',
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    75
                             checkcode.pyfilters, perfpypats))
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    76
cbd240188e4e tests: introduce check-perf-code.py to add extra checks on perf.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    77
    sys.exit(checkcode.main())