Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | """Run tests in parallel.""" |
| 3 | |
| 4 | import argparse |
| 5 | import glob |
| 6 | import itertools |
Craig Tiller | 261dd98 | 2015-01-16 16:41:45 -0800 | [diff] [blame] | 7 | import json |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 8 | import multiprocessing |
Craig Tiller | 1cc11db | 2015-01-15 22:50:50 -0800 | [diff] [blame] | 9 | import os |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 10 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 11 | import time |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 12 | |
| 13 | import jobset |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 14 | import watch_dirs |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 15 | |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 16 | |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 17 | # SimpleConfig: just compile with CONFIG=config, and run the binary to test |
| 18 | class SimpleConfig(object): |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 19 | |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 20 | def __init__(self, config): |
| 21 | self.build_config = config |
Craig Tiller | e68de0e | 2015-01-26 08:44:00 -0800 | [diff] [blame] | 22 | self.maxjobs = 2 * multiprocessing.cpu_count() |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 23 | self.allow_hashing = (config != 'gcov') |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 24 | |
| 25 | def run_command(self, binary): |
| 26 | return [binary] |
| 27 | |
| 28 | |
| 29 | # ValgrindConfig: compile with some CONFIG=config, but use valgrind to run |
| 30 | class ValgrindConfig(object): |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 31 | |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 32 | def __init__(self, config, tool): |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 33 | self.build_config = config |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 34 | self.tool = tool |
Craig Tiller | e68de0e | 2015-01-26 08:44:00 -0800 | [diff] [blame] | 35 | self.maxjobs = 2 * multiprocessing.cpu_count() |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 36 | self.allow_hashing = False |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 37 | |
| 38 | def run_command(self, binary): |
Craig Tiller | 2aa4d64 | 2015-01-14 15:59:44 -0800 | [diff] [blame] | 39 | return ['valgrind', binary, '--tool=%s' % self.tool] |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 40 | |
| 41 | |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 42 | class CLanguage(object): |
| 43 | |
Craig Tiller | e9c959d | 2015-01-18 10:23:26 -0800 | [diff] [blame] | 44 | def __init__(self, make_target, test_lang): |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 45 | self.allow_hashing = True |
| 46 | self.make_target = make_target |
Craig Tiller | e9c959d | 2015-01-18 10:23:26 -0800 | [diff] [blame] | 47 | with open('tools/run_tests/tests.json') as f: |
Craig Tiller | 06b4ff2 | 2015-01-18 11:01:25 -0800 | [diff] [blame] | 48 | js = json.load(f) |
murgatroid99 | 2c8d516 | 2015-01-26 10:41:21 -0800 | [diff] [blame] | 49 | self.binaries = [tgt['name'] |
| 50 | for tgt in js |
Craig Tiller | e9c959d | 2015-01-18 10:23:26 -0800 | [diff] [blame] | 51 | if tgt['language'] == test_lang] |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 52 | |
| 53 | def test_binaries(self, config): |
Craig Tiller | e9c959d | 2015-01-18 10:23:26 -0800 | [diff] [blame] | 54 | return ['bins/%s/%s' % (config, binary) for binary in self.binaries] |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 55 | |
| 56 | def make_targets(self): |
| 57 | return ['buildtests_%s' % self.make_target] |
| 58 | |
| 59 | def build_steps(self): |
| 60 | return [] |
| 61 | |
Craig Tiller | 9977582 | 2015-01-30 13:07:16 -0800 | [diff] [blame^] | 62 | |
murgatroid99 | 2c8d516 | 2015-01-26 10:41:21 -0800 | [diff] [blame] | 63 | class NodeLanguage(object): |
| 64 | |
| 65 | def __init__(self): |
| 66 | self.allow_hashing = False |
| 67 | |
| 68 | def test_binaries(self, config): |
| 69 | return ['tools/run_tests/run_node.sh'] |
| 70 | |
| 71 | def make_targets(self): |
murgatroid99 | c279165 | 2015-01-26 11:33:39 -0800 | [diff] [blame] | 72 | return ['static_c'] |
murgatroid99 | 2c8d516 | 2015-01-26 10:41:21 -0800 | [diff] [blame] | 73 | |
| 74 | def build_steps(self): |
| 75 | return [['tools/run_tests/build_node.sh']] |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 76 | |
Craig Tiller | 9977582 | 2015-01-30 13:07:16 -0800 | [diff] [blame^] | 77 | |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 78 | class PhpLanguage(object): |
| 79 | |
| 80 | def __init__(self): |
| 81 | self.allow_hashing = False |
| 82 | |
| 83 | def test_binaries(self, config): |
| 84 | return ['src/php/bin/run_tests.sh'] |
| 85 | |
| 86 | def make_targets(self): |
murgatroid99 | 564b944 | 2015-01-26 11:07:59 -0800 | [diff] [blame] | 87 | return ['static_c'] |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 88 | |
| 89 | def build_steps(self): |
| 90 | return [['tools/run_tests/build_php.sh']] |
| 91 | |
| 92 | |
Nathaniel Manista | 840615e | 2015-01-22 20:31:47 +0000 | [diff] [blame] | 93 | class PythonLanguage(object): |
| 94 | |
| 95 | def __init__(self): |
| 96 | self.allow_hashing = False |
| 97 | |
| 98 | def test_binaries(self, config): |
| 99 | return ['tools/run_tests/run_python.sh'] |
| 100 | |
| 101 | def make_targets(self): |
| 102 | return[] |
| 103 | |
| 104 | def build_steps(self): |
| 105 | return [['tools/run_tests/build_python.sh']] |
| 106 | |
| 107 | |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 108 | # different configurations we can run under |
| 109 | _CONFIGS = { |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 110 | 'dbg': SimpleConfig('dbg'), |
| 111 | 'opt': SimpleConfig('opt'), |
| 112 | 'tsan': SimpleConfig('tsan'), |
| 113 | 'msan': SimpleConfig('msan'), |
| 114 | 'asan': SimpleConfig('asan'), |
| 115 | 'gcov': SimpleConfig('gcov'), |
| 116 | 'memcheck': ValgrindConfig('valgrind', 'memcheck'), |
| 117 | 'helgrind': ValgrindConfig('dbg', 'helgrind') |
| 118 | } |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 119 | |
| 120 | |
Craig Tiller | b29797b | 2015-01-12 13:51:54 -0800 | [diff] [blame] | 121 | _DEFAULT = ['dbg', 'opt'] |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 122 | _LANGUAGES = { |
Craig Tiller | e9c959d | 2015-01-18 10:23:26 -0800 | [diff] [blame] | 123 | 'c++': CLanguage('cxx', 'c++'), |
| 124 | 'c': CLanguage('c', 'c'), |
murgatroid99 | 2c8d516 | 2015-01-26 10:41:21 -0800 | [diff] [blame] | 125 | 'node': NodeLanguage(), |
Nathaniel Manista | 840615e | 2015-01-22 20:31:47 +0000 | [diff] [blame] | 126 | 'php': PhpLanguage(), |
| 127 | 'python': PythonLanguage(), |
Craig Tiller | 686fb26 | 2015-01-15 07:39:09 -0800 | [diff] [blame] | 128 | } |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 129 | |
| 130 | # parse command line |
| 131 | argp = argparse.ArgumentParser(description='Run grpc tests.') |
| 132 | argp.add_argument('-c', '--config', |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 133 | choices=['all'] + sorted(_CONFIGS.keys()), |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 134 | nargs='+', |
Craig Tiller | b29797b | 2015-01-12 13:51:54 -0800 | [diff] [blame] | 135 | default=_DEFAULT) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 136 | argp.add_argument('-n', '--runs_per_test', default=1, type=int) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 137 | argp.add_argument('-f', '--forever', |
| 138 | default=False, |
| 139 | action='store_const', |
| 140 | const=True) |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 141 | argp.add_argument('--newline_on_success', |
| 142 | default=False, |
| 143 | action='store_const', |
| 144 | const=True) |
Craig Tiller | 686fb26 | 2015-01-15 07:39:09 -0800 | [diff] [blame] | 145 | argp.add_argument('-l', '--language', |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 146 | choices=sorted(_LANGUAGES.keys()), |
Craig Tiller | 686fb26 | 2015-01-15 07:39:09 -0800 | [diff] [blame] | 147 | nargs='+', |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 148 | default=sorted(_LANGUAGES.keys())) |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 149 | args = argp.parse_args() |
| 150 | |
| 151 | # grab config |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 152 | run_configs = set(_CONFIGS[cfg] |
| 153 | for cfg in itertools.chain.from_iterable( |
| 154 | _CONFIGS.iterkeys() if x == 'all' else [x] |
| 155 | for x in args.config)) |
| 156 | build_configs = set(cfg.build_config for cfg in run_configs) |
Craig Tiller | f1973b0 | 2015-01-16 12:32:13 -0800 | [diff] [blame] | 157 | |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 158 | make_targets = [] |
| 159 | languages = set(_LANGUAGES[l] for l in args.language) |
| 160 | build_steps = [['make', |
| 161 | '-j', '%d' % (multiprocessing.cpu_count() + 1), |
| 162 | 'CONFIG=%s' % cfg] + list(set( |
| 163 | itertools.chain.from_iterable(l.make_targets() |
| 164 | for l in languages))) |
| 165 | for cfg in build_configs] + list( |
| 166 | itertools.chain.from_iterable(l.build_steps() |
| 167 | for l in languages)) |
Craig Tiller | f1973b0 | 2015-01-16 12:32:13 -0800 | [diff] [blame] | 168 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 169 | runs_per_test = args.runs_per_test |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 170 | forever = args.forever |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 171 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 172 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 173 | class TestCache(object): |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 174 | """Cache for running tests.""" |
| 175 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 176 | def __init__(self): |
| 177 | self._last_successful_run = {} |
| 178 | |
| 179 | def should_run(self, cmdline, bin_hash): |
| 180 | cmdline = ' '.join(cmdline) |
| 181 | if cmdline not in self._last_successful_run: |
| 182 | return True |
| 183 | if self._last_successful_run[cmdline] != bin_hash: |
| 184 | return True |
| 185 | return False |
| 186 | |
| 187 | def finished(self, cmdline, bin_hash): |
| 188 | self._last_successful_run[' '.join(cmdline)] = bin_hash |
| 189 | |
| 190 | def dump(self): |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 191 | return [{'cmdline': k, 'hash': v} |
| 192 | for k, v in self._last_successful_run.iteritems()] |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 193 | |
| 194 | def parse(self, exdump): |
| 195 | self._last_successful_run = dict((o['cmdline'], o['hash']) for o in exdump) |
| 196 | |
| 197 | def save(self): |
| 198 | with open('.run_tests_cache', 'w') as f: |
Craig Tiller | 261dd98 | 2015-01-16 16:41:45 -0800 | [diff] [blame] | 199 | f.write(json.dumps(self.dump())) |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 200 | |
Craig Tiller | 1cc11db | 2015-01-15 22:50:50 -0800 | [diff] [blame] | 201 | def maybe_load(self): |
| 202 | if os.path.exists('.run_tests_cache'): |
| 203 | with open('.run_tests_cache') as f: |
Craig Tiller | 261dd98 | 2015-01-16 16:41:45 -0800 | [diff] [blame] | 204 | self.parse(json.loads(f.read())) |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def _build_and_run(check_cancelled, newline_on_success, cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 208 | """Do one pass of building & running tests.""" |
murgatroid99 | 666450e | 2015-01-26 13:03:31 -0800 | [diff] [blame] | 209 | # build latest sequentially |
murgatroid99 | c279165 | 2015-01-26 11:33:39 -0800 | [diff] [blame] | 210 | if not jobset.run(build_steps, maxjobs=1): |
Craig Tiller | d86a394 | 2015-01-14 12:48:54 -0800 | [diff] [blame] | 211 | return 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 212 | |
| 213 | # run all the tests |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 214 | one_run = dict( |
| 215 | (' '.join(config.run_command(x)), config.run_command(x)) |
| 216 | for config in run_configs |
| 217 | for language in args.language |
| 218 | for x in _LANGUAGES[language].test_binaries(config.build_config) |
| 219 | ).values() |
| 220 | all_runs = itertools.chain.from_iterable( |
| 221 | itertools.repeat(one_run, runs_per_test)) |
| 222 | if not jobset.run(all_runs, check_cancelled, |
| 223 | newline_on_success=newline_on_success, |
| 224 | maxjobs=min(c.maxjobs for c in run_configs), |
| 225 | cache=cache): |
Craig Tiller | d86a394 | 2015-01-14 12:48:54 -0800 | [diff] [blame] | 226 | return 2 |
| 227 | |
| 228 | return 0 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 229 | |
| 230 | |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 231 | test_cache = (None |
| 232 | if not all(x.allow_hashing |
| 233 | for x in itertools.chain(languages, run_configs)) |
Craig Tiller | b50d166 | 2015-01-15 17:28:21 -0800 | [diff] [blame] | 234 | else TestCache()) |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 235 | if test_cache: |
Craig Tiller | 1cc11db | 2015-01-15 22:50:50 -0800 | [diff] [blame] | 236 | test_cache.maybe_load() |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 237 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 238 | if forever: |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 239 | success = True |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 240 | while True: |
| 241 | dw = watch_dirs.DirWatcher(['src', 'include', 'test']) |
| 242 | initial_time = dw.most_recent_change() |
| 243 | have_files_changed = lambda: dw.most_recent_change() != initial_time |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 244 | previous_success = success |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 245 | success = _build_and_run(check_cancelled=have_files_changed, |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 246 | newline_on_success=False, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 247 | cache=test_cache) == 0 |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 248 | if not previous_success and success: |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 249 | jobset.message('SUCCESS', |
| 250 | 'All tests are now passing properly', |
| 251 | do_newline=True) |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 252 | jobset.message('IDLE', 'No change detected') |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 253 | if test_cache: test_cache.save() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 254 | while not have_files_changed(): |
| 255 | time.sleep(1) |
| 256 | else: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 257 | result = _build_and_run(check_cancelled=lambda: False, |
| 258 | newline_on_success=args.newline_on_success, |
| 259 | cache=test_cache) |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 260 | if result == 0: |
| 261 | jobset.message('SUCCESS', 'All tests passed', do_newline=True) |
| 262 | else: |
| 263 | jobset.message('FAILED', 'Some tests failed', do_newline=True) |
Craig Tiller | c744916 | 2015-01-16 14:42:10 -0800 | [diff] [blame] | 264 | if test_cache: test_cache.save() |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 265 | sys.exit(result) |