comparison contrib/revsetbenchmarks.py @ 29210:984c4d23d39c

py3: make contrib/revsetbenchmarks.py not import symbols from stdlib modules
author Yuya Nishihara <yuya@tcha.org>
date Sat, 14 May 2016 14:23:04 +0900
parents 6d7da0901a28
children e2697acd9381
comparison
equal deleted inserted replaced
29209:eccfd6500636 29210:984c4d23d39c
8 # 8 #
9 # call with --help for details 9 # call with --help for details
10 10
11 from __future__ import absolute_import, print_function 11 from __future__ import absolute_import, print_function
12 import math 12 import math
13 import optparse # cannot use argparse, python 2.7 only
13 import os 14 import os
14 import re 15 import re
16 import subprocess
15 import sys 17 import sys
16 from subprocess import (
17 CalledProcessError,
18 check_call,
19 PIPE,
20 Popen,
21 STDOUT,
22 )
23 # cannot use argparse, python 2.7 only
24 from optparse import (
25 OptionParser,
26 )
27 18
28 DEFAULTVARIANTS = ['plain', 'min', 'max', 'first', 'last', 19 DEFAULTVARIANTS = ['plain', 'min', 'max', 'first', 'last',
29 'reverse', 'reverse+first', 'reverse+last', 20 'reverse', 'reverse+first', 'reverse+last',
30 'sort', 'sort+first', 'sort+last'] 21 'sort', 'sort+first', 'sort+last']
31 22
32 def check_output(*args, **kwargs): 23 def check_output(*args, **kwargs):
33 kwargs.setdefault('stderr', PIPE) 24 kwargs.setdefault('stderr', subprocess.PIPE)
34 kwargs.setdefault('stdout', PIPE) 25 kwargs.setdefault('stdout', subprocess.PIPE)
35 proc = Popen(*args, **kwargs) 26 proc = subprocess.Popen(*args, **kwargs)
36 output, error = proc.communicate() 27 output, error = proc.communicate()
37 if proc.returncode != 0: 28 if proc.returncode != 0:
38 raise CalledProcessError(proc.returncode, ' '.join(args[0])) 29 raise subprocess.CalledProcessError(proc.returncode, ' '.join(args[0]))
39 return output 30 return output
40 31
41 def update(rev): 32 def update(rev):
42 """update the repo to a revision""" 33 """update the repo to a revision"""
43 try: 34 try:
44 check_call(['hg', 'update', '--quiet', '--check', str(rev)]) 35 subprocess.check_call(['hg', 'update', '--quiet', '--check', str(rev)])
45 check_output(['make', 'local'], 36 check_output(['make', 'local'],
46 stderr=None) # suppress output except for error/warning 37 stderr=None) # suppress output except for error/warning
47 except CalledProcessError as exc: 38 except subprocess.CalledProcessError as exc:
48 print('update to revision %s failed, aborting'%rev, file=sys.stderr) 39 print('update to revision %s failed, aborting'%rev, file=sys.stderr)
49 sys.exit(exc.returncode) 40 sys.exit(exc.returncode)
50 41
51 42
52 def hg(cmd, repo=None): 43 def hg(cmd, repo=None):
58 if repo is not None: 49 if repo is not None:
59 fullcmd += ['-R', repo] 50 fullcmd += ['-R', repo]
60 fullcmd += ['--config', 51 fullcmd += ['--config',
61 'extensions.perf=' + os.path.join(contribdir, 'perf.py')] 52 'extensions.perf=' + os.path.join(contribdir, 'perf.py')]
62 fullcmd += cmd 53 fullcmd += cmd
63 return check_output(fullcmd, stderr=STDOUT) 54 return check_output(fullcmd, stderr=subprocess.STDOUT)
64 55
65 def perf(revset, target=None, contexts=False): 56 def perf(revset, target=None, contexts=False):
66 """run benchmark for this very revset""" 57 """run benchmark for this very revset"""
67 try: 58 try:
68 args = ['perfrevset', revset] 59 args = ['perfrevset', revset]
69 if contexts: 60 if contexts:
70 args.append('--contexts') 61 args.append('--contexts')
71 output = hg(args, repo=target) 62 output = hg(args, repo=target)
72 return parseoutput(output) 63 return parseoutput(output)
73 except CalledProcessError as exc: 64 except subprocess.CalledProcessError as exc:
74 print('abort: cannot run revset benchmark: %s'%exc.cmd, file=sys.stderr) 65 print('abort: cannot run revset benchmark: %s'%exc.cmd, file=sys.stderr)
75 if getattr(exc, 'output', None) is None: # no output before 2.7 66 if getattr(exc, 'output', None) is None: # no output before 2.7
76 print('(no output)', file=sys.stderr) 67 print('(no output)', file=sys.stderr)
77 else: 68 else:
78 print(exc.output, file=sys.stderr) 69 print(exc.output, file=sys.stderr)
101 92
102 def printrevision(rev): 93 def printrevision(rev):
103 """print data about a revision""" 94 """print data about a revision"""
104 sys.stdout.write("Revision ") 95 sys.stdout.write("Revision ")
105 sys.stdout.flush() 96 sys.stdout.flush()
106 check_call(['hg', 'log', '--rev', str(rev), '--template', 97 subprocess.check_call(['hg', 'log', '--rev', str(rev), '--template',
107 '{if(tags, " ({tags})")} ' 98 '{if(tags, " ({tags})")} '
108 '{rev}:{node|short}: {desc|firstline}\n']) 99 '{rev}:{node|short}: {desc|firstline}\n'])
109 100
110 def idxwidth(nbidx): 101 def idxwidth(nbidx):
111 """return the max width of number used for index 102 """return the max width of number used for index
112 103
113 This is similar to log10(nbidx), but we use custom code here 104 This is similar to log10(nbidx), but we use custom code here
213 204
214 def getrevs(spec): 205 def getrevs(spec):
215 """get the list of rev matched by a revset""" 206 """get the list of rev matched by a revset"""
216 try: 207 try:
217 out = check_output(['hg', 'log', '--template={rev}\n', '--rev', spec]) 208 out = check_output(['hg', 'log', '--template={rev}\n', '--rev', spec])
218 except CalledProcessError as exc: 209 except subprocess.CalledProcessError as exc:
219 print("abort, can't get revision from %s"%spec, file=sys.stderr) 210 print("abort, can't get revision from %s"%spec, file=sys.stderr)
220 sys.exit(exc.returncode) 211 sys.exit(exc.returncode)
221 return [r for r in out.split() if r] 212 return [r for r in out.split() if r]
222 213
223 214
232 different revisions in your mercurial repository. After the benchmark are run 223 different revisions in your mercurial repository. After the benchmark are run
233 summary output is provided. Use it to demonstrate speed improvements or pin 224 summary output is provided. Use it to demonstrate speed improvements or pin
234 point regressions. Revsets to run are specified in a file (or from stdin), one 225 point regressions. Revsets to run are specified in a file (or from stdin), one
235 revsets per line. Line starting with '#' will be ignored, allowing insertion of 226 revsets per line. Line starting with '#' will be ignored, allowing insertion of
236 comments.""" 227 comments."""
237 parser = OptionParser(usage="usage: %prog [options] <revs>", 228 parser = optparse.OptionParser(usage="usage: %prog [options] <revs>",
238 description=helptext) 229 description=helptext)
239 parser.add_option("-f", "--file", 230 parser.add_option("-f", "--file",
240 help="read revset from FILE (stdin if omitted)", 231 help="read revset from FILE (stdin if omitted)",
241 metavar="FILE") 232 metavar="FILE")
242 parser.add_option("-R", "--repo", 233 parser.add_option("-R", "--repo",
243 help="run benchmark on REPO", metavar="REPO") 234 help="run benchmark on REPO", metavar="REPO")