Merge pull request #4601 from mwichmann/doc/gettext
[scons.git] / bin / calibrate.py
blobd18cfcd85d0454a644808ff5523b026d767e2afa
1 #!/usr/bin/env python
3 # Copyright (c) 2009 The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 import optparse
24 import os
25 import re
26 import subprocess
27 import sys
29 variable_re = re.compile(r'^VARIABLE: (.*)$', re.M)
30 elapsed_re = re.compile(r'^ELAPSED: (.*)$', re.M)
33 def main(argv=None):
34 if argv is None:
35 argv = sys.argv
37 parser = optparse.OptionParser(usage="calibrate.py [-h] [-p PACKAGE], [--min time] [--max time] timings/*/*-run.py")
38 parser.add_option('--min', type='float', default=9.5,
39 help="minimum acceptable execution time (default 9.5)")
40 parser.add_option('--max', type='float', default=10.00,
41 help="maximum acceptable execution time (default 10.00)")
42 parser.add_option('-p', '--package', type="string",
43 help="package type")
44 opts, args = parser.parse_args(argv[1:])
46 os.environ['TIMESCONS_CALIBRATE'] = '1'
48 for arg in args:
49 if len(args) > 1:
50 print(arg + ':')
52 command = [sys.executable, 'runtest.py']
53 if opts.package:
54 command.extend(['-p', opts.package])
55 command.append(arg)
57 run = 1
58 good = 0
59 while good < 3:
60 p = subprocess.Popen(command,
61 stdout=subprocess.PIPE,
62 stderr=subprocess.STDOUT,
63 universal_newlines=True)
64 output = p.communicate()[0]
65 vm = variable_re.search(output)
66 em = elapsed_re.search(output)
67 try:
68 elapsed = float(em.group(1))
69 except AttributeError:
70 print(output)
71 raise
72 print("run %3d: %7.3f: %s" % (run, elapsed, ' '.join(vm.groups())))
73 if opts.min < elapsed < opts.max:
74 good += 1
75 else:
76 good = 0
77 for v in vm.groups():
78 var, value = v.split('=', 1)
79 # TODO: this sometimes converges slowly, better algorithm?
80 value = int((int(value) * opts.max) // elapsed)
81 os.environ[var] = str(value)
82 run += 1
84 return 0
87 if __name__ == "__main__":
88 sys.exit(main())