browse.py: allow port override via environment variable
[platform/upstream/ninja.git] / misc / measure.py
1 #!/usr/bin/env python
2
3 # Copyright 2011 Google Inc. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """measure the runtime of a command by repeatedly running it.
18 """
19
20 import time
21 import subprocess
22 import sys
23
24 devnull = open('/dev/null', 'w')
25
26 def run(cmd, repeat=10):
27     print 'sampling:',
28     sys.stdout.flush()
29
30     samples = []
31     for _ in range(repeat):
32         start = time.time()
33         subprocess.call(cmd, stdout=devnull, stderr=devnull)
34         end = time.time()
35         dt = (end - start) * 1000
36         print '%dms' % int(dt),
37         sys.stdout.flush()
38         samples.append(dt)
39     print
40
41     # We're interested in the 'pure' runtime of the code, which is
42     # conceptually the smallest time we'd see if we ran it enough times
43     # such that it got the perfect time slices / disk cache hits.
44     best = min(samples)
45     # Also print how varied the outputs were in an attempt to make it
46     # more obvious if something has gone terribly wrong.
47     err = sum(s - best for s in samples) / float(len(samples))
48     print 'estimate: %dms (mean err %.1fms)' % (best, err)
49
50 if __name__ == '__main__':
51     if len(sys.argv) < 2:
52         print 'usage: measure.py command args...'
53         sys.exit(1)
54     run(cmd=sys.argv[1:])