OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Calculate reasonable timeout for each step as analysed by the actual runtimes |
| 7 on the Swarming server. |
| 8 """ |
| 9 |
| 10 import Queue |
| 11 import argparse |
| 12 import json |
| 13 import os |
| 14 import subprocess |
| 15 import sys |
| 16 import threading |
| 17 import time |
| 18 import urllib |
| 19 |
| 20 |
| 21 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 22 |
| 23 |
| 24 def human_int(s): |
| 25 """Returns human readable time rounded to the second.""" |
| 26 s = int(round(s)) |
| 27 if s <= 60: |
| 28 return '%ds' % s |
| 29 m = s/60 |
| 30 if m <= 60: |
| 31 return '%dm%02ds' % (m, s%60) |
| 32 return '%dh%02dm%02ds' % (m/60, m%60, s%60) |
| 33 |
| 34 |
| 35 def human(s): |
| 36 """Returns human readable time rounded to the tenth of second.""" |
| 37 if s <= 60: |
| 38 return '%.1fs' % s |
| 39 m = int(round(s/60)) |
| 40 if m <= 60: |
| 41 return '%dm%04.1fs' % (m, s%60) |
| 42 return '%dh%02dm%04.1fs' % (m/60, m%60, s%60) |
| 43 |
| 44 |
| 45 class Stats(object): |
| 46 """Holds runtimes statistics for a step run on a builder.""" |
| 47 def __init__(self, builder, step, durations): |
| 48 self.builder = builder |
| 49 self.step = step |
| 50 self.durations = durations |
| 51 self.avg = sum(durations) / float(len(durations)) |
| 52 self.len = len(durations) |
| 53 self.max = max(durations) |
| 54 self.timeout = max(120, int(round(self.max / 60.)) * 120) |
| 55 |
| 56 def __str__(self): |
| 57 return 'avg: %4ds max: %4ds timeout: %4ds' % ( |
| 58 round(self.avg), round(self.max), self.timeout) |
| 59 |
| 60 |
| 61 class Pool(object): |
| 62 def __init__(self, size): |
| 63 self._durations = [] |
| 64 self._inputs = Queue.Queue() |
| 65 self._lock = threading.Lock() |
| 66 self._outputs = [] |
| 67 self._start = time.time() |
| 68 self._total = 0 |
| 69 self._threads = [ |
| 70 threading.Thread(name=str(i), target=self._run) for i in xrange(size) |
| 71 ] |
| 72 for t in self._threads: |
| 73 t.start() |
| 74 |
| 75 def put(self, f): |
| 76 self._inputs.put(f) |
| 77 with self._lock: |
| 78 self._total += 1 |
| 79 |
| 80 def join(self): |
| 81 for _ in xrange(len(self._threads)): |
| 82 self._inputs.put(None) |
| 83 try: |
| 84 for t in self._threads: |
| 85 while t.isAlive(): |
| 86 t.join(0.1) |
| 87 self._print_eta() |
| 88 except KeyboardInterrupt: |
| 89 sys.stderr.write('\nInterrupted!\n') |
| 90 with self._lock: |
| 91 return self._outputs[:] |
| 92 |
| 93 def _print_eta(self): |
| 94 elapsed = human(time.time() - self._start) |
| 95 with self._lock: |
| 96 out = '\r%d/%d Elapsed: %s' % (len(self._outputs), self._total, elapsed) |
| 97 if self._durations: |
| 98 avg = sum(self._durations) / float(len(self._durations)) |
| 99 rem = self._total - len(self._outputs) |
| 100 eta = avg * rem / float(len(self._threads)) |
| 101 out += ' ETA: %s ' % human_int(eta) |
| 102 sys.stderr.write(out) |
| 103 sys.stderr.flush() |
| 104 |
| 105 def _run(self): |
| 106 while True: |
| 107 f = self._inputs.get() |
| 108 if not f: |
| 109 return |
| 110 s = time.time() |
| 111 o = f() |
| 112 e = time.time() - s |
| 113 with self._lock: |
| 114 self._durations.append(e) |
| 115 self._outputs.append(o) |
| 116 |
| 117 |
| 118 def query(server, number, builder, step): |
| 119 q = 'tasks/list?%s' % urllib.urlencode([ |
| 120 ('tags', 'buildername:%s' % builder), |
| 121 ('tags', 'name:%s' % step), |
| 122 ]) |
| 123 cmd = [ |
| 124 sys.executable, '../../tools/swarming_client/swarming.py', 'query', |
| 125 '-S', server, '--limit', str(number), q, |
| 126 ] |
| 127 out = subprocess.check_output(cmd, stderr=subprocess.PIPE) |
| 128 try: |
| 129 data = json.loads(out) |
| 130 except ValueError: |
| 131 sys.stderr.write(out) |
| 132 return None |
| 133 if not 'items' in data: |
| 134 # No task with this pattern. |
| 135 return None |
| 136 durations = [i['duration'] for i in data['items'] if i.get('duration')] |
| 137 if not durations: |
| 138 # There was tasks but none completed correctly, i.e. internal_failure. |
| 139 return None |
| 140 return Stats(builder, step, durations) |
| 141 |
| 142 |
| 143 def extract_tags(data, test_name): |
| 144 """Returns all the tags that should be queried from a json file.""" |
| 145 out = [] |
| 146 for b, d in sorted(data.iteritems()): |
| 147 if not 'gtest_tests' in d: |
| 148 continue |
| 149 for t in d['gtest_tests']: |
| 150 if not t.get('swarming', {}).get('can_use_on_swarming_builders'): |
| 151 continue |
| 152 if test_name and t['test'] != test_name: |
| 153 continue |
| 154 out.append((b, t['test'])) |
| 155 return out |
| 156 |
| 157 |
| 158 def query_server(server, number, data): |
| 159 """Query the Swarming server to steps durations.""" |
| 160 def _get_func(builder, step): |
| 161 return lambda: query(server, number, builder, step) |
| 162 # Limit to 256 threads, otherwise some OSes have trouble with it. |
| 163 p = Pool(min(len(data), 256)) |
| 164 for builder, step in data: |
| 165 p.put(_get_func(builder, step)) |
| 166 return p.join() |
| 167 |
| 168 |
| 169 def main(): |
| 170 os.chdir(THIS_DIR) |
| 171 parser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__) |
| 172 parser.add_argument( |
| 173 '-f', metavar='chromium.foo.json', help='file to open', required=True) |
| 174 parser.add_argument('-s', metavar='foo_unittest', help='step to process') |
| 175 parser.add_argument( |
| 176 '-N', metavar='200', default=200, type=int, |
| 177 help='number of executions to look at') |
| 178 parser.add_argument( |
| 179 '-S', metavar='chromium-swarm.appspot.com', |
| 180 default='chromium-swarm.appspot.com', help='server to use') |
| 181 args = parser.parse_args() |
| 182 |
| 183 with open(args.f) as f: |
| 184 d = json.load(f) |
| 185 tags = extract_tags(d, args.s) |
| 186 if not tags: |
| 187 print('No step to process found') |
| 188 return 1 |
| 189 out = [i for i in query_server(args.S, args.N, tags) if i] |
| 190 print('') |
| 191 maxbuilder = max(len(i.builder) for i in out) |
| 192 maxstep = max(len(i.step) for i in out) |
| 193 for i in sorted(out, key=lambda i: (i.builder, i.step)): |
| 194 print('%-*s / %-*s %s' % (maxbuilder, i.builder, maxstep, i.step, i)) |
| 195 return 0 |
| 196 |
| 197 |
| 198 if __name__ == "__main__": |
| 199 sys.exit(main()) |
OLD | NEW |